Class: PlainRecord::AssociationProxy
- Inherits:
-
Array
- Object
- Array
- PlainRecord::AssociationProxy
- 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
-
#owner ⇒ Object
Model with association.
-
#property ⇒ Object
Associations property name.
Class Method Summary collapse
-
.link(array, owner, property) ⇒ Object
Create proxy for one-to-many virtual associations
property
inowner
and putarray
into it.
Instance Method Summary collapse
-
#<<(obj) ⇒ Object
Push new item in association and change it property by association map.
-
#initialize(array, owner, property) ⇒ AssociationProxy
constructor
Create proxy for one-to-many virtual associations
property
inowner
witharray
in value. -
#link(obj) ⇒ Object
Change properties in
obj
by association map.
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
#owner ⇒ Object
Model with association.
25 26 27 |
# File 'lib/plain_record/association_proxy.rb', line 25 def owner @owner end |
#property ⇒ Object
Associations property name.
28 29 30 |
# File 'lib/plain_record/association_proxy.rb', line 28 def property @property end |
Class Method Details
.link(array, owner, property) ⇒ Object
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 |
#link(obj) ⇒ Object
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 |