Class: SmoothOperator::Associations::HasManyRelation

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/smooth_operator/associations/has_many_relation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, association) ⇒ HasManyRelation

Returns a new instance of HasManyRelation.



7
8
9
# File 'lib/smooth_operator/associations/has_many_relation.rb', line 7

def initialize(object, association)
  @object, @association = object, association
end

Instance Attribute Details

#associationObject (readonly)

Returns the value of attribute association.



5
6
7
# File 'lib/smooth_operator/associations/has_many_relation.rb', line 5

def association
  @association
end

#objectObject (readonly)

Returns the value of attribute object.



5
6
7
# File 'lib/smooth_operator/associations/has_many_relation.rb', line 5

def object
  @object
end

Instance Method Details

#attributesObject



35
36
37
# File 'lib/smooth_operator/associations/has_many_relation.rb', line 35

def attributes
  get_array.map(&:attributes)
end

#build(attributes = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/smooth_operator/associations/has_many_relation.rb', line 19

def build(attributes = {})
  new_entries = if attributes.is_a?(Array)
    attributes.map { |attrs| new(attrs) }
  else
    [new(attributes)]
  end

  new_array = get_array

  new_array.push *new_entries

  object.send("#{association}=", new_array)

  attributes.is_a?(Array) ? new_entries : new_entries.first
end

#new(attributes = {}) ⇒ Object



15
16
17
# File 'lib/smooth_operator/associations/has_many_relation.rb', line 15

def new(attributes = {})
  object.class.reflect_on_association(association).klass.new(attributes)
end

#reloadObject



11
12
13
# File 'lib/smooth_operator/associations/has_many_relation.rb', line 11

def reload
  "TODO"
end