Module: Castkit::Core::Attributes
- Included in:
- DataObject
- Defined in:
- lib/castkit/core/attributes.rb
Overview
Provides DSL and implementation for declaring attributes within a Castkit::DataObject.
Includes support for regular, composite, transient, readonly/writeonly, and grouped attribute definitions.
Instance Method Summary collapse
-
#attribute(field, type, **options) ⇒ void
Declares an attribute with the given type and options.
-
#attributes ⇒ Hash{Symbol => Castkit::Attribute}
Returns all declared non-transient attributes.
-
#composite(field, type, **options, &block) ⇒ Object
(also: #property)
Declares a computed (composite) attribute.
-
#optional(**options) { ... } ⇒ void
Declares a group of optional attributes within the given block.
-
#readonly(**options) { ... } ⇒ void
Declares a group of readonly attributes within the given block.
-
#required(**options) { ... } ⇒ void
Declares a group of required attributes within the given block.
-
#transient { ... } ⇒ void
Declares a group of transient attributes within the given block.
-
#writeonly(**options) { ... } ⇒ void
Declares a group of writeonly attributes within the given block.
Instance Method Details
#attribute(field, type, **options) ⇒ void
This method returns an undefined value.
Declares an attribute with the given type and options.
If ‘:transient` is true, defines only standard accessors and skips serialization logic.
18 19 20 21 22 23 24 25 26 |
# File 'lib/castkit/core/attributes.rb', line 18 def attribute(field, type, **) field = field.to_sym raise Castkit::DataObjectError, "Attribute '#{field}' already defined" if attributes.key?(field) = () return define_attribute(field, type, **) unless [:transient] attr_accessor field end |
#attributes ⇒ Hash{Symbol => Castkit::Attribute}
Returns all declared non-transient attributes.
93 94 95 |
# File 'lib/castkit/core/attributes.rb', line 93 def attributes @attributes ||= {} end |
#composite(field, type, **options, &block) ⇒ Object Also known as: property
Declares a computed (composite) attribute.
The provided block defines the read behavior.
36 37 38 39 |
# File 'lib/castkit/core/attributes.rb', line 36 def composite(field, type, **, &block) attribute(field, type, **, composite: true) define_method(field, &block) end |
#optional(**options) { ... } ⇒ void
This method returns an undefined value.
Declares a group of optional attributes within the given block.
86 87 88 |
# File 'lib/castkit/core/attributes.rb', line 86 def optional(**, &block) with_required(false, , &block) end |
#readonly(**options) { ... } ⇒ void
This method returns an undefined value.
Declares a group of readonly attributes within the given block.
59 60 61 |
# File 'lib/castkit/core/attributes.rb', line 59 def readonly(**, &block) with_access([:read], , &block) end |
#required(**options) { ... } ⇒ void
This method returns an undefined value.
Declares a group of required attributes within the given block.
77 78 79 |
# File 'lib/castkit/core/attributes.rb', line 77 def required(**, &block) with_required(true, , &block) end |
#transient { ... } ⇒ void
This method returns an undefined value.
Declares a group of transient attributes within the given block.
These attributes are not serialized or included in ‘to_h`.
47 48 49 50 51 52 |
# File 'lib/castkit/core/attributes.rb', line 47 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 within the given block.
68 69 70 |
# File 'lib/castkit/core/attributes.rb', line 68 def writeonly(**, &block) with_access([:write], , &block) end |