Class: CMDx::AttributeRegistry
- Inherits:
-
Object
- Object
- CMDx::AttributeRegistry
- Defined in:
- lib/cmdx/attribute_registry.rb
Overview
Manages a collection of attributes for task definition and verification. The registry provides methods to register, deregister, and process attributes in a hierarchical structure, supporting nested attribute definitions.
Instance Attribute Summary collapse
-
#registry ⇒ Array<Attribute>
(also: #to_a)
readonly
Returns the collection of registered attributes.
Instance Method Summary collapse
-
#define_and_verify(task) ⇒ Object
Verifies attribute definitions against a task instance.
-
#define_readers_on!(task_class, attrs = registry) ⇒ Object
Eagerly defines attribute reader methods on the task class for all attributes whose method names can be statically resolved.
-
#deregister(names) ⇒ AttributeRegistry
Removes attributes from the registry by name.
-
#dup ⇒ AttributeRegistry
Creates a duplicate of this registry with copied attributes.
-
#initialize(registry = []) ⇒ AttributeRegistry
constructor
Creates a new attribute registry with an optional initial collection.
-
#register(attributes) ⇒ AttributeRegistry
Registers one or more attributes to the registry.
-
#undefine_readers_on!(task_class, names) ⇒ Object
Removes eagerly defined reader methods from the task class for attributes about to be deregistered.
Constructor Details
#initialize(registry = []) ⇒ AttributeRegistry
Creates a new attribute registry with an optional initial collection.
31 32 33 |
# File 'lib/cmdx/attribute_registry.rb', line 31 def initialize(registry = []) @registry = registry end |
Instance Attribute Details
#registry ⇒ Array<Attribute> (readonly) Also known as: to_a
Returns the collection of registered attributes.
17 18 19 |
# File 'lib/cmdx/attribute_registry.rb', line 17 def registry @registry end |
Instance Method Details
#define_and_verify(task) ⇒ Object
Verifies attribute definitions against a task instance. Each attribute is duped before binding so the class-level originals are never mutated — this eliminates the concurrency hazard of shared mutable @task, @source, and @method_name state.
122 123 124 125 126 127 128 |
# File 'lib/cmdx/attribute_registry.rb', line 122 def define_and_verify(task) registry.each do |attribute| duplicate = attribute.dup duplicate.task = task duplicate.define_and_verify_tree end end |
#define_readers_on!(task_class, attrs = registry) ⇒ Object
Eagerly defines attribute reader methods on the task class for all attributes whose method names can be statically resolved. Called at class definition time so readers are defined once, not per-execution.
91 92 93 |
# File 'lib/cmdx/attribute_registry.rb', line 91 def define_readers_on!(task_class, attrs = registry) attrs.each { |attr| define_reader_tree!(task_class, attr) } end |
#deregister(names) ⇒ AttributeRegistry
Removes attributes from the registry by name. Supports hierarchical attribute removal by matching the entire attribute tree.
75 76 77 78 79 80 81 |
# File 'lib/cmdx/attribute_registry.rb', line 75 def deregister(names) Utils::Wrap.array(names).each do |name| @registry.reject! { |attribute| matches_attribute_tree?(attribute, name.to_sym) } end self end |
#dup ⇒ AttributeRegistry
Creates a duplicate of this registry with copied attributes.
43 44 45 |
# File 'lib/cmdx/attribute_registry.rb', line 43 def dup self.class.new(registry.dup) end |
#register(attributes) ⇒ AttributeRegistry
Registers one or more attributes to the registry.
58 59 60 61 |
# File 'lib/cmdx/attribute_registry.rb', line 58 def register(attributes) @registry.concat(Utils::Wrap.array(attributes)) self end |
#undefine_readers_on!(task_class, names) ⇒ Object
Removes eagerly defined reader methods from the task class for attributes about to be deregistered. Must be called before #deregister.
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/cmdx/attribute_registry.rb', line 102 def undefine_readers_on!(task_class, names) Utils::Wrap.array(names).each do |name| sym = name.to_sym registry.each do |attribute| next unless matches_attribute_tree?(attribute, sym) undefine_reader_tree!(task_class, attribute) end end end |