Module: Dynamoid::Associations::Association
- Includes:
- Enumerable
- Included in:
- BelongsTo, HasAndBelongsToMany, HasMany, HasOne
- Defined in:
- lib/dynamoid/associations/association.rb
Instance Attribute Summary (collapse)
-
- (Object) name
Returns the value of attribute name.
-
- (Object) options
Returns the value of attribute options.
-
- (Object) query
Returns the value of attribute query.
-
- (Object) source
Returns the value of attribute source.
Instance Method Summary (collapse)
-
- (Dynamoid::Document) <<(object)
Add an object or array of objects to an association.
-
- (Dynamoid::Document) create(attributes = {})
Create a new instance of the target class and add it directly to the association.
-
- (Dynamoid::Document) create!(attributes = {})
Create a new instance of the target class and add it directly to the association.
-
- (Dynamoid::Document) delete(object)
Deletes an object or array of objects from the association.
-
- (Object) delete_all
Deletes all members of the association and removes them from the association.
-
- (Object) destroy_all
Destroys all members of the association and removes them from the association.
-
- (Dynamoid::Document) each(&block)
Create a new instance of the target class and add it directly to the association.
-
- (Boolean) include?(object)
Delegate include? to the records.
-
- (Dynamoid::Association) initialize(source, name, options)
Create a new association.
-
- (Object) records
(also: #all)
The records associated to the source.
-
- (Dynamoid::Document) setter(object)
Replace an association with object or array of objects.
-
- (Dynamoid::Association) where(args)
Naive association filtering.
Instance Attribute Details
- (Object) name
Returns the value of attribute name
9 10 11 |
# File 'lib/dynamoid/associations/association.rb', line 9 def name @name end |
- (Object) options
Returns the value of attribute options
9 10 11 |
# File 'lib/dynamoid/associations/association.rb', line 9 def @options end |
- (Object) query
Returns the value of attribute query
9 10 11 |
# File 'lib/dynamoid/associations/association.rb', line 9 def query @query end |
- (Object) source
Returns the value of attribute source
9 10 11 |
# File 'lib/dynamoid/associations/association.rb', line 9 def source @source end |
Instance Method Details
- (Dynamoid::Document) <<(object)
Add an object or array of objects to an association. This preserves the current records in the association (if any) and adds the object to the target association if it is detected to exist.
83 84 85 86 87 |
# File 'lib/dynamoid/associations/association.rb', line 83 def <<(object) source.update_attribute(source_attribute, source_ids.merge(Array(object).collect(&:id))) Array(object).collect{|o| self.send(:associate_target, o)} if target_association object end |
- (Dynamoid::Document) create(attributes = {})
Create a new instance of the target class and add it directly to the association.
110 111 112 |
# File 'lib/dynamoid/associations/association.rb', line 110 def create(attributes = {}) self << target_class.create(attributes) end |
- (Dynamoid::Document) create!(attributes = {})
Create a new instance of the target class and add it directly to the association. If the create fails an exception will be raised.
121 122 123 |
# File 'lib/dynamoid/associations/association.rb', line 121 def create!(attributes = {}) self << target_class.create!(attributes) end |
- (Dynamoid::Document) delete(object)
Deletes an object or array of objects from the association. This removes their records from the association field on the source, and attempts to remove the source from the target association if it is detected to exist.
69 70 71 72 73 |
# File 'lib/dynamoid/associations/association.rb', line 69 def delete(object) source.update_attribute(source_attribute, source_ids - Array(object).collect(&:id)) Array(object).collect{|o| self.send(:disassociate_target, o)} if target_association object end |
- (Object) delete_all
Deletes all members of the association and removes them from the association.
161 162 163 164 165 |
# File 'lib/dynamoid/associations/association.rb', line 161 def delete_all objs = records source.update_attribute(source_attribute, nil) objs.each(&:delete) end |
- (Object) destroy_all
Destroys all members of the association and removes them from the association.
152 153 154 155 156 |
# File 'lib/dynamoid/associations/association.rb', line 152 def destroy_all objs = records source.update_attribute(source_attribute, nil) objs.each(&:destroy) end |
- (Dynamoid::Document) each(&block)
Create a new instance of the target class and add it directly to the association. If the create fails an exception will be raised.
145 146 147 |
# File 'lib/dynamoid/associations/association.rb', line 145 def each(&block) records.each(&block) end |
- (Boolean) include?(object)
Delegate include? to the records.
55 56 57 |
# File 'lib/dynamoid/associations/association.rb', line 55 def include?(object) records.include?(object) end |
- (Dynamoid::Association) initialize(source, name, options)
Create a new association.
27 28 29 30 31 32 |
# File 'lib/dynamoid/associations/association.rb', line 27 def initialize(source, name, ) @name = name @options = @source = source @query = {} end |
- (Object) records Also known as: all
The records associated to the source.
43 44 45 46 47 48 49 50 51 |
# File 'lib/dynamoid/associations/association.rb', line 43 def records results = Array(target_class.find(source_ids.to_a)) if query.empty? results else results_with_query(results) end end |
- (Dynamoid::Document) setter(object)
Replace an association with object or array of objects. This removes all of the existing associated records and replaces them with the passed object(s), and associates the target association if it is detected to exist.
97 98 99 100 101 |
# File 'lib/dynamoid/associations/association.rb', line 97 def setter(object) records.each {|o| delete(o)} self << (object) object end |
- (Dynamoid::Association) where(args)
Naive association filtering.
133 134 135 136 |
# File 'lib/dynamoid/associations/association.rb', line 133 def where(args) args.each {|k, v| query[k] = v} self end |