module Dynamoid::Associations::SingleAssociation

Public Instance Methods

==(other) click to toggle source

Is this object equal to the association's target?

@return [Boolean] true/false

@since 0.2.0

# File lib/dynamoid/associations/single_association.rb, line 37
def ==(other)
  target == other
end
create(attributes = {}) click to toggle source
# File lib/dynamoid/associations/single_association.rb, line 27
def create(attributes = {})
  setter(target_class.create!(attributes))
end
create!(attributes = {}) click to toggle source
# File lib/dynamoid/associations/single_association.rb, line 23
def create!(attributes = {})
  setter(target_class.create!(attributes))
end
delete() click to toggle source
# File lib/dynamoid/associations/single_association.rb, line 17
def delete
  source.update_attribute(source_attribute, nil)
  self.send(:disassociate_target, target) if target && target_association
  target
end
method_missing(method, *args) click to toggle source

Delegate methods we don't find directly to the target.

@since 0.2.0

Calls superclass method
# File lib/dynamoid/associations/single_association.rb, line 44
def method_missing(method, *args)
  if target.respond_to?(method)
    target.send(method, *args)
  else
    super
  end
end
nil?() click to toggle source
# File lib/dynamoid/associations/single_association.rb, line 52
def nil?
  target.nil?
end
setter(object) click to toggle source
# File lib/dynamoid/associations/single_association.rb, line 10
def setter(object)
  delete
  source.update_attribute(source_attribute, Set[object.id])
  self.send(:associate_target, object) if target_association
  object
end

Private Instance Methods

find_target() click to toggle source

Find the target of the has_one association.

@return [Dynamoid::Document] the found target (or nil if nothing)

@since 0.2.0

# File lib/dynamoid/associations/single_association.rb, line 63
def find_target
  return if source_ids.empty?
  target_class.find(source_ids.first)
end