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.



198
199
200
201
202
203
204
# File 'lib/bound.rb', line 198

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)


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: #{self.class}##{attribute}")
end

Class Attribute Details

.attrsObject

Returns the value of attribute attrs.



118
119
120
# File 'lib/bound.rb', line 118

def attrs
  @attrs
end

.nested_attr_classesObject

Returns the value of attribute nested_attr_classes.



118
119
120
# File 'lib/bound.rb', line 118

def nested_attr_classes
  @nested_attr_classes
end

Class Method Details

.initialize_valuesObject



120
121
122
123
# File 'lib/bound.rb', line 120

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

.optional(*attributes) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/bound.rb', line 125

def optional(*attributes)
  if attributes.last.kind_of? Hash
    nested_attributes = attributes.pop
  else
    nested_attributes = {}
  end

  set_attributes :optional, attributes, nested_attributes

  self
end

.required(*attributes) ⇒ Object



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

def required(*attributes)
  if attributes.last.kind_of? Hash
    nested_attributes = attributes.pop
  else
    nested_attributes = {}
  end

  set_attributes :required, attributes, nested_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



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

#has_attribute?(attr) ⇒ Boolean

Returns:

  • (Boolean)


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

def has_attribute?(attr)
  self.class.attrs.keys.include? attr
end