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

Instance Method Summary collapse

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, **options|
      attribute(field, type.to_sym, **options)
    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, **options)
  attribute(field, :array, **options)
end

#boolean(field, **options) ⇒ Object

Defines a boolean attribute.



49
50
51
# File 'lib/castkit/core/attribute_types.rb', line 49

def boolean(field, **options)
  attribute(field, :boolean, **options)
end

#dataobject(field, type, **options) ⇒ Object Also known as: dto

Defines a nested Castkit::DataObject attribute.

Raises:



99
100
101
102
103
104
105
# File 'lib/castkit/core/attribute_types.rb', line 99

def dataobject(field, type, **options)
  unless type < Castkit::DataObject
    raise Castkit::AttributeError, "Data objects must extend from Castkit::DataObject"
  end

  attribute(field, type, **options)
end

#date(field, **options) ⇒ Object

Defines a date attribute.



65
66
67
# File 'lib/castkit/core/attribute_types.rb', line 65

def date(field, **options)
  attribute(field, :date, **options)
end

#datetime(field, **options) ⇒ Object

Defines a datetime attribute.



73
74
75
# File 'lib/castkit/core/attribute_types.rb', line 73

def datetime(field, **options)
  attribute(field, :datetime, **options)
end

#float(field, **options) ⇒ Object

Defines a float attribute.



57
58
59
# File 'lib/castkit/core/attribute_types.rb', line 57

def float(field, **options)
  attribute(field, :float, **options)
end

#hash(field, **options) ⇒ Object

Defines a hash attribute.



89
90
91
# File 'lib/castkit/core/attribute_types.rb', line 89

def hash(field, **options)
  attribute(field, :hash, **options)
end

#integer(field, **options) ⇒ Object

Defines an integer attribute.



41
42
43
# File 'lib/castkit/core/attribute_types.rb', line 41

def integer(field, **options)
  attribute(field, :integer, **options)
end

#string(field, **options) ⇒ Object

Defines a string attribute.



33
34
35
# File 'lib/castkit/core/attribute_types.rb', line 33

def string(field, **options)
  attribute(field, :string, **options)
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, **options)
  attribute(field, type, **options, unwrapped: true)
end