Class: Treaty::Attribute::Builder::Base
- Inherits:
-
Object
- Object
- Treaty::Attribute::Builder::Base
- Defined in:
- lib/treaty/attribute/builder/base.rb
Overview
Base DSL builder for defining attributes in request/response definitions.
## Purpose
Provides the DSL interface for defining attributes within objects. Handles method_missing magic to support type-based method calls.
## Responsibilities
-
**DSL Interface** - Provides clean syntax for attribute definitions
-
**Method Dispatch** - Routes type methods (string, integer, etc.) to attribute creation
-
**Helper Support** - Handles helper symbols in various positions
-
**Nesting Tracking** - Tracks nesting level for nested attributes
## DSL Usage
The builder enables this clean DSL syntax:
“‘ruby request do
object :user do
string :name
integer :age, default: 18
object :profile do
string :bio
end
end
end “‘
## Method Dispatch
### Type-based Methods When you call ‘string :name`, it routes through `method_missing`:
-
‘string` becomes the type
-
‘:name` becomes the attribute name
-
Calls ‘attribute(:name, :string, …)`
### Helper Position Handling Handles helpers in different positions:
“‘ruby string :required, :name # Helper first, then name string :name, :required # Name first, then helper “`
Both resolve to the same attribute definition.
## Nesting
Tracks nesting level for:
-
Validation (enforcing maximum nesting depth)
-
Error messages (showing context)
Maximum nesting level is configured in Treaty::Engine.config.
## Subclass Requirements
Subclasses must implement:
-
‘create_attribute` - Creates the appropriate attribute type (Request/Response)
## Architecture
Used by:
-
Request::Builder - For request attribute definitions
-
Response::Builder - For response attribute definitions
Direct Known Subclasses
Entity::Builder, Request::Attribute::Builder, Response::Attribute::Builder
Instance Attribute Summary collapse
-
#collection_of_attributes ⇒ Object
readonly
Returns the value of attribute collection_of_attributes.
-
#nesting_level ⇒ Object
readonly
Returns the value of attribute nesting_level.
Instance Method Summary collapse
-
#attribute(name, type, *helpers, **options, &block) ⇒ void
Defines an attribute with explicit type.
-
#initialize(collection_of_attributes, nesting_level) ⇒ Base
constructor
Creates a new builder instance.
-
#method_missing(type, name, *helpers, **options, &block) ⇒ void
Handles DSL methods like ‘string :name` where method name is the type.
-
#respond_to_missing?(name) ⇒ Boolean
Checks if method should be handled by method_missing.
Constructor Details
#initialize(collection_of_attributes, nesting_level) ⇒ Base
Creates a new builder instance
80 81 82 83 |
# File 'lib/treaty/attribute/builder/base.rb', line 80 def initialize(collection_of_attributes, nesting_level) @collection_of_attributes = collection_of_attributes @nesting_level = nesting_level end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(type, name, *helpers, **options, &block) ⇒ void
This method returns an undefined value.
Handles DSL methods like ‘string :name` where method name is the type
112 113 114 115 116 117 118 119 |
# File 'lib/treaty/attribute/builder/base.rb', line 112 def method_missing(type, name, *helpers, **, &block) if name.is_a?(Symbol) && HelperMapper.helper?(name) helpers.unshift(name) name = helpers.shift end attribute(name, type, *helpers, **, &block) end |
Instance Attribute Details
#collection_of_attributes ⇒ Object (readonly)
Returns the value of attribute collection_of_attributes.
73 74 75 |
# File 'lib/treaty/attribute/builder/base.rb', line 73 def collection_of_attributes @collection_of_attributes end |
#nesting_level ⇒ Object (readonly)
Returns the value of attribute nesting_level.
73 74 75 |
# File 'lib/treaty/attribute/builder/base.rb', line 73 def nesting_level @nesting_level end |
Instance Method Details
#attribute(name, type, *helpers, **options, &block) ⇒ void
This method returns an undefined value.
Defines an attribute with explicit type
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/treaty/attribute/builder/base.rb', line 93 def attribute(name, type, *helpers, **, &block) @collection_of_attributes << create_attribute( name, type, *helpers, nesting_level: @nesting_level, **, &block ) end |
#respond_to_missing?(name) ⇒ Boolean
Checks if method should be handled by method_missing
125 126 127 |
# File 'lib/treaty/attribute/builder/base.rb', line 125 def respond_to_missing?(name, *) super end |