Class: Castkit::Attribute

Inherits:
Object
  • Object
show all
Includes:
Ext::Attribute::Access, Ext::Attribute::Options, Ext::Attribute::Validation
Defined in:
lib/castkit/attribute.rb

Overview

Represents a typed attribute on a Castkit::DataObject.

Provides casting, validation, access control, and serialization behavior.

Constant Summary

Constants included from Ext::Attribute::ErrorHandling

Ext::Attribute::ErrorHandling::ERROR_OPTIONS

Constants included from Ext::Attribute::Options

Ext::Attribute::Options::DEFAULT_OPTIONS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Ext::Attribute::Access

#access, #full_access?, #ignore?, #readable?, #skip_deserialization?, #skip_serialization?, #writeable?

Methods included from Ext::Attribute::Options

#alias_paths, #composite?, #dataobject?, #dataobject_collection?, #default, #ignore_blank?, #ignore_nil?, #key, #key_path, #optional?, #prefix, #required?, #transient?, #unwrapped?

Constructor Details

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

Initializes a new attribute definition.



32
33
34
35
36
37
38
39
# File 'lib/castkit/attribute.rb', line 32

def initialize(field, type, default: nil, **options)
  @field = field
  @type = normalize_type(type)
  @default = default
  @options = populate_options(options)

  validate!
end

Instance Attribute Details

#fieldSymbol (readonly)



18
19
20
# File 'lib/castkit/attribute.rb', line 18

def field
  @field
end

#optionsHash (readonly)



24
25
26
# File 'lib/castkit/attribute.rb', line 24

def options
  @options
end

#typeSymbol, ... (readonly)



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

def type
  @type
end

Instance Method Details

#to_hashHash Also known as: to_h

Returns a hash representation of the attribute definition.



44
45
46
47
48
49
50
51
# File 'lib/castkit/attribute.rb', line 44

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