Module: Castkit::Core::Attributes
- Defined in:
- lib/castkit/core/attributes.rb
Overview
Provides DSL and implementation for declaring attributes within a Castkit::DataObject.
Supports reusable attribute definitions, transient fields, composite readers, and grouped declarations such as readonly, optional, and transient blocks.
This module is included into Castkit::DataObject and handles attribute registration, accessor generation, and typed writing behavior.
Class Method Summary collapse
-
.extended(base) ⇒ Object
rubocop:disable Metrics/ModuleLength.
Instance Method Summary collapse
-
#attribute(field, type = nil, definition = nil, using: nil, **options) ⇒ void
(also: #attr)
Declares an attribute on the data object.
-
#attributes ⇒ Hash{Symbol => Castkit::Attribute}
Returns all non-transient attributes defined on the class.
-
#composite(field, type, **options, &block) ⇒ void
(also: #property)
Declares a composite (computed) attribute.
- #inherited(subclass) ⇒ Object
-
#optional(**options) { ... } ⇒ void
Declares a group of optional attributes.
-
#readonly(**options) { ... } ⇒ void
Declares a group of readonly attributes (accessible for read only).
-
#required(**options) { ... } ⇒ void
Declares a group of required attributes.
-
#transient { ... } ⇒ void
Declares a group of transient attributes within a block.
-
#writeonly(**options) { ... } ⇒ void
Declares a group of writeonly attributes (accessible for write only).
Class Method Details
.extended(base) ⇒ Object
rubocop:disable Metrics/ModuleLength
15 16 17 |
# File 'lib/castkit/core/attributes.rb', line 15 def self.extended(base) base.include(Cattri) end |
Instance Method Details
#attribute(field, type = nil, definition = nil, using: nil, **options) ⇒ void Also known as: attr
This method returns an undefined value.
Declares an attribute on the data object.
Accepts either inline options or a reusable attribute definition (using or definition). If :transient is true, defines only standard accessors and skips serialization logic.
31 32 33 34 35 36 37 38 39 |
# File 'lib/castkit/core/attributes.rb', line 31 def attribute(field, type = nil, definition = nil, using: nil, **) field = field.to_sym raise Castkit::DataObjectError, "Attribute '#{field}' already defined" if attributes.key?(field) type, = use_definition(field, definition || using&.definition, type, ) return define_attribute(field, type, **) unless [:transient] define_transient_accessor(field) end |
#attributes ⇒ Hash{Symbol => Castkit::Attribute}
Returns all non-transient attributes defined on the class.
105 106 107 |
# File 'lib/castkit/core/attributes.rb', line 105 def attributes cattri_variable_memoize(:__castkit_attributes_registry) { {} } end |
#composite(field, type, **options, &block) ⇒ void Also known as: property
This method returns an undefined value.
Declares a composite (computed) attribute.
48 49 50 51 |
# File 'lib/castkit/core/attributes.rb', line 48 def composite(field, type, **, &block) attribute(field, type, **, composite: true) define_method(field, &block) end |
#inherited(subclass) ⇒ Object
109 110 111 112 113 114 |
# File 'lib/castkit/core/attributes.rb', line 109 def inherited(subclass) super parent_attributes = cattri_variable_get(:__castkit_attributes_registry) subclass.cattri_variable_set(:__castkit_attributes_registry, parent_attributes.dup) if parent_attributes end |
#optional(**options) { ... } ⇒ void
This method returns an undefined value.
Declares a group of optional attributes.
98 99 100 |
# File 'lib/castkit/core/attributes.rb', line 98 def optional(**, &block) with_required(false, , &block) end |
#readonly(**options) { ... } ⇒ void
This method returns an undefined value.
Declares a group of readonly attributes (accessible for read only).
71 72 73 |
# File 'lib/castkit/core/attributes.rb', line 71 def readonly(**, &block) with_access([:read], , &block) end |
#required(**options) { ... } ⇒ void
This method returns an undefined value.
Declares a group of required attributes.
89 90 91 |
# File 'lib/castkit/core/attributes.rb', line 89 def required(**, &block) with_required(true, , &block) end |
#transient { ... } ⇒ void
This method returns an undefined value.
Declares a group of transient attributes within a block.
These attributes are excluded from serialization (to_h) and not stored.
59 60 61 62 63 64 |
# File 'lib/castkit/core/attributes.rb', line 59 def transient(&block) @__transient_context = true instance_eval(&block) ensure @__transient_context = nil end |
#writeonly(**options) { ... } ⇒ void
This method returns an undefined value.
Declares a group of writeonly attributes (accessible for write only).
80 81 82 |
# File 'lib/castkit/core/attributes.rb', line 80 def writeonly(**, &block) with_access([:write], , &block) end |