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
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(**options) ⇒ Behavior
Initializes a new context instance with the given attributes.
-
#to_hash ⇒ Hash{Symbol => Object}
(also: #to_h)
Returns a hash of all attribute names and their values.
Class Method Details
.included(base) ⇒ Object
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.
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/domainic/command/context/behavior.rb', line 108 def initialize(**) = .transform_keys(&:to_sym) self.class.send(:attributes).each do |attribute| value = .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_hash ⇒ Hash{Symbol => Object} Also known as: to_h
Returns a hash of all attribute names and their values.
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 |