Module: Dynamoid::Associations::SingleAssociation
- Includes:
- Association
- Defined in:
- lib/dynamoid/associations/single_association.rb
Instance Attribute Summary
Attributes included from Association
#loaded, #name, #options, #source
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Is this object equal to the association’s target?.
- #associate(hash_key) ⇒ Object
-
#create(attributes = {}) ⇒ Dynamoid::Document
Create a new instance of the target class, persist it and associate.
-
#create!(attributes = {}) ⇒ Dynamoid::Document
Create a new instance of the target class, persist it and associate.
-
#delete ⇒ Object
Delete a model from the association.
- #disassociate(_hash_key = nil) ⇒ Object
- #empty? ⇒ Boolean
-
#method_missing(method, *args, **kwargs, &block) ⇒ Object
Delegate methods we don’t find directly to the target.
- #nil? ⇒ Boolean
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
- #setter(object) ⇒ Object
Methods included from Association
#declaration_field_name, #declaration_field_type, #disassociate_source, #initialize, #loaded?, #reset, #target
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.
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?
63 64 65 |
# File 'lib/dynamoid/associations/single_association.rb', line 63 def ==(other) target == other end |
#associate(hash_key) ⇒ Object
111 112 113 114 |
# 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 = {}) ⇒ Dynamoid::Document
Create a new instance of the target class, persist it and associate.
post.logo.create(hight: 50, width: 90)
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.logo.create!(hight: 50, width: 90)
If the creation fails an exception will be raised.
44 45 46 |
# File 'lib/dynamoid/associations/single_association.rb', line 44 def create!(attributes = {}) setter(target_class.create!(attributes)) end |
#delete ⇒ Object
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.
30 31 32 33 34 |
# File 'lib/dynamoid/associations/single_association.rb', line 30 def delete disassociate_source disassociate target end |
#disassociate(_hash_key = nil) ⇒ Object
117 118 119 |
# File 'lib/dynamoid/associations/single_association.rb', line 117 def disassociate(_hash_key = nil) source.update_attribute(source_attribute, nil) end |
#empty? ⇒ Boolean
104 105 106 107 108 |
# 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 |
#nil? ⇒ Boolean
99 100 101 |
# File 'lib/dynamoid/associations/single_association.rb', line 99 def nil? target.nil? end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
94 95 96 |
# 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) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 |
# 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 |