-
Notifications
You must be signed in to change notification settings - Fork 229
/
instance_methods.rb
194 lines (180 loc) · 4.25 KB
/
instance_methods.rb
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
module Virtus
# Instance methods that are added when you include Virtus
module InstanceMethods
# 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)
self.attributes = attributes if attributes
end
# 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)
get_attribute(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)
set_attribute(name, value)
end
# 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
get_attributes(&:public_reader?)
end
# 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)
set_attributes(attributes)
end
# 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 to_hash
attributes
end
private
# Get values of all attributes defined for this class, ignoring privacy
#
# @return [Hash]
#
# @api private
def get_attributes
attribute_set.each_with_object({}) do |attribute, attributes|
name = attribute.name
attributes[name] = get_attribute(name) if yield(attribute)
end
end
# Mass-assign attribute values
#
# @see Virtus::InstanceMethods#attributes=
#
# @return [Hash]
#
# @api private
def set_attributes(attributes)
::Hash.try_convert(attributes).each do |name, value|
set_attribute(name, value) if allowed_writer_methods.include?("#{name}=")
end
end
# Returns a value of the attribute with the given name
#
# @see Virtus::InstanceMethods#[]
#
# @return [Object]
#
# @api private
def get_attribute(name)
__send__(name)
end
# Sets a value of the attribute with the given name
#
# @see Virtus::InstanceMethods#[]=
#
# @return [Object]
#
# @api private
def set_attribute(name, value)
__send__("#{name}=", value)
end
# The list of allowed public methods
#
# @return [Array<String>]
#
# @api private
def allowed_methods
public_methods.map(&:to_s)
end
end # module InstanceMethods
end # module Virtus