Class: Treaty::Attribute::HelperMapper

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

Overview

Maps DSL helper symbols to their simple mode option equivalents.

## Purpose

Helpers provide the most concise syntax for common options. They are syntactic sugar that gets converted to simple mode options.

## Available Helpers

  • ‘:required` → `required: true`

  • ‘:optional` → `required: false`

## Usage Examples

Helper mode (most concise):

string :title, :required
string :bio, :optional

Equivalent to simple mode:

string :title, required: true
string :bio, required: false

## Processing Flow

  1. Helper mode: ‘string :title, :required`

  2. HelperMapper: ‘:required` → `required: true`

  3. OptionNormalizer: ‘required: true` → `{ is: true, message: nil }`

  4. Final: Advanced mode used internally

## Adding New Helpers

To add a new helper: “‘ruby HELPER_MAPPINGS = {

required: { required: true },
optional: { required: false },
my_helper: { my_option: :smth }  # New helper example

}.freeze “‘

Constant Summary collapse

HELPER_MAPPINGS =
{
  required: { required: true },
  optional: { required: false }
}.freeze

Class Method Summary collapse

Class Method Details

.helper?(symbol) ⇒ Boolean

Checks if a symbol is a registered helper

Parameters:

  • symbol (Symbol)

    Symbol to check

Returns:

  • (Boolean)

    True if symbol is a helper



66
67
68
# File 'lib/treaty/attribute/helper_mapper.rb', line 66

def helper?(symbol)
  HELPER_MAPPINGS.key?(symbol)
end

.map(helpers) ⇒ Hash

Maps helper symbols to their simple mode equivalents

Parameters:

  • helpers (Array<Symbol>)

    Array of helper symbols

Returns:

  • (Hash)

    Simple mode options hash



55
56
57
58
59
60
# File 'lib/treaty/attribute/helper_mapper.rb', line 55

def map(helpers)
  helpers.each_with_object({}) do |helper, result|
    mapping = HELPER_MAPPINGS.fetch(helper)
    result.merge!(mapping) if mapping.present?
  end
end