Class: Domainic::Command::Context::RuntimeContext
- Inherits:
-
Object
- Object
- Domainic::Command::Context::RuntimeContext
- Defined in:
- lib/domainic/command/context/runtime_context.rb
Overview
A flexible context class for managing command state during execution. This class provides a dynamic storage mechanism for command data, allowing both hash-style and method-style access to values.
The RuntimeContext serves as a mutable workspace during command execution, bridging the gap between input parameters and output values. It automatically handles type coercion of keys to symbols and provides safe value duplication when converting to a hash.
Instance Method Summary collapse
-
#[](attribute_name) ⇒ Object?
Retrieves a value by its attribute name.
-
#[]=(attribute_name, value) ⇒ Object
Sets a value for the given attribute name.
-
#initialize(**options) ⇒ RuntimeContext
constructor
Creates a new RuntimeContext with the given options.
-
#to_hash ⇒ Hash{Symbol => Object}
(also: #to_h)
Converts the context to a hash, duplicating values where appropriate.
Constructor Details
#initialize(**options) ⇒ RuntimeContext
Creates a new RuntimeContext with the given options
36 37 38 |
# File 'lib/domainic/command/context/runtime_context.rb', line 36 def initialize(**) @data = .transform_keys(&:to_sym) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name) ⇒ Object? (private)
Handles dynamic method calls for reading and writing attributes
80 81 82 83 84 85 86 87 88 |
# File 'lib/domainic/command/context/runtime_context.rb', line 80 def method_missing(method_name, ...) return super unless respond_to_missing?(method_name) if method_name.to_s.end_with?('=') write_to_attribute(method_name.to_s.delete_suffix('=').to_sym, ...) else @data[method_name] end end |
Instance Method Details
#[](attribute_name) ⇒ Object?
Retrieves a value by its attribute name
46 47 48 |
# File 'lib/domainic/command/context/runtime_context.rb', line 46 def [](attribute_name) read_from_attribute(attribute_name) end |
#[]=(attribute_name, value) ⇒ Object
Sets a value for the given attribute name
57 58 59 |
# File 'lib/domainic/command/context/runtime_context.rb', line 57 def []=(attribute_name, value) write_to_attribute(attribute_name, value) end |
#to_hash ⇒ Hash{Symbol => Object} Also known as: to_h
Class and Module values are not duplicated to prevent potential issues
Converts the context to a hash, duplicating values where appropriate
67 68 69 70 71 |
# File 'lib/domainic/command/context/runtime_context.rb', line 67 def to_hash @data.transform_values do |value| value.is_a?(Class) || value.is_a?(Module) ? value : value.dup end end |