Class: MongoTrails::CallbackPropagation

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo_trails/callback_propagation.rb

Constant Summary collapse

PRESERVED_ATTRIBUTES =
%w[_id item_type item_id event object object_changes integer_id].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, version, bang:) ⇒ CallbackPropagation

Returns a new instance of CallbackPropagation.



9
10
11
12
13
# File 'lib/mongo_trails/callback_propagation.rb', line 9

def initialize(source, version, bang:)
  @source = source
  @version = version
  @bang = bang
end

Instance Attribute Details

#versionObject (readonly)

Returns the value of attribute version.



7
8
9
# File 'lib/mongo_trails/callback_propagation.rb', line 7

def version
  @version
end

Instance Method Details

#clearObject



42
43
44
45
# File 'lib/mongo_trails/callback_propagation.rb', line 42

def clear
  propagations = @source.instance_variable_get(:@mongo_trails_callback_propagations)
  propagations&.delete(version.event) if propagations&.fetch(version.event, nil).equal?(self)
end

#merge!(new_version, bang:) ⇒ Object



15
16
17
18
19
20
# File 'lib/mongo_trails/callback_propagation.rb', line 15

def merge!(new_version, bang:)
  merge_changes!(new_version.object_changes)
  copy_latest_metadata!(new_version)
  @bang ||= bang
  self
end

#persistObject



36
37
38
39
40
# File 'lib/mongo_trails/callback_propagation.rb', line 36

def persist
  version.send(:persist_version, version, bang: @bang)
ensure
  clear
end

#restore!(snapshot) ⇒ Object



30
31
32
33
34
# File 'lib/mongo_trails/callback_propagation.rb', line 30

def restore!(snapshot)
  version.object_changes = snapshot.fetch(:object_changes)
  snapshot.fetch(:metadata).each { |attribute, value| version[attribute] = value }
  @bang = snapshot.fetch(:bang)
end

#snapshotObject



22
23
24
25
26
27
28
# File 'lib/mongo_trails/callback_propagation.rb', line 22

def snapshot
  {
    object_changes: version.object_changes.to_h.deep_dup,
    metadata: .deep_dup,
    bang: @bang
  }
end