Class: Bound::BoundClass
- Inherits:
-
Object
show all
- Defined in:
- lib/bound.rb
Defined Under Namespace
Classes: Attribute, NestedAttribute, RequiredAttribute
Class Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(hash_or_object = {}) ⇒ BoundClass
Returns a new instance of BoundClass.
200
201
202
203
204
|
# File 'lib/bound.rb', line 200
def initialize(hash_or_object = {})
@attributes = {}
seed hash_or_object
validate!
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &blk) ⇒ Object
207
208
209
210
|
# File 'lib/bound.rb', line 207
def method_missing(meth, *args, &blk)
attribute = meth.to_s.gsub(/=$/, '')
raise ArgumentError.new("Unknown attribute: #{attribute}")
end
|
Class Attribute Details
.attrs ⇒ Object
Returns the value of attribute attrs.
126
127
128
|
# File 'lib/bound.rb', line 126
def attrs
@attrs
end
|
.nested_attr_classes ⇒ Object
Returns the value of attribute nested_attr_classes.
126
127
128
|
# File 'lib/bound.rb', line 126
def nested_attr_classes
@nested_attr_classes
end
|
Class Method Details
.initialize_values ⇒ Object
128
129
130
131
|
# File 'lib/bound.rb', line 128
def initialize_values
self.attrs = {}
self.nested_attr_classes = {}
end
|
.nested(nested_attributes) ⇒ Object
161
162
163
164
165
166
167
168
169
170
171
172
|
# File 'lib/bound.rb', line 161
def nested(nested_attributes)
attributes = nested_attributes.keys
attributes.each do |attribute|
self.attrs[attribute] = RequiredAttribute
self.nested_attr_classes[attribute] = nested_attributes[attribute]
end
define_attribute_accessors attributes
self
end
|
.optional(*optionals) ⇒ Object
147
148
149
150
151
152
153
154
155
156
157
158
159
|
# File 'lib/bound.rb', line 147
def optional(*optionals)
if optionals.any? { |a| !a.kind_of? Symbol }
raise ArgumentError.new("Invalid list of optional attributes: #{optionals.inspect}")
end
optionals.each do |attribute|
self.attrs[attribute] = Attribute
end
define_attribute_accessors optionals
self
end
|
.set_attributes(*attributes) ⇒ Object
133
134
135
136
137
138
139
140
141
142
143
144
145
|
# File 'lib/bound.rb', line 133
def set_attributes(*attributes)
if attributes.any? { |a| !a.kind_of? Symbol }
raise ArgumentError.new("Invalid list of attributes: #{attributes.inspect}")
end
attributes.each do |attribute|
self.attrs[attribute] = RequiredAttribute
end
define_attribute_accessors attributes
self
end
|
Instance Method Details
#__attributes__ ⇒ Object
218
219
220
221
|
# File 'lib/bound.rb', line 218
def __attributes__
puts "BoundClass#__attributes__ is deprecated: use get_attributes"
get_attributes.map(&:name)
end
|
#get_attributes ⇒ Object
212
213
214
215
216
|
# File 'lib/bound.rb', line 212
def get_attributes
self.class.attrs.keys.map do |attribute_name|
get_attribute(attribute_name)
end
end
|