Class: Castkit::Attribute

Inherits:
Object
  • Object
show all
Includes:
DSL::Attribute, Cattri
Defined in:
lib/castkit/attribute.rb

Overview

Represents a typed attribute on a ‘Castkit::DataObject`.

This class is responsible for:

  • Type normalization (symbol, class, or data object)

  • Default and option resolution

  • Validation hooks

  • Access and serialization control

Attributes are created automatically when using the DSL in ‘DataObject`, but can also be created manually or through reusable definitions.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DSL::Attribute

included

Constructor Details

#initialize(field, type, default: nil, **options) ⇒ Attribute

Initializes a new attribute definition.

Parameters:

  • field (Symbol)

    the attribute name

  • type (Symbol, Class, Array<Symbol, Class>)

    the type (or list of types)

  • default (Object, Proc, nil) (defaults to: nil)

    optional static or callable default

  • options (Hash)

    additional attribute options



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/castkit/attribute.rb', line 85

def initialize(field, type, default: nil, **options)
  super()

  cattri_variable_set(:field, field, final: true)
  cattri_variable_set(:type, self.class.normalize_type(type), final: true)

  @default = default
  cattri_variable_set(:options, populate_options(options), final: true)

  validate!
end

Class Method Details

.define(type, **options) { ... } ⇒ Array<(Symbol, Hash)>

Defines a reusable attribute definition via a DSL wrapper.

Parameters:

  • type (Symbol, Class)

    The base type to define.

  • options (Hash)

    Additional attribute options.

Yields:

  • The block to configure options or transformations.

Returns:

  • (Array<(Symbol, Hash)>)

    a tuple of the final type and options hash



39
40
41
42
# File 'lib/castkit/attribute.rb', line 39

def define(type, **options, &block)
  normalized_type = normalize_type(type)
  Castkit::Attributes::Definition.define(normalized_type, **options, &block)
end

.normalize_type(type) ⇒ Symbol, Class<Castkit::DataObject>

Normalizes a declared type (symbol, class, or array) for internal usage.

Parameters:

  • type (Symbol, Class, Array)

    the input type

Returns:



48
49
50
51
52
53
# File 'lib/castkit/attribute.rb', line 48

def normalize_type(type)
  return type.map { |t| normalize_type(t) } if type.is_a?(Array)
  return type if Castkit.dataobject?(type)

  process_type(type).to_sym
end

.process_type(type) ⇒ Symbol

Converts a raw type into a normalized symbol.

Recognized forms:

  • ‘TrueClass`/`FalseClass` → `:boolean`

  • Class → ‘class.name.downcase.to_sym`

  • Symbol → passed through

Parameters:

  • type (Symbol, Class)

    the type to convert

Returns:

  • (Symbol)

    normalized type symbol

Raises:



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/castkit/attribute.rb', line 65

def process_type(type)
  case type
  when Class
    return :boolean if [TrueClass, FalseClass].include?(type)

    type.name.downcase.to_sym
  when Symbol
    type
  else
    raise Castkit::AttributeError, "Unknown type: #{type.inspect}"
  end
end

Instance Method Details

#to_hashHash Also known as: to_h

Converts the attribute definition to a serializable hash.

Returns:

  • (Hash)

    the full attribute metadata



100
101
102
103
104
105
106
107
# File 'lib/castkit/attribute.rb', line 100

def to_hash
  {
    field: field,
    type: type,
    options: options,
    default: default
  }
end