Class: RaceGuard::Context::Snapshot

Inherits:
Object
  • Object
show all
Defined in:
lib/race_guard/context.rb

Overview

Immutable snapshot of #current state for the calling thread. protected_blocks is ordered outermost-first (first push_protected is index 0; innermost / most recent is last). Reserved for RaceGuard.protect (Task 2.1).

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(thread_id:, in_transaction:, protected_blocks:, current_rule:) ⇒ Snapshot

Returns a new instance of Snapshot.



279
280
281
282
283
284
285
# File 'lib/race_guard/context.rb', line 279

def initialize(thread_id:, in_transaction:, protected_blocks:, current_rule:)
  @thread_id = thread_id
  @in_transaction = in_transaction
  @protected_blocks = protected_blocks
  @current_rule = current_rule
  freeze
end

Instance Attribute Details

#current_ruleObject (readonly)

Returns the value of attribute current_rule.



266
267
268
# File 'lib/race_guard/context.rb', line 266

def current_rule
  @current_rule
end

#in_transactionObject (readonly) Also known as: in_transaction?

Returns the value of attribute in_transaction.



266
267
268
# File 'lib/race_guard/context.rb', line 266

def in_transaction
  @in_transaction
end

#protected_blocksObject (readonly)

Returns the value of attribute protected_blocks.



266
267
268
# File 'lib/race_guard/context.rb', line 266

def protected_blocks
  @protected_blocks
end

#thread_idObject (readonly)

Returns the value of attribute thread_id.



266
267
268
# File 'lib/race_guard/context.rb', line 266

def thread_id
  @thread_id
end

Class Method Details

.build(store) ⇒ Object



270
271
272
273
274
275
276
277
# File 'lib/race_guard/context.rb', line 270

def self.build(store)
  new(
    thread_id: Thread.current.object_id,
    in_transaction: store.transaction_depth.positive?,
    protected_blocks: store.protected_blocks.dup.freeze,
    current_rule: nil
  )
end

Instance Method Details

#to_hObject



287
288
289
290
291
292
293
294
# File 'lib/race_guard/context.rb', line 287

def to_h
  {
    'current_rule' => current_rule,
    'in_transaction' => in_transaction,
    'protected_blocks' => protected_blocks.map(&:to_s),
    'thread_id' => thread_id
  }
end