Class: ActiveData::Model::Associations::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/active_data/model/associations/base.rb

Direct Known Subclasses

EmbedsMany, EmbedsOne, ReferencesMany, ReferencesOne

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

#ownerObject

Returns the value of attribute owner.



5
6
7
# File 'lib/active_data/model/associations/base.rb', line 5

def owner
  @owner
end

#reflectionObject

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

Returns:



19
20
21
# File 'lib/active_data/model/associations/base.rb', line 19

def evar_loaded?
  !!@evar_loaded
end

#inspectObject



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

#loaded?Boolean

Returns:



23
24
25
# File 'lib/active_data/model/associations/base.rb', line 23

def loaded?
  !!@loaded
end

#reloadObject



37
38
39
40
# File 'lib/active_data/model/associations/base.rb', line 37

def reload
  reset
  target
end

#resetObject



14
15
16
17
# File 'lib/active_data/model/associations/base.rb', line 14

def reset
  @loaded = false
  @target = nil
end

#targetObject



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