Class: Consumerable::Associations::HasManyProxy

Inherits:
BasicObject
Defined in:
lib/consumerable/associations/has_many_proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, target_klass, target, source) ⇒ HasManyProxy

Returns a new instance of HasManyProxy.



7
8
9
10
11
12
# File 'lib/consumerable/associations/has_many_proxy.rb', line 7

def initialize(parent, target_klass, target, source)
  @parent         = parent
  @target_klass   = target_klass
  @target         = target
  @source         = source
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



14
15
16
# File 'lib/consumerable/associations/has_many_proxy.rb', line 14

def method_missing(name, *args, &block)
  target.send(name, *args, &block)
end

Instance Attribute Details

#parentObject (readonly)

Returns the value of attribute parent.



5
6
7
# File 'lib/consumerable/associations/has_many_proxy.rb', line 5

def parent
  @parent
end

#sourceObject (readonly)

Returns the value of attribute source.



5
6
7
# File 'lib/consumerable/associations/has_many_proxy.rb', line 5

def source
  @source
end

#targetObject (readonly)

Returns the value of attribute target.



5
6
7
# File 'lib/consumerable/associations/has_many_proxy.rb', line 5

def target
  @target
end

#target_klassObject (readonly)

Returns the value of attribute target_klass.



5
6
7
# File 'lib/consumerable/associations/has_many_proxy.rb', line 5

def target_klass
  @target_klass
end

Instance Method Details

#build(attributes) ⇒ Object



22
23
24
25
26
27
# File 'lib/consumerable/associations/has_many_proxy.rb', line 22

def build(attributes)
  target_klass.new(attributes).tap do |item|
    item.send("#{source}=", parent)
    target << item
  end
end

#create(attributes = {}) ⇒ Object



18
19
20
# File 'lib/consumerable/associations/has_many_proxy.rb', line 18

def create(attributes = {})
  build(attributes).save
end

#tap {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



29
30
31
32
# File 'lib/consumerable/associations/has_many_proxy.rb', line 29

def tap
  yield self
  self
end