Class: Domainic::Command::Context::RuntimeContext

Inherits:
Object
  • Object
show all
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.

Examples:

Hash-style access

context = RuntimeContext.new(count: 1)
context[:count] #=> 1
context[:count] = 2
context[:count] #=> 2

Method-style access

context = RuntimeContext.new(name: "test")
context.name #=> "test"
context.name = "new test"
context.name #=> "new test"

Author:

Since:

  • 0.1.0

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ RuntimeContext

Creates a new RuntimeContext with the given options

Parameters:

  • options (Hash)

    Initial values for the context

Since:

  • 0.1.0



36
37
38
# File 'lib/domainic/command/context/runtime_context.rb', line 36

def initialize(**options)
  @data = options.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

Returns:

  • (Object, nil)

Since:

  • 0.1.0



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

Parameters:

  • attribute_name (String, Symbol)

    The name of the attribute to retrieve

Returns:

  • (Object, nil)

    The value associated with the attribute name

Since:

  • 0.1.0



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

Parameters:

  • attribute_name (String, Symbol)

    The name of the attribute to set

  • value (Object)

    The value to store

Returns:

  • (Object)

    The stored value

Since:

  • 0.1.0



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_hashHash{Symbol => Object} Also known as: to_h

Note:

Class and Module values are not duplicated to prevent potential issues

Converts the context to a hash, duplicating values where appropriate

Returns:

  • (Hash{Symbol => Object})

    A hash containing all stored values

Since:

  • 0.1.0



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