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

Inherits:
Object
  • Object
show all
Defined in:
lib/treaty/entity/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:

  • :requiredrequired: true
  • :optionalrequired: 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)



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

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



98
99
100
# File 'lib/treaty/entity/attribute/option/base.rb', line 98

def target_name
  @attribute_name
end

#transform_value(value, _root_data = {}) ⇒ 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

  • _root_data (Hash) (defaults to: {})

    Full raw data from root level (used by computed modifier)

Returns:

  • (Object)

    Transformed value



82
83
84
# File 'lib/treaty/entity/attribute/option/base.rb', line 82

def transform_value(value, _root_data = {})
  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



90
91
92
# File 'lib/treaty/entity/attribute/option/base.rb', line 90

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:



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

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:



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

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