Class: ActiveAny::Associations::Association
- Inherits:
-
Object
- Object
- ActiveAny::Associations::Association
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
Returns the value of attribute inversed.
6
7
8
|
# File 'lib/active_any/associations/association.rb', line 6
def inversed
@inversed
end
|
Returns the value of attribute loaded.
6
7
8
|
# File 'lib/active_any/associations/association.rb', line 6
def loaded
@loaded
end
|
Returns the value of attribute owner.
6
7
8
|
# File 'lib/active_any/associations/association.rb', line 6
def owner
@owner
end
|
#reflection ⇒ Object
Returns the value of attribute reflection.
6
7
8
|
# File 'lib/active_any/associations/association.rb', line 6
def reflection
@reflection
end
|
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_scope ⇒ Object
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
75
76
77
|
# File 'lib/active_any/associations/association.rb', line 75
def find_from_target?
loaded?
end
|
#find_target ⇒ Object
79
80
81
|
# File 'lib/active_any/associations/association.rb', line 79
def find_target
raise NotImplementedError
end
|
47
48
49
|
# File 'lib/active_any/associations/association.rb', line 47
def klass
reflection.klass
end
|
#load_target ⇒ Object
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
end
|
83
84
85
86
|
# File 'lib/active_any/associations/association.rb', line 83
def loaded!
@loaded = true
@inversed = false
end
|
#loaded? ⇒ Boolean
71
72
73
|
# File 'lib/active_any/associations/association.rb', line 71
def loaded?
@loaded
end
|
16
17
18
19
20
21
22
|
# File 'lib/active_any/associations/association.rb', line 16
def reader
@proxy ||= CollectionProxy.create(owner, self)
@proxy.reset_scope
end
|
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
|
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_scope ⇒ Object
67
68
69
|
# File 'lib/active_any/associations/association.rb', line 67
def reset_scope
@association_scope = nil
end
|
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_scope ⇒ Object
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
24
25
26
|
# File 'lib/active_any/associations/association.rb', line 24
def writer(_records)
raise NotImplementedError.new, 'writer is unimplemented'
end
|