Module: Domainic::Command::Context::Behavior

Included in:
InputContext, OutputContext
Defined in:
lib/domainic/command/context/behavior.rb

Overview

A module that provides attribute management for command contexts. When included in a class, it provides a DSL for defining and managing typed attributes with validation, default values, and thread-safe access.

Thread Safety

The attribute system is designed to be thread-safe during class definition and inheritance. A class-level mutex protects the attribute registry during:

  • Definition of new attributes via the DSL
  • Inheritance of attributes to subclasses

Author:

Since:

  • 0.1.0

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Since:

  • 0.1.0



22
23
24
25
# File 'lib/domainic/command/context/behavior.rb', line 22

def self.included(base)
  super
  base.extend(ClassMethods)
end

Instance Method Details

#initialize(**options) ⇒ Behavior

Initializes a new context instance with the given attributes.

Parameters:

  • options (Hash{String, Symbol => Object})

    Attribute values for initialization

Returns:

Raises:

  • (ArgumentError)

    If any attribute values are invalid

Since:

  • 0.1.0



108
109
110
111
112
113
114
115
116
117
# File 'lib/domainic/command/context/behavior.rb', line 108

def initialize(**options)
  options = options.transform_keys(&:to_sym)

  self.class.send(:attributes).each do |attribute|
    value = options.fetch(attribute.name) { attribute.default if attribute.default? }
    raise ArgumentError, "Invalid value for #{attribute.name}: #{value.inspect}" unless attribute.valid?(value)

    instance_variable_set(:"@#{attribute.name}", value)
  end
end

#to_hashHash{Symbol => Object} Also known as: to_h

Returns a hash of all attribute names and their values.

Returns:

  • (Hash{Symbol => Object})

    A hash of attribute values

Since:

  • 0.1.0



123
124
125
126
127
# File 'lib/domainic/command/context/behavior.rb', line 123

def to_hash
  self.class.send(:attributes).each_with_object({}) do |attribute, hash|
    hash[attribute.name] = public_send(attribute.name)
  end
end