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

Inherits:
Base
  • Object
show all
Defined in:
lib/treaty/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**: “‘ruby request do

    string :user_id, as: :id
    

    end # Input: { user_id: “123” } # Service receives: { id: “123” } “‘

  2. **Service to Response mapping**: “‘ruby 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::Attribute::Option::Base

Instance Method Details

#target_nameSymbol

Returns the target name for the attribute



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

def target_name
  option_value
end

#transform_value(value) ⇒ Object

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



83
84
85
# File 'lib/treaty/attribute/option/modifiers/as_modifier.rb', line 83

def transform_value(value)
  value
end

#transforms_name?Boolean

Indicates that AsModifier transforms attribute names



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

def transforms_name?
  true
end

#validate_schema!void

This method returns an undefined value.

Validates that target name is a Symbol

Raises:



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

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