Class: Servactory::Maintenance::Attributes::OptionsCollection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/servactory/maintenance/attributes/options_collection.rb

Overview

Collection wrapper for managing Option objects.

## Purpose

OptionsCollection provides a unified interface for storing and querying Option instances associated with service attributes (inputs, internals, outputs). It wraps a Set to ensure uniqueness and delegates common enumeration methods.

## Usage

The collection is used internally by Input, Internal, and Output classes to manage their registered options:

“‘ruby collection = OptionsCollection.new collection << Option.new(name: :required, …) collection << Option.new(name: :types, …)

collection.names # => [:required, :types] collection.find_by(name: :required) # => Option instance “‘

## Performance

The collection uses memoization for frequently accessed data:

  • ‘validation_classes` - cached list of unique validation classes

  • ‘options_for_checks` - cached hash for validation pipeline

  • ‘options_index` - cached hash for O(1) lookups by name

Instance Method Summary collapse

Constructor Details

#initializeOptionsCollection

Initializes an empty collection.



50
51
52
# File 'lib/servactory/maintenance/attributes/options_collection.rb', line 50

def initialize
  @collection = Set.new
end

Instance Method Details

#defined_conflict_codeObject?

Returns the first conflict code found among options.

Returns:

  • (Object, nil)

    conflict code or nil if no conflicts



83
84
85
86
87
# File 'lib/servactory/maintenance/attributes/options_collection.rb', line 83

def defined_conflict_code
  flat_map { |option| resolve_conflicts_from(option:) }
    .reject(&:blank?)
    .first
end

#find_by(name:) ⇒ Option?

Finds an option by its name using indexed lookup.

Parameters:

  • name (Symbol)

    the option name to find

Returns:

  • (Option, nil)

    the found option or nil



93
94
95
# File 'lib/servactory/maintenance/attributes/options_collection.rb', line 93

def find_by(name:)
  options_index[name]
end

#namesArray<Symbol>

Returns all option names in the collection.

Returns:

  • (Array<Symbol>)

    list of option names



57
58
59
# File 'lib/servactory/maintenance/attributes/options_collection.rb', line 57

def names
  map(&:name)
end

#options_for_checksHash{Symbol => Object}

Returns options that need validation checks as a hash.

Returns:

  • (Hash{Symbol => Object})

    option names mapped to normalized bodies



74
75
76
77
78
# File 'lib/servactory/maintenance/attributes/options_collection.rb', line 74

def options_for_checks
  @options_for_checks ||= filter(&:need_for_checks?).to_h do |option|
    [option.name, extract_normalized_body_from(option:)]
  end
end

#validation_classesArray<Class>

Returns unique validation classes from all options.

Returns:

  • (Array<Class>)

    deduplicated list of validation classes



64
65
66
67
68
69
# File 'lib/servactory/maintenance/attributes/options_collection.rb', line 64

def validation_classes
  @validation_classes ||=
    filter { |option| option.validation_class.present? }
    .map(&:validation_class)
    .uniq
end