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.



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

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.



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

def cached_accessor_name
  @cached_accessor_name
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#records_variable_nameObject (readonly)

Returns the value of attribute records_variable_name.



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

def records_variable_name
  @records_variable_name
end

#reflectionObject (readonly)

Returns the value of attribute reflection.



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

def reflection
  @reflection
end

Instance Method Details

#buildObject

Raises:

  • (NotImplementedError)


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

def build
  raise NotImplementedError
end

#clear(_record) ⇒ Object

Raises:

  • (NotImplementedError)


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

def clear(_record)
  raise NotImplementedError
end

#embedded?Boolean

Returns:

  • (Boolean)


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

def embedded?
  embedded_by_reference? || embedded_recursively?
end

#embedded_by_reference?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


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

def embedded_by_reference?
  raise NotImplementedError
end

#embedded_recursively?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


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

def embedded_recursively?
  raise NotImplementedError
end

#fetch(_records) ⇒ Object

Raises:

  • (NotImplementedError)


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

def fetch(_records)
  raise NotImplementedError
end

#fetch_async(_load_strategy, _records) ⇒ Object

Raises:

  • (NotImplementedError)


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

def fetch_async(_load_strategy, _records)
  raise NotImplementedError
end

#inverse_nameObject



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

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

#read(_record) ⇒ Object

Raises:

  • (NotImplementedError)


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

def read(_record)
  raise NotImplementedError
end

#validateObject



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

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)


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

def write(_record, _value)
  raise NotImplementedError
end