Module: Dynamoid::Associations::Association

Includes:
Enumerable
Included in:
BelongsTo, HasAndBelongsToMany, HasMany, HasOne
Defined in:
lib/dynamoid/associations/association.rb

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

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

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/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.

Parameters:

  • attribute (Hash)

    hash for the new object

Returns:

Since:

  • 0.2.0



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.

Parameters:

  • attribute (Hash)

    hash for the new object

Returns:

Since:

  • 0.2.0



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.

Parameters:

  • object (Dynamoid::Document)

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

Returns:

Since:

  • 0.2.0



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.

Since:

  • 0.2.0



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.

Since:

  • 0.2.0



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.

Parameters:

  • attribute (Hash)

    hash for the new object

Returns:

Since:

  • 0.2.0



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.

Returns:

  • (Boolean)


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.

Parameters:

  • source (Class)

    the source record of the association; that is, the record that you already have

  • name (Symbol)

    the name of the association

  • options (Hash)

    optional parameters for the association

Options Hash (options):

  • :class (Class)

    the target class of the association; that is, the class to which the association objects belong

  • :class_name (Symbol)

    the name of the target class of the association; only this or Class is necessary

  • :inverse_of (Symbol)

    the name of the association on the target class

Since:

  • 0.2.0



27
28
29
30
31
32
# File 'lib/dynamoid/associations/association.rb', line 27

def initialize(source, name, options)
  @name = name
  @options = options
  @source = source
  @query = {}
end

- (Object) records Also known as: all

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



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.

Parameters:

  • object (Dynamoid::Document)

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

Returns:

Since:

  • 0.2.0



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.

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



133
134
135
136
# File 'lib/dynamoid/associations/association.rb', line 133

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