Class: ChronoForge::Executor::Context
- Inherits:
-
Object
- Object
- ChronoForge::Executor::Context
- 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
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
-
#fetch(key, default = nil) ⇒ Object
Fetches a value from the context Returns the value if the key exists, otherwise returns the default value.
-
#initialize(workflow) ⇒ Context
constructor
A new instance of Context.
- #key?(key) ⇒ Boolean
- #save! ⇒ Object
-
#set(key, value) ⇒ Object
Sets a value in the context Alias for the []= method.
-
#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.
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
63 64 65 |
# File 'lib/chrono_forge/executor/context.rb', line 63 def key?(key) @context.key?(key.to_s) end |
#save! ⇒ Object
67 68 69 70 71 72 |
# File 'lib/chrono_forge/executor/context.rb', line 67 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
56 57 58 59 60 61 |
# File 'lib/chrono_forge/executor/context.rb', line 56 def set_once(key, value) return false if key?(key) set_value(key, value) true end |