Class: Treaty::Attribute::Option::Base

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

Overview

Base class for all option processors (validators and modifiers).

## Option Modes

Treaty supports two modes for defining options:

  1. **Simple Mode** - Concise syntax for common cases:

    • ‘required: true`

    • ‘as: :value`

    • ‘default: 12`

    • ‘in: %w[twitter linkedin]`

  2. **Advanced Mode** - Extended syntax with custom messages:

    • ‘required: { is: true, message: “Custom error” }`

    • ‘as: { is: :value, message: nil }`

    • ‘inclusion: { in: %w, message: “Must be one of…” }`

## Helpers

Helpers are shortcuts in DSL that map to simple mode options:

  • ‘:required` → `required: true`

  • ‘:optional` → `required: false`

## Advanced Mode Keys

Each option in advanced mode has a value key:

  • Default key: ‘:is` (used by most options)

  • Special key: ‘:in` (used by inclusion validator)

The value key is defined by overriding ‘value_key` method in subclasses.

## Processing Phases

Each option processor can participate in three phases:

  • Phase 1: Schema validation (validate DSL definition correctness)

  • Phase 2: Value validation (validate runtime data values)

  • Phase 3: Value transformation (transform values: defaults, renaming, etc.)

Instance Method Summary collapse

Constructor Details

#initialize(attribute_name:, attribute_type:, option_schema:) ⇒ Base

Creates a new option processor instance

Parameters:

  • attribute_name (Symbol)

    The name of the attribute

  • attribute_type (Symbol)

    The type of the attribute

  • option_schema (Object)

    The option schema (simple or advanced mode)



49
50
51
52
53
# File 'lib/treaty/attribute/option/base.rb', line 49

def initialize(attribute_name:, attribute_type:, option_schema:)
  @attribute_name = attribute_name
  @attribute_type = attribute_type
  @option_schema = option_schema
end

Instance Method Details

#target_nameSymbol

Returns the target name for the attribute if this processor transforms names Override in subclasses if needed (e.g., AsModifier)

Returns:

  • (Symbol)

    The target attribute name



96
97
98
# File 'lib/treaty/attribute/option/base.rb', line 96

def target_name
  @attribute_name
end

#transform_value(value) ⇒ Object

Phase 3: Transforms value Returns transformed value or original if no transformation needed Override in subclasses if transformation is needed

Parameters:

  • value (Object)

    The value to transform

Returns:

  • (Object)

    Transformed value



80
81
82
# File 'lib/treaty/attribute/option/base.rb', line 80

def transform_value(value)
  value
end

#transforms_name?Boolean

Indicates if this option processor transforms attribute names Override in subclasses if needed (e.g., AsModifier)

Returns:

  • (Boolean)

    True if this processor transforms names



88
89
90
# File 'lib/treaty/attribute/option/base.rb', line 88

def transforms_name?
  false
end

#validate_schema!void

This method returns an undefined value.

Phase 1: Validates schema (DSL definition) Override in subclasses if validation is needed

Raises:



60
61
62
# File 'lib/treaty/attribute/option/base.rb', line 60

def validate_schema!
  # No-op by default
end

#validate_value!(value) ⇒ void

This method returns an undefined value.

Phase 2: Validates value (runtime data) Override in subclasses if validation is needed

Parameters:

  • value (Object)

    The value to validate

Raises:



70
71
72
# File 'lib/treaty/attribute/option/base.rb', line 70

def validate_value!(value)
  # No-op by default
end