Class: Stannum::Associations::One
- Inherits:
-
Stannum::Association
- Object
- Stannum::Association
- Stannum::Associations::One
- Defined in:
- lib/stannum/associations/one.rb
Overview
Data object representing a singular association.
Instance Attribute Summary
Attributes inherited from Stannum::Association
Instance Method Summary collapse
-
#add_value(entity, value, update_inverse: true) ⇒ void
private
Adds the given value to the association for the entity.
-
#clear_value(entity, update_inverse: true) ⇒ void
private
Removes the value of the association for the entity.
-
#foreign_key? ⇒ Boolean
True if the association has a foreign key; otherwise false.
-
#foreign_key_name ⇒ String?
The name of the foreign key, if any.
-
#foreign_key_type ⇒ Class, ...
The type of the foreign key, if any.
-
#get_value(entity) ⇒ Object
private
Retrieves the value of the association for the entity.
-
#one? ⇒ true
True if the association is a singular association; otherwise false.
-
#remove_value(entity, value, update_inverse: true) ⇒ void
private
Removes the given value from the association for the entity.
-
#set_value(entity, value, update_inverse: true) ⇒ void
private
Replaces the association for the entity with the given value.
Methods inherited from Stannum::Association
#entity_class_name, #initialize, #inverse?, #inverse_name, #many?, #reader_name, #resolved_inverse, #resolved_type, #writer_name
Constructor Details
This class inherits a constructor from Stannum::Association
Instance Method Details
#add_value(entity, value, update_inverse: true) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Adds the given value to the association for the entity.
9 10 11 |
# File 'lib/stannum/associations/one.rb', line 9 def add_value(entity, value, update_inverse: true) set_value(entity, value, update_inverse:) end |
#clear_value(entity, update_inverse: true) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Removes the value of the association for the entity.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/stannum/associations/one.rb', line 14 def clear_value(entity, update_inverse: true) previous_value = entity.read_association(name, safe: false) if update_inverse && previous_value && inverse? resolved_inverse.remove_value( previous_value, entity, update_inverse: false ) end entity.write_attribute(foreign_key_name, nil, safe: false) if foreign_key? entity.write_association(name, nil, safe: false) end |
#foreign_key? ⇒ Boolean
Returns true if the association has a foreign key; otherwise false.
32 33 34 35 36 37 38 39 40 |
# File 'lib/stannum/associations/one.rb', line 32 def foreign_key? return @has_foreign_key unless @has_foreign_key.nil? value = [:foreign_key_name] return @has_foreign_key = false if value.nil? || value == false @has_foreign_key = true end |
#foreign_key_name ⇒ String?
Returns the name of the foreign key, if any.
43 44 45 46 47 |
# File 'lib/stannum/associations/one.rb', line 43 def foreign_key_name return nil unless foreign_key? @foreign_key_name ||= [:foreign_key_name].to_s end |
#foreign_key_type ⇒ Class, ...
Returns the type of the foreign key, if any.
51 52 53 54 55 |
# File 'lib/stannum/associations/one.rb', line 51 def foreign_key_type return nil unless foreign_key? @foreign_key_type ||= [:foreign_key_type] end |
#get_value(entity) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Retrieves the value of the association for the entity.
58 59 60 |
# File 'lib/stannum/associations/one.rb', line 58 def get_value(entity) entity.read_association(name, safe: false) end |
#one? ⇒ true
Returns true if the association is a singular association; otherwise false.
64 65 66 |
# File 'lib/stannum/associations/one.rb', line 64 def one? true end |
#remove_value(entity, value, update_inverse: true) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Removes the given value from the association for the entity.
69 70 71 72 73 74 75 |
# File 'lib/stannum/associations/one.rb', line 69 def remove_value(entity, value, update_inverse: true) previous_value = entity.read_association(name, safe: false) return unless matching_value?(value, previous_value) clear_value(entity, update_inverse:) end |
#set_value(entity, value, update_inverse: true) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Replaces the association for the entity with the given value.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/stannum/associations/one.rb', line 78 def set_value(entity, value, update_inverse: true) # rubocop:disable Metrics/MethodLength if foreign_key? entity.write_attribute( foreign_key_name, value&.primary_key, safe: false ) end entity.write_association(name, value, safe: false) return unless update_inverse && value && inverse? previous_inverse = resolved_inverse.get_value(value) resolved_inverse.remove_value(value, previous_inverse) if previous_inverse resolved_inverse.add_value(value, entity, update_inverse: false) end |