Class: Treaty::Entity::Attribute::Option::Modifiers::AsModifier

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

Overview

Transforms attribute names during data processing.

Usage Examples

Simple mode:

# Request: expects "handle", outputs as "value"
string :handle, as: :value

Advanced mode:

string :handle, as: { is: :value, message: nil }

Use Cases

  1. Request to Service mapping:

    request do
      string :user_id, as: :id
    end
    # Input: { user_id: "123" }
    # Service receives: { id: "123" }
    
  2. Service to Response mapping:

    response 200 do
      string :id, as: :user_id
    end
    # Service returns: { id: "123" }
    # Output: { user_id: "123" }
    

How It Works

AsModifier doesn't transform values - it transforms attribute names. The orchestrator uses target_name to map source name to target name.

Advanced Mode

Schema format: { is: :symbol, message: nil }

Instance Method Summary collapse

Methods inherited from Base

#initialize, #validate_value!

Constructor Details

This class inherits a constructor from Treaty::Entity::Attribute::Option::Base

Instance Method Details

#target_nameSymbol

Returns the target name for the attribute

Returns:

  • (Symbol)

    The target attribute name



75
76
77
# File 'lib/treaty/entity/attribute/option/modifiers/as_modifier.rb', line 75

def target_name
  option_value
end

#transform_value(value, _root_data = {}) ⇒ Object

AsModifier doesn't modify the value itself, only the name The renaming is handled by the orchestrator using target_name

Parameters:

  • value (Object)

    The value to transform

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

    Unused root data parameter

Returns:

  • (Object)

    Unchanged value



85
86
87
# File 'lib/treaty/entity/attribute/option/modifiers/as_modifier.rb', line 85

def transform_value(value, _root_data = {})
  value
end

#transforms_name?Boolean

Indicates that AsModifier transforms attribute names

Returns:

  • (Boolean)

    Always returns true



68
69
70
# File 'lib/treaty/entity/attribute/option/modifiers/as_modifier.rb', line 68

def transforms_name?
  true
end

#validate_schema!void

This method returns an undefined value.

Validates that target name is a Symbol

Raises:



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/treaty/entity/attribute/option/modifiers/as_modifier.rb', line 52

def validate_schema!
  target = option_value

  return if target.is_a?(Symbol)

  raise Treaty::Exceptions::Validation,
        I18n.t(
          "treaty.attributes.modifiers.as.invalid_type",
          attribute: @attribute_name,
          type: target.class
        )
end