Class: ActiveAny::Associations::Association

Inherits:
Object
  • Object
show all
Defined in:
lib/active_any/associations/association.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner, reflection) ⇒ Association

Returns a new instance of Association.



8
9
10
11
12
13
14
# File 'lib/active_any/associations/association.rb', line 8

def initialize(owner, reflection)
  @owner = owner
  @reflection = reflection

  reset
  reset_scope
end

Instance Attribute Details

#inversedObject (readonly)

Returns the value of attribute inversed.



6
7
8
# File 'lib/active_any/associations/association.rb', line 6

def inversed
  @inversed
end

#loadedObject (readonly)

Returns the value of attribute loaded.



6
7
8
# File 'lib/active_any/associations/association.rb', line 6

def loaded
  @loaded
end

#ownerObject (readonly)

Returns the value of attribute owner.



6
7
8
# File 'lib/active_any/associations/association.rb', line 6

def owner
  @owner
end

#reflectionObject (readonly)

Returns the value of attribute reflection.



6
7
8
# File 'lib/active_any/associations/association.rb', line 6

def reflection
  @reflection
end

#targetObject

Returns the value of attribute target.



6
7
8
# File 'lib/active_any/associations/association.rb', line 6

def target
  @target
end

Instance Method Details

#association_scopeObject



36
37
38
# File 'lib/active_any/associations/association.rb', line 36

def association_scope
  @association_scope ||= klass ? AssociationScope.scope(self) : nil
end

#find_from_target?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/active_any/associations/association.rb', line 75

def find_from_target?
  loaded?
end

#find_targetObject

Raises:

  • (NotImplementedError)


79
80
81
# File 'lib/active_any/associations/association.rb', line 79

def find_target
  raise NotImplementedError
end

#klassObject



47
48
49
# File 'lib/active_any/associations/association.rb', line 47

def klass
  reflection.klass
end

#load_targetObject



51
52
53
54
55
56
57
58
59
# File 'lib/active_any/associations/association.rb', line 51

def load_target
  @target = find_target if find_target?

  loaded! unless loaded?
  target
  # TODO: implement
  # rescue ActiveRecord::RecordNotFound
  #   reset
end

#loaded!Object



83
84
85
86
# File 'lib/active_any/associations/association.rb', line 83

def loaded!
  @loaded = true
  @inversed = false
end

#loaded?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/active_any/associations/association.rb', line 71

def loaded?
  @loaded
end

#readerObject



16
17
18
19
20
21
22
# File 'lib/active_any/associations/association.rb', line 16

def reader
  # TODO: implement
  # reload if stale_target?

  @proxy ||= CollectionProxy.create(owner, self)
  @proxy.reset_scope
end

#reloadObject



40
41
42
43
44
45
# File 'lib/active_any/associations/association.rb', line 40

def reload
  reset
  reset_scope
  load_target
  self unless target.nil?
end

#resetObject



61
62
63
64
65
# File 'lib/active_any/associations/association.rb', line 61

def reset
  @loaded = false
  @stale_target = nil
  @inversed = false
end

#reset_scopeObject



67
68
69
# File 'lib/active_any/associations/association.rb', line 67

def reset_scope
  @association_scope = nil
end

#scopeObject



28
29
30
# File 'lib/active_any/associations/association.rb', line 28

def scope
  target_scope.merge!(association_scope)
end

#set_inverse_instance(record) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/active_any/associations/association.rb', line 93

def set_inverse_instance(record)
  if invertible_for?(record)
    inverse = record.association(inverse_reflection_for(record).name)
    inverse.target = owner
    inverse.inversed = true
  end
  record
end

#target_scopeObject



32
33
34
# File 'lib/active_any/associations/association.rb', line 32

def target_scope
  AssociationRelation.create(klass, self).merge!(klass.all)
end

#writer(_records) ⇒ Object

Raises:

  • (NotImplementedError.new)


24
25
26
# File 'lib/active_any/associations/association.rb', line 24

def writer(_records)
  raise NotImplementedError.new, 'writer is unimplemented'
end