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.

Parameters:

  • field (Symbol)

    the name of the attribute

  • type (Symbol, Class, Array)

    the type or array of types

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

    optional default value

  • options (Hash)

    additional configuration options



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)

Returns the attribute name.

Returns:

  • (Symbol)

    the attribute name



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

def field
  @field
end

#optionsHash (readonly)

Returns attribute options (including aliases, default, access, etc.).

Returns:

  • (Hash)

    attribute options (including aliases, default, access, etc.)



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

def options
  @options
end

#typeSymbol, ... (readonly)

Returns the declared type (normalized).

Returns:

  • (Symbol, Class, Array)

    the declared type (normalized)



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.

Returns:

  • (Hash)


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