Class: Treaty::Attribute::Builder::Base

Inherits:
Object
  • Object
show all
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

  1. **DSL Interface** - Provides clean syntax for attribute definitions

  2. **Method Dispatch** - Routes type methods (string, integer, etc.) to attribute creation

  3. **Helper Support** - Handles helper symbols in various positions

  4. **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`:

  1. ‘string` becomes the type

  2. ‘:name` becomes the attribute name

  3. 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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection_of_attributes, nesting_level) ⇒ Base

Creates a new builder instance

Parameters:

  • collection_of_attributes (Collection)

    Collection to add attributes to

  • nesting_level (Integer)

    Current nesting depth



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

Parameters:

  • type (Symbol)

    The attribute type (method name)

  • name (Symbol)

    The attribute name (first argument)

  • helpers (Array<Symbol>)

    Helper symbols

  • options (Hash)

    Attribute options

  • block (Proc)

    Block for nested attributes



112
113
114
115
116
117
118
119
# File 'lib/treaty/attribute/builder/base.rb', line 112

def method_missing(type, name, *helpers, **options, &block)
  if name.is_a?(Symbol) && HelperMapper.helper?(name)
    helpers.unshift(name)
    name = helpers.shift
  end

  attribute(name, type, *helpers, **options, &block)
end

Instance Attribute Details

#collection_of_attributesObject (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_levelObject (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

Parameters:

  • name (Symbol)

    The attribute name

  • type (Symbol)

    The attribute type

  • helpers (Array<Symbol>)

    Helper symbols (:required, :optional)

  • options (Hash)

    Attribute options

  • block (Proc)

    Block for nested attributes



93
94
95
96
97
98
99
100
101
102
# File 'lib/treaty/attribute/builder/base.rb', line 93

def attribute(name, type, *helpers, **options, &block)
  @collection_of_attributes << create_attribute(
    name,
    type,
    *helpers,
    nesting_level: @nesting_level,
    **options,
    &block
  )
end

#respond_to_missing?(name) ⇒ Boolean

Checks if method should be handled by method_missing

Parameters:

  • name (Symbol)

    Method name

Returns:

  • (Boolean)


125
126
127
# File 'lib/treaty/attribute/builder/base.rb', line 125

def respond_to_missing?(name, *)
  super
end