Class: Treaty::Entity::Attribute::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/treaty/entity/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)



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

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.



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

def name
  @name
end

#nesting_levelObject (readonly)

Returns the value of attribute nesting_level.



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

def nesting_level
  @nesting_level
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#array?Boolean

Checks if this attribute is an array type

Returns:

  • (Boolean)

    True if type is :array



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

def array?
  @type == :array
end

#collection_of_attributesTreaty::Entity::Attribute::Collection

Returns collection of nested attributes for this attribute

Returns:



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

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



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

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



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

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



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

def object_or_array?
  object? || array?
end