Module: Dynamoid::Associations::ManyAssociation

Includes:
Association, Enumerable
Included in:
HasAndBelongsToMany, HasMany
Defined in:
lib/dynamoid/associations/many_association.rb

Instance Attribute Summary (collapse)

Attributes included from Association

#loaded, #name, #options, #source

Instance Method Summary (collapse)

Methods included from Association

#loaded?, #reset, #target

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(method, *args)

Delegate methods we don't find directly to the records array.

Since:

  • 0.2.0



164
165
166
167
168
169
170
# File 'lib/dynamoid/associations/many_association.rb', line 164

def method_missing(method, *args)
  if records.respond_to?(method)
    records.send(method, *args)
  else
    super
  end
end

Instance Attribute Details

- (Object) query

Returns the value of attribute query



8
9
10
# File 'lib/dynamoid/associations/many_association.rb', line 8

def query
  @query
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.

Parameters:

  • object (Dynamoid::Document)

    the object (or array of objects) to add to the association

Returns:

Since:

  • 0.2.0



69
70
71
72
73
# File 'lib/dynamoid/associations/many_association.rb', line 69

def <<(object)
  source.update_attribute(source_attribute, source_ids.merge(Array(object).collect(&:id)))
  Array(object).each {|o| self.send(:associate_target, o)} if target_association
  object
end

- (Boolean) ==(other)

Is this array equal to the association's records?

Returns:

  • (Boolean)

    true/false

Since:

  • 0.2.0



157
158
159
# File 'lib/dynamoid/associations/many_association.rb', line 157

def ==(other)
  records == Array(other)
end

- (Dynamoid::Document) create(attributes = {})

Create a new instance of the target class and add it directly to the association.

Parameters:

  • attribute (Hash)

    hash for the new object

Returns:

Since:

  • 0.2.0



107
108
109
# File 'lib/dynamoid/associations/many_association.rb', line 107

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.

Parameters:

  • attribute (Hash)

    hash for the new object

Returns:

Since:

  • 0.2.0



96
97
98
# File 'lib/dynamoid/associations/many_association.rb', line 96

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.

Parameters:

  • object (Dynamoid::Document)

    the object (or array of objects) to remove from the association

Returns:

Since:

  • 0.2.0



54
55
56
57
58
# File 'lib/dynamoid/associations/many_association.rb', line 54

def delete(object)
  source.update_attribute(source_attribute, source_ids - Array(object).collect(&:id))
  Array(object).each {|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.

Since:

  • 0.2.0



134
135
136
137
138
# File 'lib/dynamoid/associations/many_association.rb', line 134

def delete_all
  objs = target
  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.

Since:

  • 0.2.0



125
126
127
128
129
# File 'lib/dynamoid/associations/many_association.rb', line 125

def destroy_all
  objs = target
  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.

Parameters:

  • attribute (Hash)

    hash for the new object

Returns:

Since:

  • 0.2.0



118
119
120
# File 'lib/dynamoid/associations/many_association.rb', line 118

def each(&block)
  records.each(&block)
end

- (Object) find_target

The records associated to the source.

Returns:

  • the association records; depending on which association this is, either a single instance or an array

Since:

  • 0.2.0



24
25
26
# File 'lib/dynamoid/associations/many_association.rb', line 24

def find_target
  Array(target_class.find(source_ids.to_a))
end

- (Boolean) include?(object)

Delegate include? to the records.

Returns:

  • (Boolean)


42
43
44
# File 'lib/dynamoid/associations/many_association.rb', line 42

def include?(object)
  records.include?(object)
end

- (ManyAssociation) initialize(*args)

A new instance of ManyAssociation



10
11
12
13
# File 'lib/dynamoid/associations/many_association.rb', line 10

def initialize(*args)
  @query = {}
  super
end

- (Object) records Also known as: all



28
29
30
31
32
33
34
# File 'lib/dynamoid/associations/many_association.rb', line 28

def records
  if query.empty?
    target
  else
    results_with_query(target)
  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.

Parameters:

  • object (Dynamoid::Document)

    the object (or array of objects) to add to the association

Returns:

Since:

  • 0.2.0



83
84
85
86
87
# File 'lib/dynamoid/associations/many_association.rb', line 83

def setter(object)
  target.each {|o| delete(o)}
  self << (object)
  object
end

- (Dynamoid::Association) where(args)

Naive association filtering.

Parameters:

  • A (Hash)

    hash of attributes; each must match every returned object's attribute exactly.

Returns:

  • (Dynamoid::Association)

    the association this method was called on (for chaining purposes)

Since:

  • 0.2.0



147
148
149
150
# File 'lib/dynamoid/associations/many_association.rb', line 147

def where(args)
  args.each {|k, v| query[k] = v}
  self
end