Class: Treaty::Entity::Attribute::HelperMapper

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

  • :requiredrequired: true
  • :optionalrequired: 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: :requiredrequired: true
  3. OptionNormalizer: required: true{ is: true, message: nil }
  4. Final: Advanced mode used internally

Adding New Helpers

To add a new helper:

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



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

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



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

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