Class: CMDx::AttributeRegistry

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

Instance Method Summary collapse

Constructor Details

#initialize(registry = []) ⇒ AttributeRegistry

Creates a new attribute registry with an optional initial collection.

Examples:

registry = AttributeRegistry.new
registry = AttributeRegistry.new([attr1, attr2])

Parameters:

  • registry (Array<Attribute>) (defaults to: [])

    Initial attributes to register

Rbs:

  • (?Array registry) -> void



31
32
33
# File 'lib/cmdx/attribute_registry.rb', line 31

def initialize(registry = [])
  @registry = registry
end

Instance Attribute Details

#registryArray<Attribute> (readonly) Also known as: to_a

Returns the collection of registered attributes.

Examples:

registry.registry # => [#<Attribute @name=:name>, #<Attribute @name=:email>]

Returns:

  • (Array<Attribute>)

    Array of registered attributes

Rbs:



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.

Parameters:

  • task (Task)

    The task to verify attributes against

Rbs:

  • (Task task) -> void



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.

Parameters:

  • task_class (Class)

    The task class to define readers on

  • attrs (Array<Attribute>) (defaults to: registry)

    Attributes to process (defaults to all)

Rbs:

  • (Class task_class, ?Array attrs) -> void



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.

Examples:

registry.deregister(:name)
registry.deregister(['name1', 'name2'])

Parameters:

  • names (Symbol, String, Array<Symbol, String>)

    Name(s) of attributes to remove

Returns:

Rbs:

  • ((Symbol | String | Array[Symbol | String]) names) -> self



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

#dupAttributeRegistry

Creates a duplicate of this registry with copied attributes.

Examples:

new_registry = registry.dup

Returns:

Rbs:

  • () -> AttributeRegistry



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.

Examples:

registry.register(attribute)
registry.register([attr1, attr2])

Parameters:

Returns:

Rbs:

  • (Attribute | Array attributes) -> self



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.

Parameters:

  • task_class (Class)

    The task class to undefine readers on

  • names (Array<Symbol, String>)

    Attribute method names being removed

Rbs:

  • (Class task_class, (Symbol | String | Array[Symbol | String]) names) -> void



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