Class: PlainRecord::AssociationProxy

Inherits:
Array
  • Object
show all
Defined in:
lib/plain_record/association_proxy.rb

Overview

Storage for one-to-many virtual associations. When new object will pushed, proxy change it mapped properties.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(array, owner, property) ⇒ AssociationProxy

Create proxy for one-to-many virtual associations property in owner with array in value.



40
41
42
43
44
# File 'lib/plain_record/association_proxy.rb', line 40

def initialize(array, owner, property)
  @owner = owner
  @property = property
  super(array)
end

Instance Attribute Details

#ownerObject

Model with association.



25
26
27
# File 'lib/plain_record/association_proxy.rb', line 25

def owner
  @owner
end

#propertyObject

Associations property name.



28
29
30
# File 'lib/plain_record/association_proxy.rb', line 28

def property
  @property
end

Class Method Details

Create proxy for one-to-many virtual associations property in owner and put array into it.



32
33
34
35
36
# File 'lib/plain_record/association_proxy.rb', line 32

def self.link(array, owner, property)
  proxy = new(array, owner, property)
  proxy.each { |i| proxy.link(i) }
  proxy
end

Instance Method Details

#<<(obj) ⇒ Object

Push new item in association and change it property by association map.



47
48
49
50
# File 'lib/plain_record/association_proxy.rb', line 47

def <<(obj)
  link(obj)
  super(obj)
end

Change properties in obj by association map.



53
54
55
56
57
# File 'lib/plain_record/association_proxy.rb', line 53

def link(obj)
  @owner.class.association_maps[@property].each do |from, to|
    obj.send(from.to_s + '=', @owner.send(to))
  end
end