Class: IdentityCache::Cached::Association

Inherits:
Object
  • Object
show all
Includes:
EmbeddedFetching
Defined in:
lib/identity_cache/cached/association.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, reflection:) ⇒ Association

Returns a new instance of Association.



8
9
10
11
12
13
# File 'lib/identity_cache/cached/association.rb', line 8

def initialize(name, reflection:)
  @name = name
  @reflection = reflection
  @cached_accessor_name = :"fetch_#{name}"
  @records_variable_name = :"@cached_#{name}"
end

Instance Attribute Details

#cached_accessor_nameObject (readonly)

Returns the value of attribute cached_accessor_name.



15
16
17
# File 'lib/identity_cache/cached/association.rb', line 15

def cached_accessor_name
  @cached_accessor_name
end

#nameObject (readonly)

Returns the value of attribute name.



15
16
17
# File 'lib/identity_cache/cached/association.rb', line 15

def name
  @name
end

#records_variable_nameObject (readonly)

Returns the value of attribute records_variable_name.



15
16
17
# File 'lib/identity_cache/cached/association.rb', line 15

def records_variable_name
  @records_variable_name
end

#reflectionObject (readonly)

Returns the value of attribute reflection.



15
16
17
# File 'lib/identity_cache/cached/association.rb', line 15

def reflection
  @reflection
end

Instance Method Details

#buildObject

Raises:

  • (NotImplementedError)


17
18
19
# File 'lib/identity_cache/cached/association.rb', line 17

def build
  raise NotImplementedError
end

#clear(_record) ⇒ Object

Raises:

  • (NotImplementedError)


29
30
31
# File 'lib/identity_cache/cached/association.rb', line 29

def clear(_record)
  raise NotImplementedError
end

#embedded?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/identity_cache/cached/association.rb', line 41

def embedded?
  embedded_by_reference? || embedded_recursively?
end

#embedded_by_reference?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


45
46
47
# File 'lib/identity_cache/cached/association.rb', line 45

def embedded_by_reference?
  raise NotImplementedError
end

#embedded_recursively?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


49
50
51
# File 'lib/identity_cache/cached/association.rb', line 49

def embedded_recursively?
  raise NotImplementedError
end

#fetch(_records) ⇒ Object

Raises:

  • (NotImplementedError)


33
34
35
# File 'lib/identity_cache/cached/association.rb', line 33

def fetch(_records)
  raise NotImplementedError
end

#fetch_async(_load_strategy, _records) ⇒ Object

Raises:

  • (NotImplementedError)


37
38
39
# File 'lib/identity_cache/cached/association.rb', line 37

def fetch_async(_load_strategy, _records)
  raise NotImplementedError
end

#inverse_nameObject



53
54
55
# File 'lib/identity_cache/cached/association.rb', line 53

def inverse_name
  @inverse_name ||= reflection.inverse_of&.name || reflection.active_record.name.underscore
end

#read(_record) ⇒ Object

Raises:

  • (NotImplementedError)


21
22
23
# File 'lib/identity_cache/cached/association.rb', line 21

def read(_record)
  raise NotImplementedError
end

#validateObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/identity_cache/cached/association.rb', line 57

def validate
  parent_class = reflection.active_record
  child_class  = reflection.klass

  unless child_class < IdentityCache::WithoutPrimaryIndex
    if embedded_recursively?
      raise UnsupportedAssociationError, <<~MSG.squish
        cached association #{parent_class}\##{reflection.name} requires
        associated class #{child_class} to include IdentityCache
        or IdentityCache::WithoutPrimaryIndex
      MSG
    else
      raise UnsupportedAssociationError, <<~MSG.squish
        cached association #{parent_class}\##{reflection.name} requires
        associated class #{child_class} to include IdentityCache
      MSG
    end
  end

  unless child_class.reflect_on_association(inverse_name)
    raise InverseAssociationError, <<~MSG
      Inverse name for association #{parent_class}\##{reflection.name} could not be determined.
      Use the :inverse_of option on the Active Record association to specify the inverse association name.
    MSG
  end
end

#write(_record, _value) ⇒ Object

Raises:

  • (NotImplementedError)


25
26
27
# File 'lib/identity_cache/cached/association.rb', line 25

def write(_record, _value)
  raise NotImplementedError
end