Class: Castkit::Attribute
- Inherits:
-
Object
- Object
- Castkit::Attribute
- Includes:
- DSL::Attribute, Cattri
- Defined in:
- lib/castkit/attribute.rb
Overview
Represents a typed attribute on a ‘Castkit::DataObject`.
This class is responsible for:
-
Type normalization (symbol, class, or data object)
-
Default and option resolution
-
Validation hooks
-
Access and serialization control
Attributes are created automatically when using the DSL in ‘DataObject`, but can also be created manually or through reusable definitions.
Class Method Summary collapse
-
.define(type, **options) { ... } ⇒ Array<(Symbol, Hash)>
Defines a reusable attribute definition via a DSL wrapper.
-
.normalize_type(type) ⇒ Symbol, Class<Castkit::DataObject>
Normalizes a declared type (symbol, class, or array) for internal usage.
-
.process_type(type) ⇒ Symbol
Converts a raw type into a normalized symbol.
Instance Method Summary collapse
-
#initialize(field, type, default: nil, **options) ⇒ Attribute
constructor
Initializes a new attribute definition.
-
#to_hash ⇒ Hash
(also: #to_h)
Converts the attribute definition to a serializable hash.
Methods included from DSL::Attribute
Constructor Details
#initialize(field, type, default: nil, **options) ⇒ Attribute
Initializes a new attribute definition.
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/castkit/attribute.rb', line 85 def initialize(field, type, default: nil, **) super() cattri_variable_set(:field, field, final: true) cattri_variable_set(:type, self.class.normalize_type(type), final: true) @default = default cattri_variable_set(:options, (), final: true) validate! end |
Class Method Details
.define(type, **options) { ... } ⇒ Array<(Symbol, Hash)>
Defines a reusable attribute definition via a DSL wrapper.
39 40 41 42 |
# File 'lib/castkit/attribute.rb', line 39 def define(type, **, &block) normalized_type = normalize_type(type) Castkit::Attributes::Definition.define(normalized_type, **, &block) end |
.normalize_type(type) ⇒ Symbol, Class<Castkit::DataObject>
Normalizes a declared type (symbol, class, or array) for internal usage.
48 49 50 51 52 53 |
# File 'lib/castkit/attribute.rb', line 48 def normalize_type(type) return type.map { |t| normalize_type(t) } if type.is_a?(Array) return type if Castkit.dataobject?(type) process_type(type).to_sym end |
.process_type(type) ⇒ Symbol
Converts a raw type into a normalized symbol.
Recognized forms:
-
‘TrueClass`/`FalseClass` → `:boolean`
-
Class → ‘class.name.downcase.to_sym`
-
Symbol → passed through
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/castkit/attribute.rb', line 65 def process_type(type) case type when Class return :boolean if [TrueClass, FalseClass].include?(type) type.name.downcase.to_sym when Symbol type else raise Castkit::AttributeError, "Unknown type: #{type.inspect}" end end |
Instance Method Details
#to_hash ⇒ Hash Also known as: to_h
Converts the attribute definition to a serializable hash.
100 101 102 103 104 105 106 107 |
# File 'lib/castkit/attribute.rb', line 100 def to_hash { field: field, type: type, options: , default: default } end |