Class: Treaty::Entity::Attribute::Option::Modifiers::AsModifier
- 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
-
Request to Service mapping:
request do string :user_id, as: :id end # Input: { user_id: "123" } # Service receives: { id: "123" } -
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
-
#target_name ⇒ Symbol
Returns the target name for the attribute.
-
#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.
-
#transforms_name? ⇒ Boolean
Indicates that AsModifier transforms attribute names.
-
#validate_schema! ⇒ void
Validates that target name is a Symbol.
Methods inherited from Base
Constructor Details
This class inherits a constructor from Treaty::Entity::Attribute::Option::Base
Instance Method Details
#target_name ⇒ Symbol
Returns the target name for the attribute
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
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
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
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 |