Module: Castkit::DataObjectExtensions::AttributeTypes

Included in:
Castkit::DataObject
Defined in:
lib/castkit/data_object_extensions/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.

Instance Method Summary collapse

Instance Method Details

#array(field, **options) ⇒ Object Also known as: collection

Defines an array attribute.

Parameters:

  • field (Symbol)
  • options (Hash)


61
62
63
# File 'lib/castkit/data_object_extensions/attribute_types.rb', line 61

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

#boolean(field, **options) ⇒ Object

Defines a boolean attribute.

Parameters:

  • field (Symbol)
  • options (Hash)


29
30
31
# File 'lib/castkit/data_object_extensions/attribute_types.rb', line 29

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

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

Defines a nested Castkit::DataObject attribute.

Parameters:

Raises:



79
80
81
82
83
84
85
# File 'lib/castkit/data_object_extensions/attribute_types.rb', line 79

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.

Parameters:

  • field (Symbol)
  • options (Hash)


45
46
47
# File 'lib/castkit/data_object_extensions/attribute_types.rb', line 45

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

#datetime(field, **options) ⇒ Object

Defines a datetime attribute.

Parameters:

  • field (Symbol)
  • options (Hash)


53
54
55
# File 'lib/castkit/data_object_extensions/attribute_types.rb', line 53

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

#float(field, **options) ⇒ Object

Defines a float attribute.

Parameters:

  • field (Symbol)
  • options (Hash)


37
38
39
# File 'lib/castkit/data_object_extensions/attribute_types.rb', line 37

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

#hash(field, **options) ⇒ Object

Defines a hash attribute.

Parameters:

  • field (Symbol)
  • options (Hash)


69
70
71
# File 'lib/castkit/data_object_extensions/attribute_types.rb', line 69

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

#integer(field, **options) ⇒ Object

Defines an integer attribute.

Parameters:

  • field (Symbol)
  • options (Hash)


21
22
23
# File 'lib/castkit/data_object_extensions/attribute_types.rb', line 21

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

#string(field, **options) ⇒ Object

Defines a string attribute.

Parameters:

  • field (Symbol)
  • options (Hash)


13
14
15
# File 'lib/castkit/data_object_extensions/attribute_types.rb', line 13

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.

Parameters:



94
95
96
# File 'lib/castkit/data_object_extensions/attribute_types.rb', line 94

def unwrapped(field, type, **options)
  attribute(field, type, **options, unwrapped: true)
end