Module: Dynomite::Associations::Association

Included in:
ManyAssociation, SingleAssociation
Defined in:
lib/dynomite/associations/association.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#loadedObject

Returns the value of attribute loaded.



7
8
9
# File 'lib/dynomite/associations/association.rb', line 7

def loaded
  @loaded
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/dynomite/associations/association.rb', line 7

def name
  @name
end

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/dynomite/associations/association.rb', line 7

def options
  @options
end

#sourceObject

Returns the value of attribute source.



7
8
9
# File 'lib/dynomite/associations/association.rb', line 7

def source
  @source
end

Instance Method Details

#coerce_to_id(object) ⇒ Object



27
28
29
# File 'lib/dynomite/associations/association.rb', line 27

def coerce_to_id(object)
  object.respond_to?(:partition_key) ? object.partition_key : object
end

#coerce_to_item(object) ⇒ Object



31
32
33
# File 'lib/dynomite/associations/association.rb', line 31

def coerce_to_item(object)
  object.is_a?(String) ? target_class.find(object) : object
end

#declaration_field_nameObject



59
60
61
# File 'lib/dynomite/associations/association.rb', line 59

def declaration_field_name
  "#{name}_ids"
end

#declaration_field_typeObject



63
64
65
# File 'lib/dynomite/associations/association.rb', line 63

def declaration_field_type
  :set
end

#find_targetObject



39
# File 'lib/dynomite/associations/association.rb', line 39

def find_target; end

#initialize(source, name, options) ⇒ Dynomite::Association

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

  • :foreign_key (Symbol)

    the name of the field for belongs_to association

Returns:

  • (Dynomite::Association)

    the actual association instance itself



20
21
22
23
24
25
# File 'lib/dynomite/associations/association.rb', line 20

def initialize(source, name, options)
  @source = source
  @name = name
  @options = options
  @loaded = false
end

#loaded?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/dynomite/associations/association.rb', line 35

def loaded?
  @loaded
end

#reader_targetObject



50
51
52
# File 'lib/dynomite/associations/association.rb', line 50

def reader_target
  self
end

#resetObject



54
55
56
57
# File 'lib/dynomite/associations/association.rb', line 54

def reset
  @target = nil
  @loaded = false
end

#targetObject



41
42
43
44
45
46
47
48
# File 'lib/dynomite/associations/association.rb', line 41

def target
  unless loaded?
    @target = find_target
    @loaded = true
  end

  @target
end