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



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



17
18
19
# File 'lib/cmdx/attribute_registry.rb', line 17

def registry
  @registry
end

Instance Method Details

#define_and_verify(task) ⇒ Object

Associates all registered attributes with a task and verifies their definitions. This method is called during task setup to establish attribute-task relationships and validate the attribute hierarchy.

Parameters:

  • task (Task)

    The task to associate with all attributes



90
91
92
93
94
95
# File 'lib/cmdx/attribute_registry.rb', line 90

def define_and_verify(task)
  registry.each do |attribute|
    attribute.task = task
    attribute.define_and_verify_tree
  end
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:



75
76
77
78
79
80
81
# File 'lib/cmdx/attribute_registry.rb', line 75

def deregister(names)
  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:



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:



58
59
60
61
# File 'lib/cmdx/attribute_registry.rb', line 58

def register(attributes)
  @registry.concat(Array(attributes))
  self
end