Class: Og::Collection
- Inherits:
-
Object
- Object
- Og::Collection
- Defined in:
- lib/og/collection.rb
Overview
An ‘active’ collection that reflects a relation. A collection stores entitities that participate in a relation.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#building ⇒ Object
Is the collection in build mode?.
-
#find_options ⇒ Object
The default find options.
-
#find_proc ⇒ Object
A method used to find the objects that belong to the collection.
-
#insert_proc ⇒ Object
A method used to add insert objects in the collection.
-
#loaded ⇒ Object
Is the collection loaded?.
-
#members ⇒ Object
The members of this collection.
-
#owner ⇒ Object
The owner of this collection.
-
#remove_proc ⇒ Object
A method used to remove objects from the collection.
Instance Method Summary collapse
-
#[](idx) ⇒ Object
Defined to avoid the method missing overhead.
-
#delete(*objects) ⇒ Object
Delete a member from the collection AND the store.
-
#delete_all ⇒ Object
Delete all members of the collection.
-
#each(&block) ⇒ Object
Defined to avoid the method missing overhead.
-
#initialize(owner = nil, insert_proc = nil, remove_proc = nil, find_proc = nil, find_options = {}) ⇒ Collection
constructor
Initialize the collection.
-
#load_members ⇒ Object
Load the members of the collection.
-
#method_missing(symbol, *args, &block) ⇒ Object
Redirect all other methods to the members array.
-
#push(obj) ⇒ Object
(also: #<<, #add)
Add a new member to the collection.
-
#reload(options) ⇒ Object
Reload the collection.
-
#remove(*objects) ⇒ Object
Remove a member from the collection, the actual object is not deleted.
-
#remove_all ⇒ Object
(also: #clear)
Remove all members from the collection.
-
#to_ary ⇒ Object
Convert the collection to an array.
Constructor Details
#initialize(owner = nil, insert_proc = nil, remove_proc = nil, find_proc = nil, find_options = {}) ⇒ Collection
Initialize the collection.
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/og/collection.rb', line 45 def initialize(owner = nil, insert_proc = nil, remove_proc = nil, find_proc = nil, = {}) @owner = owner @insert_proc = insert_proc @remove_proc = remove_proc @find_proc = find_proc @find_options = @members = [] @loaded = false @building = false end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, *args, &block) ⇒ Object
Redirect all other methods to the members array.
160 161 162 163 |
# File 'lib/og/collection.rb', line 160 def method_missing(symbol, *args, &block) load_members @members.send(symbol, *args, &block) end |
Instance Attribute Details
#building ⇒ Object
Is the collection in build mode?
37 38 39 |
# File 'lib/og/collection.rb', line 37 def building @building end |
#find_options ⇒ Object
The default find options.
33 34 35 |
# File 'lib/og/collection.rb', line 33 def @find_options end |
#find_proc ⇒ Object
A method used to find the objects that belong to the collection.
29 30 31 |
# File 'lib/og/collection.rb', line 29 def find_proc @find_proc end |
#insert_proc ⇒ Object
A method used to add insert objects in the collection.
20 21 22 |
# File 'lib/og/collection.rb', line 20 def insert_proc @insert_proc end |
#loaded ⇒ Object
Is the collection loaded?
41 42 43 |
# File 'lib/og/collection.rb', line 41 def loaded @loaded end |
#members ⇒ Object
The members of this collection. Keeps the objects tha belong to this collection.
16 17 18 |
# File 'lib/og/collection.rb', line 16 def members @members end |
#owner ⇒ Object
The owner of this collection.
11 12 13 |
# File 'lib/og/collection.rb', line 11 def owner @owner end |
#remove_proc ⇒ Object
A method used to remove objects from the collection.
24 25 26 |
# File 'lib/og/collection.rb', line 24 def remove_proc @remove_proc end |
Instance Method Details
#[](idx) ⇒ Object
Defined to avoid the method missing overhead.
89 90 91 92 |
# File 'lib/og/collection.rb', line 89 def [](idx) load_members @members[idx] end |
#delete(*objects) ⇒ Object
Delete a member from the collection AND the store.
124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/og/collection.rb', line 124 def delete(*objects) objects = objects.flatten objects.reject! { |obj| @members.delete(obj) if obj.unsaved? } return if objects.empty? @owner.transaction do objects.each do |obj| obj.delete @members.delete(obj) end end end |
#delete_all ⇒ Object
Delete all members of the collection. Also delete from the store.
151 152 153 154 155 156 |
# File 'lib/og/collection.rb', line 151 def delete_all @owner.transaction do @members.each { |obj| obj.delete } end @members.clear end |
#each(&block) ⇒ Object
Defined to avoid the method missing overhead.
82 83 84 85 |
# File 'lib/og/collection.rb', line 82 def each(&block) load_members @members.each(&block) end |
#load_members ⇒ Object
Load the members of the collection.
58 59 60 61 62 63 64 |
# File 'lib/og/collection.rb', line 58 def load_members unless @loaded @members = @owner.send(@find_proc, @find_options) @loaded = true end @members end |
#push(obj) ⇒ Object Also known as: <<, add
Add a new member to the collection.
96 97 98 99 100 101 |
# File 'lib/og/collection.rb', line 96 def push(obj) @members.push(obj) unless @building or owner.unsaved? @owner.send(@insert_proc, obj) end end |
#reload(options) ⇒ Object
Reload the collection.
68 69 70 71 |
# File 'lib/og/collection.rb', line 68 def reload() @find_options = @members = @owner.send(@find_proc, @find_options) end |
#remove(*objects) ⇒ Object
Remove a member from the collection, the actual object is not deleted.
108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/og/collection.rb', line 108 def remove(*objects) objects = objects.flatten objects.reject! { |obj| @members.delete(obj) if obj.unsaved? } return if objects.empty? @owner.transaction do objects.each do |obj| @owner.send(@remove_proc, obj) @members.delete(obj) end end end |
#remove_all ⇒ Object Also known as: clear
Remove all members from the collection.
140 141 142 143 144 145 |
# File 'lib/og/collection.rb', line 140 def remove_all @owner.transaction do @members.each { |obj| @owner.send(@remove_proc, obj) } end @members.clear end |
#to_ary ⇒ Object
Convert the collection to an array.
75 76 77 78 |
# File 'lib/og/collection.rb', line 75 def to_ary load_members @members end |