Class: Treaty::Attribute::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/treaty/attribute/base.rb

Overview

Base class for all attribute definitions in Treaty DSL.

## Purpose

Represents a single attribute defined in request/response definitions. Handles:

  • Attribute metadata (name, type, nesting level)

  • Helper mode to simple mode conversion

  • Simple mode to advanced mode normalization

  • Nested attributes (for object and array types)

## Usage

Attributes are created through DSL methods:

string :title, :required
integer :age, default: 18
object :author do
  string :name
end

## Processing Flow

  1. Extract helpers from arguments (‘:required`, `:optional`)

  2. Convert helpers to simple mode options

  3. Merge with explicit options

  4. Normalize all options to advanced mode

  5. Apply defaults (required: true for request, false for response)

  6. Process nested attributes if block given

## Nested Attributes

Object and array types can have nested attributes:

  • ‘object` - nested attributes as direct children

  • ‘array` - nested attributes define array element structure

Special attribute name ‘:_self` is used for simple arrays:

array :tags do
  string :_self  # Array of strings
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, *helpers, nesting_level: 0, **options, &block) ⇒ Base

Creates a new attribute instance

Parameters:

  • name (Symbol)

    The attribute name

  • type (Symbol)

    The attribute type (:string, :integer, :object, :array, etc.)

  • helpers (Array<Symbol>)

    Helper symbols (:required, :optional)

  • nesting_level (Integer) (defaults to: 0)

    Current nesting depth (default: 0)

  • options (Hash)

    Attribute options (required, default, as, etc.)

  • block (Proc)

    Block for defining nested attributes (for object/array types)



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/treaty/attribute/base.rb', line 58

def initialize(name, type, *helpers, nesting_level: 0, **options, &block)
  @name = name
  @type = type
  @nesting_level = nesting_level

  validate_nesting_level!

  # Separate helpers from non-helper symbols.
  @helpers = extract_helpers(helpers)

  # Merge helper options with explicit options.
  merged_options = merge_options(@helpers, options)

  # Normalize all options to advanced mode.
  @options = OptionNormalizer.normalize(merged_options)

  apply_defaults!

  # Process nested attributes for object and array types.
  process_nested_attributes(&block) if block_given?
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



45
46
47
# File 'lib/treaty/attribute/base.rb', line 45

def name
  @name
end

#nesting_levelObject (readonly)

Returns the value of attribute nesting_level.



45
46
47
# File 'lib/treaty/attribute/base.rb', line 45

def nesting_level
  @nesting_level
end

#optionsObject (readonly)

Returns the value of attribute options.



45
46
47
# File 'lib/treaty/attribute/base.rb', line 45

def options
  @options
end

#typeObject (readonly)

Returns the value of attribute type.



45
46
47
# File 'lib/treaty/attribute/base.rb', line 45

def type
  @type
end

Instance Method Details

#array?Boolean

Checks if this attribute is an array type

Returns:

  • (Boolean)

    True if type is :array



111
112
113
# File 'lib/treaty/attribute/base.rb', line 111

def array?
  @type == :array
end

#collection_of_attributesCollection

Returns collection of nested attributes for this attribute

Returns:

  • (Collection)

    Collection of nested attributes



83
84
85
# File 'lib/treaty/attribute/base.rb', line 83

def collection_of_attributes
  @collection_of_attributes ||= Collection.new
end

#nested?Boolean

Checks if this attribute has nested attributes

Returns:

  • (Boolean)

    True if attribute is object/array with nested attributes



90
91
92
# File 'lib/treaty/attribute/base.rb', line 90

def nested?
  object_or_array? && collection_of_attributes.exists?
end

#object?Boolean

Checks if this attribute is an object type

Returns:

  • (Boolean)

    True if type is :object



104
105
106
# File 'lib/treaty/attribute/base.rb', line 104

def object?
  @type == :object
end

#object_or_array?Boolean

Checks if this attribute is an object or array type

Returns:

  • (Boolean)

    True if type is :object or :array



97
98
99
# File 'lib/treaty/attribute/base.rb', line 97

def object_or_array?
  object? || array?
end