Module: Castkit::Core::AttributeTypes
- Included in:
- Castkit::Contract::Base, DataObject
- Defined in:
- lib/castkit/core/attribute_types.rb
Overview
Provides DSL methods to define typed attributes in a Castkit::DataObject.
These helpers are shortcuts for calling ‘attribute` with a specific type.
Class Method Summary collapse
-
.define_type_dsl(type) ⇒ void
Dynamically defines a DSL method for a registered type.
-
.included(base) ⇒ Object
Inclusion hook: ensures config is loaded and extends the including class with DSL.
Instance Method Summary collapse
-
#array(field, **options) ⇒ Object
Defines an array attribute.
-
#boolean(field, **options) ⇒ Object
Defines a boolean attribute.
-
#dataobject(field, type, **options) ⇒ Object
(also: #dto)
Defines a nested Castkit::DataObject attribute.
-
#date(field, **options) ⇒ Object
Defines a date attribute.
-
#datetime(field, **options) ⇒ Object
Defines a datetime attribute.
-
#float(field, **options) ⇒ Object
Defines a float attribute.
-
#hash(field, **options) ⇒ Object
Defines a hash attribute.
-
#integer(field, **options) ⇒ Object
Defines an integer attribute.
-
#string(field, **options) ⇒ Object
Defines a string attribute.
-
#unwrapped(field, type, **options) ⇒ Object
Defines an unwrapped nested Castkit::DataObject attribute.
Class Method Details
.define_type_dsl(type) ⇒ void
This method returns an undefined value.
Dynamically defines a DSL method for a registered type.
20 21 22 23 24 25 26 |
# File 'lib/castkit/core/attribute_types.rb', line 20 def define_type_dsl(type) AttributeTypes.module_eval do define_method(type) do |field, **| attribute(field, type.to_sym, **) end end end |
.included(base) ⇒ Object
Inclusion hook: ensures config is loaded and extends the including class with DSL
10 11 12 13 |
# File 'lib/castkit/core/attribute_types.rb', line 10 def self.included(base) Castkit.configuration # triggers type/alias registration base.extend(self) end |
Instance Method Details
#array(field, **options) ⇒ Object
Defines an array attribute.
81 82 83 |
# File 'lib/castkit/core/attribute_types.rb', line 81 def array(field, **) attribute(field, :array, **) end |
#boolean(field, **options) ⇒ Object
Defines a boolean attribute.
49 50 51 |
# File 'lib/castkit/core/attribute_types.rb', line 49 def boolean(field, **) attribute(field, :boolean, **) end |
#dataobject(field, type, **options) ⇒ Object Also known as: dto
Defines a nested Castkit::DataObject attribute.
99 100 101 102 103 104 105 |
# File 'lib/castkit/core/attribute_types.rb', line 99 def dataobject(field, type, **) unless type < Castkit::DataObject raise Castkit::AttributeError, "Data objects must extend from Castkit::DataObject" end attribute(field, type, **) end |
#date(field, **options) ⇒ Object
Defines a date attribute.
65 66 67 |
# File 'lib/castkit/core/attribute_types.rb', line 65 def date(field, **) attribute(field, :date, **) end |
#datetime(field, **options) ⇒ Object
Defines a datetime attribute.
73 74 75 |
# File 'lib/castkit/core/attribute_types.rb', line 73 def datetime(field, **) attribute(field, :datetime, **) end |
#float(field, **options) ⇒ Object
Defines a float attribute.
57 58 59 |
# File 'lib/castkit/core/attribute_types.rb', line 57 def float(field, **) attribute(field, :float, **) end |
#hash(field, **options) ⇒ Object
Defines a hash attribute.
89 90 91 |
# File 'lib/castkit/core/attribute_types.rb', line 89 def hash(field, **) attribute(field, :hash, **) end |
#integer(field, **options) ⇒ Object
Defines an integer attribute.
41 42 43 |
# File 'lib/castkit/core/attribute_types.rb', line 41 def integer(field, **) attribute(field, :integer, **) end |
#string(field, **options) ⇒ Object
Defines a string attribute.
33 34 35 |
# File 'lib/castkit/core/attribute_types.rb', line 33 def string(field, **) attribute(field, :string, **) end |
#unwrapped(field, type, **options) ⇒ Object
Defines an unwrapped nested Castkit::DataObject attribute.
All keys from this object will be flattened with an optional prefix.
114 115 116 |
# File 'lib/castkit/core/attribute_types.rb', line 114 def unwrapped(field, type, **) attribute(field, type, **, unwrapped: true) end |