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

#declaration_field_name, #declaration_field_type, #loaded?, #reset, #target

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

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

Since:

  • 0.2.0



171
172
173
174
175
176
177
# File 'lib/dynamoid/associations/many_association.rb', line 171

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

Instance Attribute Details

#queryObject

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

#<<(object) ⇒ Dynamoid::Document

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



70
71
72
73
74
75
76
77
78
# File 'lib/dynamoid/associations/many_association.rb', line 70

def <<(object)
  associate(Array(object).collect(&:hash_key))

  if target_association
    Array(object).each { |obj| obj.send(target_association).associate(source.hash_key) }
  end

  object
end

#==(other) ⇒ Boolean

Is this array equal to the association’s records?

Returns:

  • (Boolean)

    true/false

Since:

  • 0.2.0



164
165
166
# File 'lib/dynamoid/associations/many_association.rb', line 164

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

#associate(hash_key) ⇒ Object



179
180
181
# File 'lib/dynamoid/associations/many_association.rb', line 179

def associate(hash_key)
  source.update_attribute(source_attribute, source_ids.merge(Array(hash_key)))
end

#create(attributes = {}) ⇒ Dynamoid::Document

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



112
113
114
# File 'lib/dynamoid/associations/many_association.rb', line 112

def create(attributes = {})
  self << target_class.create(attributes)
end

#create!(attributes = {}) ⇒ Dynamoid::Document

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



101
102
103
# File 'lib/dynamoid/associations/many_association.rb', line 101

def create!(attributes = {})
  self << target_class.create!(attributes)
end

#delete(object) ⇒ Dynamoid::Document

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
59
60
# File 'lib/dynamoid/associations/many_association.rb', line 54

def delete(object)
  disassociate(Array(object).collect(&:hash_key))
  if target_association
    Array(object).each { |obj| obj.send(target_association).disassociate(source.hash_key) }
  end
  object
end

#delete_allObject

Deletes all members of the association and removes them from the association.

Since:

  • 0.2.0



139
140
141
142
143
# File 'lib/dynamoid/associations/many_association.rb', line 139

def delete_all
  objs = target
  source.update_attribute(source_attribute, nil)
  objs.each(&:delete)
end

#destroy_allObject

Destroys all members of the association and removes them from the association.

Since:

  • 0.2.0



130
131
132
133
134
# File 'lib/dynamoid/associations/many_association.rb', line 130

def destroy_all
  objs = target
  source.update_attribute(source_attribute, nil)
  objs.each(&:destroy)
end

#disassociate(hash_key) ⇒ Object



183
184
185
# File 'lib/dynamoid/associations/many_association.rb', line 183

def disassociate(hash_key)
  source.update_attribute(source_attribute, source_ids - Array(hash_key))
end

#each(&block) ⇒ Dynamoid::Document

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



123
124
125
# File 'lib/dynamoid/associations/many_association.rb', line 123

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

#find_targetObject

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

#include?(object) ⇒ Boolean

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

#initialize(*args) ⇒ Object



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

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

#recordsObject 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

#setter(object) ⇒ Dynamoid::Document

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



88
89
90
91
92
# File 'lib/dynamoid/associations/many_association.rb', line 88

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

#where(args) ⇒ Dynamoid::Association

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



152
153
154
155
156
157
# File 'lib/dynamoid/associations/many_association.rb', line 152

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