Class: Treaty::Attribute::Base
- Inherits:
-
Object
- Object
- Treaty::Attribute::Base
- 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
-
Extract helpers from arguments (‘:required`, `:optional`)
-
Convert helpers to simple mode options
-
Merge with explicit options
-
Normalize all options to advanced mode
-
Apply defaults (required: true for request, false for response)
-
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
Direct Known Subclasses
Entity::Attribute, Request::Attribute::Attribute, Response::Attribute::Attribute
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#nesting_level ⇒ Object
readonly
Returns the value of attribute nesting_level.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#array? ⇒ Boolean
Checks if this attribute is an array type.
-
#collection_of_attributes ⇒ Collection
Returns collection of nested attributes for this attribute.
-
#initialize(name, type, *helpers, nesting_level: 0, **options, &block) ⇒ Base
constructor
Creates a new attribute instance.
-
#nested? ⇒ Boolean
Checks if this attribute has nested attributes.
-
#object? ⇒ Boolean
Checks if this attribute is an object type.
-
#object_or_array? ⇒ Boolean
Checks if this attribute is an object or array type.
Constructor Details
#initialize(name, type, *helpers, nesting_level: 0, **options, &block) ⇒ Base
Creates a new attribute instance
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, **, &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. = (@helpers, ) # Normalize all options to advanced mode. @options = OptionNormalizer.normalize() apply_defaults! # Process nested attributes for object and array types. process_nested_attributes(&block) if block_given? end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
45 46 47 |
# File 'lib/treaty/attribute/base.rb', line 45 def name @name end |
#nesting_level ⇒ Object (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 |
#options ⇒ Object (readonly)
Returns the value of attribute options.
45 46 47 |
# File 'lib/treaty/attribute/base.rb', line 45 def @options end |
#type ⇒ Object (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
111 112 113 |
# File 'lib/treaty/attribute/base.rb', line 111 def array? @type == :array end |
#collection_of_attributes ⇒ Collection
Returns collection of nested attributes for this attribute
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
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
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
97 98 99 |
# File 'lib/treaty/attribute/base.rb', line 97 def object_or_array? object? || array? end |