Class: Castkit::Attribute
- Inherits:
-
Object
- Object
- Castkit::Attribute
- Includes:
- Castkit::AttributeExtensions::Access, Castkit::AttributeExtensions::Casting, Castkit::AttributeExtensions::Options, Castkit::AttributeExtensions::Serialization, Castkit::AttributeExtensions::Validation
- Defined in:
- lib/castkit/attribute.rb
Overview
Represents a typed attribute on a Castkit::DataObject.
Provides casting, validation, access control, and serialization behavior.
Constant Summary
Constants included from Castkit::AttributeExtensions::ErrorHandling
Castkit::AttributeExtensions::ErrorHandling::ERROR_OPTIONS
Constants included from Castkit::AttributeExtensions::Casting
Castkit::AttributeExtensions::Casting::PRIMITIVE_CASTERS
Constants included from Castkit::AttributeExtensions::Options
Castkit::AttributeExtensions::Options::DEFAULT_OPTIONS
Instance Attribute Summary collapse
-
#field ⇒ Symbol
readonly
The attribute name.
-
#options ⇒ Hash
readonly
Attribute options (including aliases, default, access, etc.).
-
#type ⇒ Symbol, ...
readonly
The declared type (normalized).
Instance Method Summary collapse
-
#initialize(field, type, default: nil, **options) ⇒ Attribute
constructor
Initializes a new attribute definition.
-
#to_hash ⇒ Hash
(also: #to_h)
Returns a hash representation of the attribute definition.
Methods included from Castkit::AttributeExtensions::Serialization
Methods included from Castkit::AttributeExtensions::Access
#access, #full_access?, #ignore?, #readable?, #skip_deserialization?, #skip_serialization?, #writeable?
Methods included from Castkit::AttributeExtensions::Options
#alias_paths, #composite?, #dataobject?, #dataobject_collection?, #default, #ignore_blank?, #ignore_nil?, #key, #key_path, #optional?, #prefix, #required?, #unwrapped?
Constructor Details
#initialize(field, type, default: nil, **options) ⇒ Attribute
Initializes a new attribute definition.
37 38 39 40 41 42 43 44 |
# File 'lib/castkit/attribute.rb', line 37 def initialize(field, type, default: nil, **) @field = field @type = normalize_type(type) @default = default @options = () validate! end |
Instance Attribute Details
#field ⇒ Symbol (readonly)
Returns the attribute name.
23 24 25 |
# File 'lib/castkit/attribute.rb', line 23 def field @field end |
#options ⇒ Hash (readonly)
Returns attribute options (including aliases, default, access, etc.).
29 30 31 |
# File 'lib/castkit/attribute.rb', line 29 def @options end |
#type ⇒ Symbol, ... (readonly)
Returns the declared type (normalized).
26 27 28 |
# File 'lib/castkit/attribute.rb', line 26 def type @type end |
Instance Method Details
#to_hash ⇒ Hash Also known as: to_h
Returns a hash representation of the attribute definition.
49 50 51 52 53 54 55 56 |
# File 'lib/castkit/attribute.rb', line 49 def to_hash { field: field, type: type, options: , default: default } end |