Class: Stannum::Associations::One

Inherits:
Stannum::Association show all
Defined in:
lib/stannum/associations/one.rb

Overview

Data object representing a singular association.

Instance Attribute Summary

Attributes inherited from Stannum::Association

#name, #options, #type

Instance Method Summary collapse

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.

Parameters:

  • entity (Stannum::Entity)

    the entity to update.

  • value (Object)

    the new value for the association.

  • update_inverse (Boolean) (defaults to: true)

    if true, updates the inverse association (if any). Defaults to false.



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.

Parameters:



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.

Returns:

  • (Boolean)

    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 = options[:foreign_key_name]

  return @has_foreign_key = false if value.nil? || value == false

  @has_foreign_key = true
end

#foreign_key_nameString?

Returns the name of the foreign key, if any.

Returns:

  • (String?)

    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 ||= options[:foreign_key_name].to_s
end

#foreign_key_typeClass, ...

Returns the type of the foreign key, if any.

Returns:



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 ||= options[: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.

Parameters:

Returns:

  • (Object)

    the value of the association.



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.

Returns:

  • (true)

    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.

Parameters:

  • entity (Stannum::Entity)

    the entity to update.

  • value (Stannum::Entity)

    the association value to remove.

  • update_inverse (Boolean) (defaults to: true)

    if true, updates the inverse association (if any). Defaults to false.



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.

Parameters:

  • entity (Stannum::Entity)

    the entity to update.

  • value (Object)

    the new value for the association.

  • update_inverse (Boolean) (defaults to: true)

    if true, updates the inverse association (if any). Defaults to false.



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