Class: Domainic::Command::Context::AttributeSet

Inherits:
Object
  • Object
show all
Defined in:
lib/domainic/command/context/attribute_set.rb

Overview

A collection class for managing a set of command context attributes. This class provides a simple interface for storing, accessing, and iterating over Attribute instances.

Examples:

set = AttributeSet.new
set.add(Attribute.new(:name))
set[:name] # => #<Attribute name=:name>

Author:

Since:

  • 0.1.0

Instance Method Summary collapse

Constructor Details

#initializeAttributeSet

Creates a new AttributeSet instance

Since:

  • 0.1.0



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

def initialize
  @lookup = {}
end

Instance Method Details

#[](attribute_name) ⇒ Attribute?

Retrieves an attribute by name

Parameters:

  • attribute_name (String, Symbol)

    The name of the attribute to retrieve

Returns:

  • (Attribute, nil)

    The attribute with the given name, or nil if not found

Since:

  • 0.1.0



33
34
35
# File 'lib/domainic/command/context/attribute_set.rb', line 33

def [](attribute_name)
  @lookup[attribute_name.to_sym]
end

#add(attribute) ⇒ void

This method returns an undefined value.

Adds an attribute to the set

Parameters:

  • attribute (Attribute)

    The attribute to add

Raises:

Since:

  • 0.1.0



44
45
46
47
48
49
50
# File 'lib/domainic/command/context/attribute_set.rb', line 44

def add(attribute)
  unless attribute.is_a?(Attribute)
    raise ArgumentError, 'Attribute must be an instance of Domainic::Command::Context::Attribute'
  end

  @lookup[attribute.name] = attribute
end

#allArray<Attribute>

Returns all attributes in the set

Returns:

  • (Array<Attribute>)

    An array of all attributes

Since:

  • 0.1.0



56
57
58
# File 'lib/domainic/command/context/attribute_set.rb', line 56

def all
  @lookup.values
end

#each {|Attribute| ... } ⇒ void

This method returns an undefined value.

Iterates over each attribute in the set

Yields:

Since:

  • 0.1.0



66
67
68
# File 'lib/domainic/command/context/attribute_set.rb', line 66

def each(...)
  all.each(...)
end

#each_with_object(object) {|Attribute, Object| ... } ⇒ Object

Iterates over each attribute in the set with an object

Returns The final state of the object.

Parameters:

  • object (Object)

    The object to pass to the block

Yields:

  • (Attribute, Object)

    Each attribute and the object

Returns:

  • (Object)

    The final state of the object

Since:

  • 0.1.0



78
79
80
# File 'lib/domainic/command/context/attribute_set.rb', line 78

def each_with_object(...)
  all.each_with_object(...) # steep:ignore UnresolvedOverloading
end