Class: Treaty::Entity::Attribute::Base
- Inherits:
-
Object
- Object
- Treaty::Entity::Attribute::Base
- 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
- 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 childrenarray- 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
Action::Request::Attribute::Attribute, Action::Response::Attribute::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 ⇒ Treaty::Entity::Attribute::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
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, **, &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.
46 47 48 |
# File 'lib/treaty/entity/attribute/base.rb', line 46 def name @name end |
#nesting_level ⇒ Object (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 |
#options ⇒ Object (readonly)
Returns the value of attribute options.
46 47 48 |
# File 'lib/treaty/entity/attribute/base.rb', line 46 def @options end |
#type ⇒ Object (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
112 113 114 |
# File 'lib/treaty/entity/attribute/base.rb', line 112 def array? @type == :array end |
#collection_of_attributes ⇒ Treaty::Entity::Attribute::Collection
Returns collection of nested attributes for this attribute
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
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
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
98 99 100 |
# File 'lib/treaty/entity/attribute/base.rb', line 98 def object_or_array? object? || array? end |