Class: Bound::BoundClass

Inherits:
Object
  • 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(*seeds) ⇒ BoundClass

Returns a new instance of BoundClass.



202
203
204
205
206
207
208
# File 'lib/bound.rb', line 202

def initialize(*seeds)
  @attributes = {}
  seeds.reverse.each do |seed|
    seed_with seed
  end
  validate!
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &blk) ⇒ Object

Raises:

  • (ArgumentError)


211
212
213
214
# File 'lib/bound.rb', line 211

def method_missing(meth, *args, &blk)
  attribute = meth.to_s.gsub(/=$/, '')
  raise ArgumentError.new("Unknown attribute: #{self.class}##{attribute}")
end

Class Attribute Details

.attrsObject

Returns the value of attribute attrs.



128
129
130
# File 'lib/bound.rb', line 128

def attrs
  @attrs
end

.nested_attr_classesObject

Returns the value of attribute nested_attr_classes.



128
129
130
# File 'lib/bound.rb', line 128

def nested_attr_classes
  @nested_attr_classes
end

Class Method Details

.initialize_valuesObject



130
131
132
133
# File 'lib/bound.rb', line 130

def initialize_values
  self.attrs = {}
  self.nested_attr_classes = {}
end

.nested(nested_attributes) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/bound.rb', line 163

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



149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/bound.rb', line 149

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



135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/bound.rb', line 135

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



222
223
224
225
# File 'lib/bound.rb', line 222

def __attributes__
  puts "BoundClass#__attributes__ is deprecated: use get_attributes"
  get_attributes.map(&:name)
end

#get_attribute(attribute_name) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/bound.rb', line 227

def get_attribute(attribute_name)
  attribute_class = self.class.attrs[attribute_name]
  nested_class = self.class.nested_attr_classes[attribute_name]

  return nil if attribute_class.nil?

  attribute = @attributes[attribute_name]

  unless attribute
    @attributes[attribute_name] = attribute_class.new(attribute_name)
    attribute = @attributes[attribute_name]
    attribute.nested_class = nested_class if nested_class
  end

  attribute
end

#get_attributesObject



216
217
218
219
220
# File 'lib/bound.rb', line 216

def get_attributes
  self.class.attrs.keys.map do |attribute_name|
    get_attribute(attribute_name)
  end
end