Class: Castkit::Attribute
- Inherits:
-
Object
- Object
- Castkit::Attribute
- 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 Ext::Attribute::ErrorHandling
Ext::Attribute::ErrorHandling::ERROR_OPTIONS
Constants included from Ext::Attribute::Options
Ext::Attribute::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 Ext::Attribute::Access
#access, #full_access?, #ignore?, #readable?, #skip_deserialization?, #skip_serialization?, #writeable?
Methods included from Ext::Attribute::Options
#alias_paths, #composite?, #dataobject?, #dataobject_collection?, #default, #ignore_blank?, #ignore_nil?, #key, #key_path, #optional?, #prefix, #required?, #transient?, #unwrapped?
Constructor Details
#initialize(field, type, default: nil, **options) ⇒ Attribute
Initializes a new attribute definition.
32 33 34 35 36 37 38 39 |
# File 'lib/castkit/attribute.rb', line 32 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.
18 19 20 |
# File 'lib/castkit/attribute.rb', line 18 def field @field end |
#options ⇒ Hash (readonly)
Returns attribute options (including aliases, default, access, etc.).
24 25 26 |
# File 'lib/castkit/attribute.rb', line 24 def @options end |
#type ⇒ Symbol, ... (readonly)
Returns the declared type (normalized).
21 22 23 |
# File 'lib/castkit/attribute.rb', line 21 def type @type end |
Instance Method Details
#to_hash ⇒ Hash Also known as: to_h
Returns a hash representation of the attribute definition.
44 45 46 47 48 49 50 51 |
# File 'lib/castkit/attribute.rb', line 44 def to_hash { field: field, type: type, options: , default: default } end |