Class: DeltaCore::Context
- Inherits:
-
Object
- Object
- DeltaCore::Context
- Defined in:
- lib/delta_core/context.rb
Instance Method Summary collapse
- #build_state(model) ⇒ Object
- #calculate_delta(model) ⇒ Object
-
#initialize(config, adapter: nil) ⇒ Context
constructor
A new instance of Context.
- #reset_flags(_model) ⇒ Object
- #update_snapshot(model) ⇒ Object
- #with_delta_transaction(model) ⇒ Object
Constructor Details
#initialize(config, adapter: nil) ⇒ Context
Returns a new instance of Context.
5 6 7 8 |
# File 'lib/delta_core/context.rb', line 5 def initialize(config, adapter: nil) @config = config @adapter = adapter || Adapters::ActiveRecord.new(config) end |
Instance Method Details
#build_state(model) ⇒ Object
10 11 12 |
# File 'lib/delta_core/context.rb', line 10 def build_state(model) StateBuilder.build(model, @config) end |
#calculate_delta(model) ⇒ Object
14 15 16 17 18 |
# File 'lib/delta_core/context.rb', line 14 def calculate_delta(model) snapshot = @adapter.load_snapshot(model) current_state = build_state(model) Comparator.compare(snapshot.state, current_state, @config) end |
#reset_flags(_model) ⇒ Object
55 56 57 58 |
# File 'lib/delta_core/context.rb', line 55 def reset_flags(_model) # Extension point: clears any dirty-tracking metadata. # Default implementation is a no-op. end |
#update_snapshot(model) ⇒ Object
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/delta_core/context.rb', line 20 def update_snapshot(model) delta = calculate_delta(model) raise EmptyDeltaError, "Cannot update snapshot: delta is empty" if delta.empty? @adapter.lock_record(model) do current_state = build_state(model) serialized = Snapshot.new.serialize(current_state) @adapter.persist(model, serialized) end end |
#with_delta_transaction(model) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/delta_core/context.rb', line 31 def with_delta_transaction(model) raise ArgumentError, "Block required" unless block_given? result = nil @adapter.lock_record(model) do current_state = build_state(model) delta = Comparator.compare( @adapter.load_snapshot(model).state, current_state, @config ) raise EmptyDeltaError, "Delta is empty — nothing to transmit" if delta.empty? result = yield delta serialized = Snapshot.new.serialize(current_state) @adapter.persist(model, serialized) end result end |