Class: CMDx::Context
- Inherits:
-
Object
- Object
- CMDx::Context
- Extended by:
- Forwardable
- Defined in:
- lib/cmdx/context.rb
Overview
A hash-like context object that provides a flexible way to store and access key-value pairs during task execution. Keys are automatically converted to symbols for consistency.
The Context class extends Forwardable to delegate common hash methods and provides additional convenience methods for working with context data.
Instance Attribute Summary collapse
-
#table ⇒ Hash{Symbol => Object}
(also: #to_h)
readonly
Returns the internal hash storing context data.
Class Method Summary collapse
-
.build(context = {}) ⇒ Context
Builds a Context instance, reusing existing unfrozen contexts when possible.
Instance Method Summary collapse
-
#[](key) ⇒ Object?
Retrieves a value from the context by key.
-
#clear! ⇒ Context
(also: #clear)
Clears all key-value pairs from the context.
-
#delete!(key) {|key| ... } ⇒ Object?
(also: #delete)
Deletes a key-value pair from the context.
-
#dig(key, *keys) ⇒ Object?
Digs into nested structures using the given keys.
-
#eql?(other) ⇒ Boolean
(also: #==)
Compares this context with another object for equality.
-
#fetch(key) {|key| ... } ⇒ Object
Fetches a value from the context by key, with optional default value.
-
#fetch_or_store(key, value = nil) {|key| ... } ⇒ Object
Fetches a value from the context by key, or stores and returns a default value if not found.
-
#initialize(args = {}) ⇒ Context
constructor
Creates a new Context instance from the given arguments.
-
#key?(key) ⇒ Boolean
Checks if the context contains a specific key.
-
#merge!(args = {}) ⇒ Context
(also: #merge)
Merges the given arguments into the current context, modifying it in place.
-
#store(key, value) ⇒ Object
(also: #[]=)
Stores a key-value pair in the context.
-
#to_s ⇒ String
Converts the context to a string representation.
Constructor Details
#initialize(args = {}) ⇒ Context
Creates a new Context instance from the given arguments.
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/cmdx/context.rb', line 41 def initialize(args = {}) @table = if args.respond_to?(:to_hash) args.to_hash elsif args.respond_to?(:to_h) args.to_h else raise ArgumentError, "must respond to `to_h` or `to_hash`" end.transform_keys(&:to_sym) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, **_kwargs) {|Object| ... } ⇒ Object (private)
Handles method calls that don’t match defined methods. Supports assignment-style calls (e.g., ‘name=`) and key access.
282 283 284 285 286 287 |
# File 'lib/cmdx/context.rb', line 282 def method_missing(method_name, *args, **_kwargs, &) fetch(method_name) do str_name = method_name.to_s store(str_name.chop, args.first) if str_name.end_with?("=") end end |
Instance Attribute Details
#table ⇒ Hash{Symbol => Object} (readonly) Also known as: to_h
Returns the internal hash storing context data.
22 23 24 |
# File 'lib/cmdx/context.rb', line 22 def table @table end |
Class Method Details
.build(context = {}) ⇒ Context
Builds a Context instance, reusing existing unfrozen contexts when possible.
65 66 67 68 69 70 71 72 73 |
# File 'lib/cmdx/context.rb', line 65 def self.build(context = {}) if context.is_a?(self) && !context.frozen? context elsif context.respond_to?(:context) build(context.context) else new(context) end end |
Instance Method Details
#[](key) ⇒ Object?
Retrieves a value from the context by key.
87 88 89 |
# File 'lib/cmdx/context.rb', line 87 def [](key) table[key.to_sym] end |
#clear! ⇒ Context Also known as: clear
Clears all key-value pairs from the context.
198 199 200 201 |
# File 'lib/cmdx/context.rb', line 198 def clear! table.clear self end |
#delete!(key) {|key| ... } ⇒ Object? Also known as: delete
Deletes a key-value pair from the context.
183 184 185 |
# File 'lib/cmdx/context.rb', line 183 def delete!(key, &) table.delete(key.to_sym, &) end |
#dig(key, *keys) ⇒ Object?
Digs into nested structures using the given keys.
250 251 252 |
# File 'lib/cmdx/context.rb', line 250 def dig(key, *keys) table.dig(key.to_sym, *keys) end |
#eql?(other) ⇒ Boolean Also known as: ==
Compares this context with another object for equality.
216 217 218 |
# File 'lib/cmdx/context.rb', line 216 def eql?(other) other.is_a?(self.class) && (to_h == other.to_h) end |
#fetch(key) {|key| ... } ⇒ Object
Fetches a value from the context by key, with optional default value.
124 125 126 |
# File 'lib/cmdx/context.rb', line 124 def fetch(key, ...) table.fetch(key.to_sym, ...) end |
#fetch_or_store(key, value = nil) {|key| ... } ⇒ Object
Fetches a value from the context by key, or stores and returns a default value if not found.
144 145 146 147 148 |
# File 'lib/cmdx/context.rb', line 144 def fetch_or_store(key, value = nil) table.fetch(key.to_sym) do table[key.to_sym] = block_given? ? yield : value end end |
#key?(key) ⇒ Boolean
Checks if the context contains a specific key.
233 234 235 |
# File 'lib/cmdx/context.rb', line 233 def key?(key) table.key?(key.to_sym) end |
#merge!(args = {}) ⇒ Context Also known as: merge
Merges the given arguments into the current context, modifying it in place.
163 164 165 166 |
# File 'lib/cmdx/context.rb', line 163 def merge!(args = {}) args.to_h.each { |key, value| self[key.to_sym] = value } self end |
#store(key, value) ⇒ Object Also known as: []=
Stores a key-value pair in the context.
104 105 106 |
# File 'lib/cmdx/context.rb', line 104 def store(key, value) table[key.to_sym] = value end |