Class: ChronoForge::Executor::Context

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

Defined Under Namespace

Classes: ValidationError

Constant Summary collapse

ALLOWED_TYPES =
[
  String,
  Integer,
  Float,
  TrueClass,
  FalseClass,
  NilClass,
  Hash,
  Array
]
MAX_VALUE_BYTESIZE =

Maximum serialized byte size of a single context value. Applies to the variable-length types (String, Hash, Array); scalars are unbounded in practice. Measured in bytes (not characters) since that is what is actually stored and what matters for write/storage cost.

Context is meant to hold small working state (ids, flags, timestamps, small structures) — not documents or payloads, which belong in their own storage and can be referenced from context by id. 16 KB per value is already generous for that (hundreds of ids / dozens of records).

16.kilobytes

Instance Method Summary collapse

Constructor Details

#initialize(workflow) ⇒ Context

Returns a new instance of Context.



28
29
30
31
32
# File 'lib/chrono_forge/executor/context.rb', line 28

def initialize(workflow)
  @workflow = workflow
  @context = workflow.context || {}
  @dirty = false
end

Instance Method Details

#[](key) ⇒ Object



38
39
40
# File 'lib/chrono_forge/executor/context.rb', line 38

def [](key)
  get_value(key)
end

#[]=(key, value) ⇒ Object



34
35
36
# File 'lib/chrono_forge/executor/context.rb', line 34

def []=(key, value)
  set_value(key, value)
end

#fetch(key, default = nil) ⇒ Object

Fetches a value from the context Returns the value if the key exists, otherwise returns the default value



44
45
46
# File 'lib/chrono_forge/executor/context.rb', line 44

def fetch(key, default = nil)
  key?(key) ? get_value(key) : default
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/chrono_forge/executor/context.rb', line 86

def key?(key)
  @context.key?(key.to_s)
end

#merge(hash) ⇒ Object Also known as: set_multiple

Sets multiple values in the context at once from a hash. The merge is atomic: every value is validated before any is written, so a single invalid value raises and leaves the context untouched. Returns self for chaining.



58
59
60
61
62
# File 'lib/chrono_forge/executor/context.rb', line 58

def merge(hash)
  hash.each_value { |value| validate_value!(value) }
  hash.each { |key, value| set_value(key, value) }
  self
end

#merge_once(hash) ⇒ Object Also known as: set_multiple_once

Like #merge, but only sets keys that don't already exist; present keys are skipped entirely (their values are never validated), matching #set_once semantics. The applied keys are written atomically: an invalid value among the new keys raises and writes nothing. Returns self.



69
70
71
72
73
74
# File 'lib/chrono_forge/executor/context.rb', line 69

def merge_once(hash)
  new_pairs = hash.reject { |key, _| key?(key) }
  new_pairs.each_value { |value| validate_value!(value) }
  new_pairs.each { |key, value| set_value(key, value) }
  self
end

#save!Object



90
91
92
93
94
95
# File 'lib/chrono_forge/executor/context.rb', line 90

def save!
  return unless @dirty

  @workflow.update_column(:context, @context)
  @dirty = false
end

#set(key, value) ⇒ Object

Sets a value in the context Alias for the []= method



50
51
52
# File 'lib/chrono_forge/executor/context.rb', line 50

def set(key, value)
  set_value(key, value)
end

#set_once(key, value) ⇒ Object

Sets a value in the context only if the key doesn't already exist Returns true if the value was set, false otherwise



79
80
81
82
83
84
# File 'lib/chrono_forge/executor/context.rb', line 79

def set_once(key, value)
  return false if key?(key)

  set_value(key, value)
  true
end