Class: Infuser::Collections::ChildProxy
- Inherits:
-
Object
- Object
- Infuser::Collections::ChildProxy
- Includes:
- Enumerable
- Defined in:
- lib/infuser/collections/child_proxy.rb
Instance Attribute Summary collapse
-
#child_klass ⇒ Object
readonly
Returns the value of attribute child_klass.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Instance Method Summary collapse
- #<<(item) ⇒ Object
- #each(&block) ⇒ Object
- #find(id) ⇒ Object
-
#initialize(parent, child_klass) ⇒ ChildProxy
constructor
A new instance of ChildProxy.
- #remove(item) ⇒ Object
- #set ⇒ Object
Constructor Details
#initialize(parent, child_klass) ⇒ ChildProxy
Returns a new instance of ChildProxy.
11 12 13 14 |
# File 'lib/infuser/collections/child_proxy.rb', line 11 def initialize parent, child_klass @parent = parent @child_klass = child_klass end |
Instance Attribute Details
#child_klass ⇒ Object (readonly)
Returns the value of attribute child_klass.
9 10 11 |
# File 'lib/infuser/collections/child_proxy.rb', line 9 def child_klass @child_klass end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
9 10 11 |
# File 'lib/infuser/collections/child_proxy.rb', line 9 def parent @parent end |
Instance Method Details
#<<(item) ⇒ Object
25 26 27 28 29 |
# File 'lib/infuser/collections/child_proxy.rb', line 25 def << item item.save if item.new_record? item.send(:update, { parent_klass_field => parent.id }) set << item end |
#each(&block) ⇒ Object
31 32 33 |
# File 'lib/infuser/collections/child_proxy.rb', line 31 def each &block set.each(&block) end |
#find(id) ⇒ Object
35 36 37 |
# File 'lib/infuser/collections/child_proxy.rb', line 35 def find id set.find { |item| item.id == id } end |
#remove(item) ⇒ Object
20 21 22 23 |
# File 'lib/infuser/collections/child_proxy.rb', line 20 def remove item item.destroy set.delete item end |
#set ⇒ Object
16 17 18 |
# File 'lib/infuser/collections/child_proxy.rb', line 16 def set @set ||= table.find_by(parent_klass_field => parent.id) end |