Class: ActiveData::Model::Associations::Base
- Inherits:
-
Object
- Object
- ActiveData::Model::Associations::Base
show all
- Defined in:
- lib/active_data/model/associations/base.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(owner, reflection) ⇒ Base
Returns a new instance of Base.
8
9
10
11
12
|
# File 'lib/active_data/model/associations/base.rb', line 8
def initialize owner, reflection
@owner, @reflection = owner, reflection
@evar_loaded = owner.persisted?
reset
end
|
Instance Attribute Details
#owner ⇒ Object
Returns the value of attribute owner.
5
6
7
|
# File 'lib/active_data/model/associations/base.rb', line 5
def owner
@owner
end
|
#reflection ⇒ Object
Returns the value of attribute reflection.
5
6
7
|
# File 'lib/active_data/model/associations/base.rb', line 5
def reflection
@reflection
end
|
Instance Method Details
#apply_changes! ⇒ Object
42
43
44
|
# File 'lib/active_data/model/associations/base.rb', line 42
def apply_changes!
apply_changes or raise ActiveData::AssociationChangesNotApplied
end
|
#evar_loaded? ⇒ Boolean
19
20
21
|
# File 'lib/active_data/model/associations/base.rb', line 19
def evar_loaded?
!!@evar_loaded
end
|
#inspect ⇒ Object
55
56
57
|
# File 'lib/active_data/model/associations/base.rb', line 55
def inspect
"#<#{reflection.macro.to_s.camelize} #{target.inspect.truncate(50, omission: collection? ? '...]' : '...')}>"
end
|
#loaded! ⇒ Object
27
28
29
30
|
# File 'lib/active_data/model/associations/base.rb', line 27
def loaded!
@evar_loaded = true
@loaded = true
end
|
23
24
25
|
# File 'lib/active_data/model/associations/base.rb', line 23
def loaded?
!!@loaded
end
|
#reload ⇒ Object
37
38
39
40
|
# File 'lib/active_data/model/associations/base.rb', line 37
def reload
reset
target
end
|
#reset ⇒ Object
14
15
16
17
|
# File 'lib/active_data/model/associations/base.rb', line 14
def reset
@loaded = false
@target = nil
end
|
#target ⇒ Object
32
33
34
35
|
# File 'lib/active_data/model/associations/base.rb', line 32
def target
return @target if loaded?
self.target = load_target
end
|
#transaction(&block) ⇒ Object
46
47
48
49
50
51
52
53
|
# File 'lib/active_data/model/associations/base.rb', line 46
def transaction &block
data = Marshal.load(Marshal.dump(read_source))
block.call
rescue StandardError => e
write_source data
reload
raise e
end
|