Class: Servactory::Maintenance::Attributes::OptionsCollection
- Inherits:
-
Object
- Object
- Servactory::Maintenance::Attributes::OptionsCollection
- 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
-
#defined_conflict_code ⇒ Object?
Returns the first conflict code found among options.
-
#find_by(name:) ⇒ Option?
Finds an option by its name using indexed lookup.
-
#initialize ⇒ OptionsCollection
constructor
Initializes an empty collection.
-
#names ⇒ Array<Symbol>
Returns all option names in the collection.
-
#options_for_checks ⇒ Hash{Symbol => Object}
Returns options that need validation checks as a hash.
-
#validation_classes ⇒ Array<Class>
Returns unique validation classes from all options.
Constructor Details
#initialize ⇒ OptionsCollection
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_code ⇒ Object?
Returns the first conflict code found among options.
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.
93 94 95 |
# File 'lib/servactory/maintenance/attributes/options_collection.rb', line 93 def find_by(name:) [name] end |
#names ⇒ Array<Symbol>
Returns all option names in the collection.
57 58 59 |
# File 'lib/servactory/maintenance/attributes/options_collection.rb', line 57 def names map(&:name) end |
#options_for_checks ⇒ Hash{Symbol => Object}
Returns options that need validation checks as a hash.
74 75 76 77 78 |
# File 'lib/servactory/maintenance/attributes/options_collection.rb', line 74 def @options_for_checks ||= filter(&:need_for_checks?).to_h do |option| [option.name, extract_normalized_body_from(option:)] end end |
#validation_classes ⇒ Array<Class>
Returns unique validation classes from all options.
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 |