Class: Treaty::Entity::Attribute::Option::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/treaty/entity/attribute/option/registry.rb

Overview

Central registry for all option processors (validators, modifiers, and conditionals).

Purpose

Provides a centralized registry pattern for managing all option processors. Enables dynamic discovery and extensibility of the option system.

Responsibilities

  1. Registration - Stores option processor classes
  2. Retrieval - Provides access to registered processors
  3. Categorization - Organizes processors by category (validator/modifier/conditional)
  4. Validation - Checks if options are registered

Registered Options

Validators (sorted by position)

  • :type → TypeValidator (position: 100)
  • :required → RequiredValidator (position: 200)
  • :inclusion → InclusionValidator (position: 300)
  • :format → FormatValidator (position: 400)

Modifiers (sorted by position)

  • :transform → TransformModifier (position: 500)
  • :cast → CastModifier (position: 600)
  • :computed → ComputedModifier (position: 700)
  • :default → DefaultModifier (position: 800)
  • :as → AsModifier (position: 900)

Conditionals (no position - handled separately)

  • :if → IfConditional
  • :unless → UnlessConditional

Usage

Registration (done in RegistryInitializer):

Registry.register(:required, RequiredValidator, category: :validator, position: 200)
Registry.register(:if, IfConditional, category: :conditional)

Retrieval (done in OptionOrchestrator):

processor_class = Registry.processor_for(:required)
processor = processor_class.new(...)

Extensibility

To add a new option:

  1. Create processor class inheriting from Option::Base
  2. Register it: Registry.register(:my_option, MyProcessor, category: :validator)
  3. Option becomes available in DSL immediately

Architecture

Works with:

  • RegistryInitializer - Populates registry with built-in options
  • OptionOrchestrator - Uses registry to build processors
  • Option::Base - Base class for all registered processors

Class Method Summary collapse

Class Method Details

.all_optionsArray<Symbol>

Get all registered option names

Returns:

  • (Array<Symbol>)


114
115
116
# File 'lib/treaty/entity/attribute/option/registry.rb', line 114

def all_options
  registry.keys
end

.category_for(option_name) ⇒ Symbol?

Get category for an option

Parameters:

  • option_name (Symbol)

    The name of the option

Returns:

  • (Symbol, nil)

    The category (:validator or :modifier) or nil if not found



91
92
93
# File 'lib/treaty/entity/attribute/option/registry.rb', line 91

def category_for(option_name)
  registry.dig(option_name, :category)
end

.conditionalsHash

Get all conditionals

Returns:

  • (Hash)

    Hash of option_name => processor_class for conditionals



137
138
139
140
# File 'lib/treaty/entity/attribute/option/registry.rb', line 137

def conditionals
  registry.select { |_, info| info.fetch(:category) == :conditional }
          .transform_values { |info| info.fetch(:processor_class) }
end

.modifiersHash

Get all modifiers

Returns:

  • (Hash)

    Hash of option_name => processor_class for modifiers



129
130
131
132
# File 'lib/treaty/entity/attribute/option/registry.rb', line 129

def modifiers
  registry.select { |_, info| info.fetch(:category) == :modifier }
          .transform_values { |info| info.fetch(:processor_class) }
end

.position_for(option_name) ⇒ Integer?

Get position for an option

Parameters:

  • option_name (Symbol)

    The name of the option

Returns:

  • (Integer, nil)

    The execution order position or nil if not set



99
100
101
# File 'lib/treaty/entity/attribute/option/registry.rb', line 99

def position_for(option_name)
  registry.dig(option_name, :position)
end

.processor_for(option_name) ⇒ Class?

Get processor class for an option

Parameters:

  • option_name (Symbol)

    The name of the option

Returns:

  • (Class, nil)

    The processor class or nil if not found



83
84
85
# File 'lib/treaty/entity/attribute/option/registry.rb', line 83

def processor_for(option_name)
  registry.dig(option_name, :processor_class)
end

.register(option_name, processor_class, category:, position: nil) ⇒ Object

Register an option processor

Parameters:

  • option_name (Symbol)

    The name of the option (e.g., :required, :as, :default)

  • processor_class (Class)

    The processor class

  • category (Symbol)

    The category (:validator, :modifier, or :conditional)

  • position (Integer, nil) (defaults to: nil)

    Execution order position (nil for conditionals)



71
72
73
74
75
76
77
# File 'lib/treaty/entity/attribute/option/registry.rb', line 71

def register(option_name, processor_class, category:, position: nil)
  registry[option_name] = {
    processor_class:,
    category:,
    position:
  }
end

.registered?(option_name) ⇒ Boolean

Check if an option is registered

Parameters:

  • option_name (Symbol)

    The name of the option

Returns:

  • (Boolean)


107
108
109
# File 'lib/treaty/entity/attribute/option/registry.rb', line 107

def registered?(option_name)
  registry.key?(option_name)
end

.reset!Object

Reset registry (mainly for testing)



143
144
145
# File 'lib/treaty/entity/attribute/option/registry.rb', line 143

def reset!
  @registry = nil
end

.validatorsHash

Get all validators

Returns:

  • (Hash)

    Hash of option_name => processor_class for validators



121
122
123
124
# File 'lib/treaty/entity/attribute/option/registry.rb', line 121

def validators
  registry.select { |_, info| info.fetch(:category) == :validator }
          .transform_values { |info| info.fetch(:processor_class) }
end