Module: Dynamoid::Associations::SingleAssociation

Defined in:
lib/dynamoid/associations/single_association.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, **kwargs, &block) ⇒ Object

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

Since:

  • 0.2.0



72
73
74
75
76
77
78
# 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

Instance Method Details

#==(other) ⇒ Boolean

Is this object equal to the association’s target?

Returns:

  • (Boolean)

    true/false

Since:

  • 0.2.0



63
64
65
# File 'lib/dynamoid/associations/single_association.rb', line 63

def ==(other)
  target == other
end

#create(attributes = {}) ⇒ Dynamoid::Document

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

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

Parameters:

  • attributes (Hash) (defaults to: {})

    attributes of a model to create

Returns:



54
55
56
# File 'lib/dynamoid/associations/single_association.rb', line 54

def create(attributes = {})
  setter(target_class.create(attributes))
end

#create!(attributes = {}) ⇒ Dynamoid::Document

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

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

If the creation fails an exception will be raised.

Parameters:

  • attributes (Hash) (defaults to: {})

    attributes of a model to create

Returns:



44
45
46
# File 'lib/dynamoid/associations/single_association.rb', line 44

def create!(attributes = {})
  setter(target_class.create!(attributes))
end

#deleteObject

Delete a model from the association.

post..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.



30
31
32
33
34
# File 'lib/dynamoid/associations/single_association.rb', line 30

def delete
  disassociate_source
  disassociate
  target
end