module Dynamoid::Associations::ClassMethods

Public Instance Methods

belongs_to(name, options = {}) click to toggle source

create a belongs_to association for this document.

@param [Symbol] name the name of the association @param [Hash] options options to pass to the association constructor @option options [Class] :class the target class of the has_one association; that is, the has_many or has_one class @option options [Symbol] :class_name the name of the target class of the association; that is, the name of the has_many or has_one class @option options [Symbol] :inverse_of the name of the association on the target class; that is, if the class has a has_many or has_one association, the name of that association

@since 0.2.0

# File lib/dynamoid/associations.rb, line 64
def belongs_to(name, options = {})
  association(:belongs_to, name, options)
end
has_and_belongs_to_many(name, options = {}) click to toggle source

create a has_and_belongs_to_many association for this document.

@param [Symbol] name the name of the association @param [Hash] options options to pass to the association constructor @option options [Class] :class the target class of the has_and_belongs_to_many association; that is, the belongs_to class @option options [Symbol] :class_name the name of the target class of the association; that is, the name of the belongs_to class @option options [Symbol] :inverse_of the name of the association on the target class; that is, if the class has a belongs_to association, the name of that association

@since 0.2.0

# File lib/dynamoid/associations.rb, line 77
def has_and_belongs_to_many(name, options = {})
  association(:has_and_belongs_to_many, name, options)
end
has_many(name, options = {}) click to toggle source

create a has_many association for this document.

@param [Symbol] name the name of the association @param [Hash] options options to pass to the association constructor @option options [Class] :class the target class of the has_many association; that is, the belongs_to class @option options [Symbol] :class_name the name of the target class of the association; that is, the name of the belongs_to class @option options [Symbol] :inverse_of the name of the association on the target class; that is, if the class has a belongs_to association, the name of that association

@since 0.2.0

# File lib/dynamoid/associations.rb, line 38
def has_many(name, options = {})
  association(:has_many, name, options)
end
has_one(name, options = {}) click to toggle source

create a has_one association for this document.

@param [Symbol] name the name of the association @param [Hash] options options to pass to the association constructor @option options [Class] :class the target class of the has_one association; that is, the belongs_to class @option options [Symbol] :class_name the name of the target class of the association; that is, the name of the belongs_to class @option options [Symbol] :inverse_of the name of the association on the target class; that is, if the class has a belongs_to association, the name of that association

@since 0.2.0

# File lib/dynamoid/associations.rb, line 51
def has_one(name, options = {})
  association(:has_one, name, options)
end

Private Instance Methods

association(type, name, options = {}) click to toggle source

create getters and setters for an association.

@param [Symbol] symbol the type (:has_one, :has_many, :has_and_belongs_to_many, :belongs_to) of the association @param [Symbol] name the name of the association @param [Hash] options options to pass to the association constructor; see above for all valid options

@since 0.2.0

# File lib/dynamoid/associations.rb, line 90
def association(type, name, options = {})
  field "#{name}_ids".to_sym, :set
  self.associations[name] = options.merge(:type => type)
  define_method(name) do
    @associations[:"#{name}_ids"] ||= Dynamoid::Associations.const_get(type.to_s.camelcase).new(self, name, options)
  end
  define_method("#{name}=".to_sym) do |objects|
    @associations[:"#{name}_ids"] ||= Dynamoid::Associations.const_get(type.to_s.camelcase).new(self, name, options)
    @associations[:"#{name}_ids"].setter(objects)
  end
end