File: instance_methods.rb

package info (click to toggle)
ruby-virtus 2.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 660 kB
  • sloc: ruby: 4,378; makefile: 2
file content (218 lines) | stat: -rw-r--r-- 4,841 bytes parent folder | download | duplicates (2)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
module Virtus

  # Instance methods that are added when you include Virtus
  module InstanceMethods

    module Constructor

      # Set attributes during initialization of an object
      #
      # @param [#to_hash] attributes
      #   the attributes hash to be set
      #
      # @return [undefined]
      #
      # @api private
      def initialize(attributes = nil)
        attribute_set.set(self, attributes) if attributes
        set_default_attributes
      end

    end # Constructor

    module MassAssignment

      # Returns a hash of all publicly accessible attributes
      #
      # @example
      #   class User
      #     include Virtus
      #
      #     attribute :name, String
      #     attribute :age,  Integer
      #   end
      #
      #   user = User.new(:name => 'John', :age => 28)
      #   user.attributes  # => { :name => 'John', :age => 28 }
      #
      # @return [Hash]
      #
      # @api public
      def attributes
        attribute_set.get(self)
      end
      alias_method :to_hash, :attributes
      alias_method :to_h, :attributes

      # Mass-assign attribute values
      #
      # Keys in the +attributes+ param can be symbols or strings.
      # All referenced Attribute writer methods *will* be called.
      # Non-attribute setter methods on the receiver *will* be called.
      #
      # @example
      #   class User
      #     include Virtus
      #
      #     attribute :name, String
      #     attribute :age,  Integer
      #   end
      #
      #   user = User.new
      #   user.attributes = { :name => 'John', 'age' => 28 }
      #
      # @param [#to_hash] attributes
      #   a hash of attribute names and values to set on the receiver
      #
      # @return [Hash]
      #
      # @api public
      def attributes=(attributes)
        attribute_set.set(self, attributes)
      end

    end # MassAssignment

    # Returns a value of the attribute with the given name
    #
    # @example
    #   class User
    #     include Virtus
    #
    #     attribute :name, String
    #   end
    #
    #   user = User.new(:name => 'John')
    #   user[:name]  # => "John"
    #
    # @param [Symbol] name
    #   a name of an attribute
    #
    # @return [Object]
    #   a value of an attribute
    #
    # @api public
    def [](name)
      public_send(name)
    end

    # Sets a value of the attribute with the given name
    #
    # @example
    #   class User
    #     include Virtus
    #
    #     attribute :name, String
    #   end
    #
    #   user = User.new
    #   user[:name] = "John"  # => "John"
    #   user.name             # => "John"
    #
    # @param [Symbol] name
    #   a name of an attribute
    #
    # @param [Object] value
    #   a value to be set
    #
    # @return [Object]
    #   the value set on an object
    #
    # @api public
    def []=(name, value)
      public_send("#{name}=", value)
    end

    # Freeze object
    #
    # @return [self]
    #
    # @api public
    #
    # @example
    #
    #   class User
    #     include Virtus
    #
    #     attribute :name, String
    #     attribute :age,  Integer
    #   end
    #
    #   user = User.new(:name => 'John', :age => 28)
    #   user.frozen? # => false
    #   user.freeze
    #   user.frozen? # => true
    #
    # @api public
    def freeze
      set_default_attributes!
      super
    end

    # Reset an attribute to its default
    #
    # @return [self]
    #
    # @api public
    #
    # @example
    #
    #   class User
    #     include Virtus
    #
    #     attribute :age,  Integer, default: 21
    #   end
    #
    #   user = User.new(:name => 'John', :age => 28)
    #   user.age = 30
    #   user.age # => 30
    #   user.reset_attribute(:age)
    #   user.age # => 21
    #
    # @api public
    def reset_attribute(attribute_name)
      attribute = attribute_set[attribute_name]
      attribute.set_default_value(self) if attribute
      self
    end

    # Set default attributes
    #
    # @return [self]
    #
    # @api private
    def set_default_attributes
      attribute_set.set_defaults(self)
      self
    end

    # Set default attributes even lazy ones
    #
    # @return [self]
    #
    # @api public
    def set_default_attributes!
      attribute_set.set_defaults(self, proc { |object, attribute| attribute.defined?(object) })
      self
    end

    private

    # The list of allowed public methods
    #
    # @return [Array<String>]
    #
    # @api private
    def allowed_methods
      public_methods.map(&:to_s)
    end

    # @api private
    def assert_valid_name(name)
      if respond_to?(:attributes) && name.to_sym == :attributes || name.to_sym == :attribute_set
        raise ArgumentError, "#{name.inspect} is not allowed as an attribute name"
      end
    end

  end # module InstanceMethods
end # module Virtus