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 63
def ==(other)
  target == other
end
associate(hash_key) click to toggle source

@private

# File lib/dynamoid/associations/single_association.rb, line 111
def associate(hash_key)
  disassociate_source
  source.update_attribute(source_attribute, Set[hash_key])
end
create(attributes = {}) click to toggle source

Create a new instance of the target class, persist it and associate.

post.logo.create(hight: 50, width: 90)

@param attributes [Hash] attributes of a model to create @return [Dynamoid::Document] created model

# File lib/dynamoid/associations/single_association.rb, line 54
def create(attributes = {})
  setter(target_class.create(attributes))
end
create!(attributes = {}) click to toggle source

Create a new instance of the target class, persist it and associate.

post.logo.create!(hight: 50, width: 90)

If the creation fails an exception will be raised.

@param attributes [Hash] attributes of a model to create @return [Dynamoid::Document] created model

# File lib/dynamoid/associations/single_association.rb, line 44
def create!(attributes = {})
  setter(target_class.create!(attributes))
end
delete() click to toggle source

Delete a model from the association.

post.logo.delete # => nil

Saves both models immediately - a source model and a target one so any unsaved changes will be saved. Doesn’t delete an associated model from DynamoDB.

# File lib/dynamoid/associations/single_association.rb, line 30
def delete
  disassociate_source
  disassociate
  target
end
disassociate(_hash_key = nil) click to toggle source

@private

# File lib/dynamoid/associations/single_association.rb, line 117
def disassociate(_hash_key = nil)
  source.update_attribute(source_attribute, nil)
end
empty?() click to toggle source

@private

# File lib/dynamoid/associations/single_association.rb, line 104
def empty?
  # This is needed to that ActiveSupport's #blank? and #present?
  # methods work as expected for SingleAssociations.
  target.nil?
end
method_missing(method, *args, &block) click to toggle source

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

@private @since 0.2.0

Calls superclass method
# File lib/dynamoid/associations/single_association.rb, line 72
def method_missing(method, *args, &block)
  if target.respond_to?(method)
    target.send(method, *args, &block)
  else
    super
  end
end
nil?() click to toggle source

@private

# File lib/dynamoid/associations/single_association.rb, line 99
def nil?
  target.nil?
end
respond_to_missing?(method_name, include_private = false) click to toggle source

@private

Calls superclass method
# File lib/dynamoid/associations/single_association.rb, line 94
def respond_to_missing?(method_name, include_private = false)
  target.respond_to?(method_name, include_private) || super
end
setter(object) click to toggle source

@private

# File lib/dynamoid/associations/single_association.rb, line 11
def setter(object)
  if object.nil?
    delete
    return
  end

  associate(object.hash_key)
  self.target = object
  object.send(target_association).associate(source.hash_key) 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 128
def find_target
  return if source_ids.empty?

  target_class.find(source_ids.first, raise_error: false)
end
target=(object) click to toggle source
# File lib/dynamoid/associations/single_association.rb, line 134
def target=(object)
  @target = object
  @loaded = true
end