Class: Bound::BoundClass

Inherits:
Object
  • Object
show all
Defined in:
lib/bound.rb

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.



69
70
71
72
73
# File 'lib/bound.rb', line 69

def initialize(hash_or_object = {})
  build_hash(hash_or_object)
  validate!
  seed
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Raises:

  • (ArgumentError)


75
76
77
78
# File 'lib/bound.rb', line 75

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

Class Attribute Details

.attributesObject

Returns the value of attribute attributes.



26
27
28
# File 'lib/bound.rb', line 26

def attributes
  @attributes
end

.nested_attributesObject

Returns the value of attribute nested_attributes.



26
27
28
# File 'lib/bound.rb', line 26

def nested_attributes
  @nested_attributes
end

.optional_attributesObject

Returns the value of attribute optional_attributes.



26
27
28
# File 'lib/bound.rb', line 26

def optional_attributes
  @optional_attributes
end

Class Method Details

.initialize_valuesObject



28
29
30
31
32
# File 'lib/bound.rb', line 28

def initialize_values
  self.attributes = []
  self.optional_attributes = []
  self.nested_attributes = []
end

.nested(nested_attributes) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/bound.rb', line 48

def nested(nested_attributes)
  attributes = nested_attributes.keys
  self.nested_attributes += attributes
  self.attributes += attributes
  attr_reader *attributes
  
  attributes.each do |attribute|
    define_method :"#{attribute}=" do |initial_values|
      nested_target = nested_attributes[attribute]
      value = extract_values_for_nested_attribute(nested_target, initial_values)

      instance_variable_set :"@#{attribute}", value
    end
  end

  self
end

.optional(*optionals) ⇒ Object



41
42
43
44
45
46
# File 'lib/bound.rb', line 41

def optional(*optionals)
  self.optional_attributes += optionals
  attr_accessor *optionals

  self
end

.set_attributes(*attributes) ⇒ Object



34
35
36
37
38
39
# File 'lib/bound.rb', line 34

def set_attributes(*attributes)
  self.attributes += attributes
  attr_accessor *attributes

  self
end

Instance Method Details

#inspectObject



80
81
82
83
84
85
86
87
88
# File 'lib/bound.rb', line 80

def inspect
  class_name = self.class.name
  id = '%0#16x' % (object_id << 1)
  values = (self.class.attributes + self.class.optional_attributes).map do |attr|
    "#{attr}=#{public_send(attr).inspect}"
  end

  (["#<#{class_name}:#{id}"] + values + [">"]).join(" ")
end