module Dynamoid::Associations::Association

@private

Attributes

loaded[RW]
name[RW]
options[RW]
source[RW]

Public Class Methods

new(source, name, options) click to toggle source

Create a new association.

@param [Class] source the source record of the association; that is, the record that you already have @param [Symbol] name the name of the association @param [Hash] options optional parameters for the association @option options [Class] :class the target class of the association; that is, the class to which the association objects belong @option options [Symbol] :class_name the name of the target class of the association; only this or Class is necessary @option options [Symbol] :inverse_of the name of the association on the target class @option options [Symbol] :foreign_key the name of the field for belongs_to association

@return [Dynamoid::Association] the actual association instance itself

@since 0.2.0

# File lib/dynamoid/associations/association.rb, line 26
def initialize(source, name, options)
  @name = name
  @options = options
  @source = source
  @loaded = false
end

Public Instance Methods

declaration_field_name() click to toggle source
# File lib/dynamoid/associations/association.rb, line 53
def declaration_field_name
  "#{name}_ids"
end
declaration_field_type() click to toggle source
# File lib/dynamoid/associations/association.rb, line 57
def declaration_field_type
  :set
end
disassociate_source() click to toggle source
# File lib/dynamoid/associations/association.rb, line 61
def disassociate_source
  Array(target).each do |target_entry|
    target_entry.send(target_association).disassociate(source.hash_key) if target_entry && target_association
  end
end
find_target() click to toggle source
# File lib/dynamoid/associations/association.rb, line 37
def find_target; end
loaded?() click to toggle source
# File lib/dynamoid/associations/association.rb, line 33
def loaded?
  @loaded
end
reset() click to toggle source
# File lib/dynamoid/associations/association.rb, line 48
def reset
  @target = nil
  @loaded = false
end
target() click to toggle source
# File lib/dynamoid/associations/association.rb, line 39
def target
  unless loaded?
    @target = find_target
    @loaded = true
  end

  @target
end

Private Instance Methods

build(attributes = {}) click to toggle source

Create a new instance of the target class without trying to add it to the association. This creates a base, that caller can update before setting or adding it.

@param attributes [Hash] attribute values for the new object

@return [Dynamoid::Document] the newly-created object

@since 1.1.1

# File lib/dynamoid/associations/association.rb, line 132
def build(attributes = {})
  target_class.build(attributes)
end
source_attribute() click to toggle source

The source’s association attribute: the name of the association with _ids afterwards, like “users_ids”.

@since 0.2.0

# File lib/dynamoid/associations/association.rb, line 113
def source_attribute
  declaration_field_name.to_sym
end
source_class() click to toggle source

The ids in the target association.

@since 0.2.0

# File lib/dynamoid/associations/association.rb, line 106
def source_class
  source.class
end
source_ids() click to toggle source

The ids in the source association.

@since 0.2.0

# File lib/dynamoid/associations/association.rb, line 120
def source_ids
  # handle case when we store scalar value instead of collection (when foreign_key option is specified)
  Array(source.send(source_attribute)).compact.to_set || Set.new
end
target_attribute() click to toggle source

The target attribute: that is, the attribute on each object of the association that should reference the source.

@since 0.2.0

# File lib/dynamoid/associations/association.rb, line 86
def target_attribute
  # In simple case it's equivalent to
  # "#{target_association}_ids".to_sym if target_association
  if target_association
    target_options = target_class.associations[target_association]
    assoc = Dynamoid::Associations.const_get(target_options[:type].to_s.camelcase).new(nil, target_association, target_options)
    assoc.send(:source_attribute)
  end
end
target_class() click to toggle source

The target class, either inferred through the association’s name or specified in options.

@since 0.2.0

# File lib/dynamoid/associations/association.rb, line 79
def target_class
  options[:class] || target_class_name.constantize
end
target_class_name() click to toggle source

The target class name, either inferred through the association’s name or specified in options.

@since 0.2.0

# File lib/dynamoid/associations/association.rb, line 72
def target_class_name
  options[:class_name] || name.to_s.classify
end
target_ids() click to toggle source

The ids in the target association.

@since 0.2.0

# File lib/dynamoid/associations/association.rb, line 99
def target_ids
  target.send(target_attribute) || Set.new
end