File: anima.rb

package info (click to toggle)
ruby-unparser 0.6.13-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 936 kB
  • sloc: ruby: 7,691; sh: 6; makefile: 4
file content (184 lines) | stat: -rw-r--r-- 4,381 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# frozen_string_literal: true

module Unparser
  # Original code before vendoring and reduction from: https://github.com/mbj/anima.
  class Anima < Module
    include Adamantium, Equalizer.new(:attributes)

    # Return names
    #
    # @return [AttributeSet]
    attr_reader :attributes

    # Initialize object
    #
    # @return [undefined]
    #
    # rubocop:disable Lint/MissingSuper
    def initialize(*names)
      @attributes = names.uniq.map(&Attribute.public_method(:new)).freeze
    end
    # rubocop:enable Lint/MissingSuper

    # Return new anima with attributes added
    #
    # @return [Anima]
    #
    # @example
    #   anima = Anima.new(:foo)
    #   anima.add(:bar) # equals Anima.new(:foo, :bar)
    #
    def add(*names)
      new(attribute_names + names)
    end

    # Return new anima with attributes removed
    #
    # @return [Anima]
    #
    # @example
    #   anima = Anima.new(:foo, :bar)
    #   anima.remove(:bar) # equals Anima.new(:foo)
    #
    def remove(*names)
      new(attribute_names - names)
    end

    # Return attributes hash for instance
    #
    # @param [Object] object
    #
    # @return [Hash]
    def attributes_hash(object)
      attributes.each_with_object({}) do |attribute, attributes_hash|
        attributes_hash[attribute.name] = attribute.get(object)
      end
    end

    # Return attribute names
    #
    # @return [Enumerable<Symbol>]
    def attribute_names
      attributes.map(&:name)
    end
    memoize :attribute_names

    # Initialize instance
    #
    # @param [Object] object
    #
    # @param [Hash] attribute_hash
    #
    # @return [self]
    def initialize_instance(object, attribute_hash)
      assert_known_attributes(object.class, attribute_hash)
      attributes.each do |attribute|
        attribute.load(object, attribute_hash)
      end
      self
    end

    # Static instance methods for anima infected classes
    module InstanceMethods
      # Initialize an anima infected object
      #
      # @param [#to_h] attributes
      #   a hash that matches anima defined attributes
      #
      # @return [undefined]
      #
      # rubocop:disable Lint/MissingSuper
      def initialize(attributes)
        self.class.anima.initialize_instance(self, attributes)
      end
      # rubocop:enable Lint/MissingSuper

      # Return a hash representation of an anima infected object
      #
      # @example
      #   anima.to_h # => { :foo => : bar }
      #
      # @return [Hash]
      #
      # @api public
      def to_h
        self.class.anima.attributes_hash(self)
      end

      # Return updated instance
      #
      # @example
      #   klass = Class.new do
      #     include Anima.new(:foo, :bar)
      #   end
      #
      #   foo = klass.new(:foo => 1, :bar => 2)
      #   updated = foo.with(:foo => 3)
      #   updated.foo # => 3
      #   updated.bar # => 2
      #
      # @param [Hash] attributes
      #
      # @return [Anima]
      #
      # @api public
      def with(attributes)
        self.class.new(to_h.update(attributes))
      end
    end # InstanceMethods

    private

    # Infect the instance with anima
    #
    # @param [Class, Module] scope
    #
    # @return [undefined]
    def included(descendant)
      descendant.instance_exec(self, attribute_names) do |anima, names|
        # Define anima method
        define_singleton_method(:anima) { anima }

        # Define instance methods
        include InstanceMethods

        # Define attribute readers
        attr_reader(*names)

        # Define equalizer
        include Equalizer.new(*names)
      end
    end

    # Fail unless keys in +attribute_hash+ matches #attribute_names
    #
    # @param [Class] klass
    #   the class being initialized
    #
    # @param [Hash] attribute_hash
    #   the attributes to initialize +object+ with
    #
    # @return [undefined]
    #
    # @raise [Error]
    def assert_known_attributes(klass, attribute_hash)
      keys = attribute_hash.keys

      unknown = keys - attribute_names
      missing = attribute_names - keys

      unless unknown.empty? && missing.empty?
        fail Error.new(klass, missing, unknown)
      end
    end

    # Return new instance
    #
    # @param [Enumerable<Symbol>] attributes
    #
    # @return [Anima]
    def new(attributes)
      self.class.new(*attributes)
    end
  end # Anima
end # Unparser