Class: Castkit::Attributes::Definition

Inherits:
Object
  • Object
show all
Extended by:
Options
Defined in:
lib/castkit/attributes/definition.rb

Overview

Provides a class-based DSL for defining reusable attribute definitions.

Extend this class in a subclass of ‘Castkit::Attributes::Base` to define shared attribute settings that can be reused across multiple DataObjects.

Examples:

Defining a reusable attribute

class UuidDefinition < Castkit::Attributes::Base
  type :string
  required true
  format /\A[0-9a-f\-]{36}\z/
end

attribute :id, UuidDefinition.definition

Constant Summary

Constants included from Options

Options::ACCESS_MODES, Options::DEFAULTS

Class Method Summary collapse

Methods included from Options

access, composite, default, force_type, format, ignore, ignore_blank, ignore_nil, of, prefix, readonly, required, transient, type, unwrapped, validator

Class Method Details

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

Defines the attribute’s type and configuration using a DSL block.

Examples:

define :string, default: "none" do
  required true
  access [:read]
end

Parameters:

  • type (Symbol, Class<Castkit::DataObject>)

    the attribute type (e.g., :string, :integer)

  • options (Hash)

    additional options to merge after the block (e.g., default:, access:)

Yields:

  • DSL block used to set options like ‘required`, `format`, `readonly`, etc.

Returns:

  • (Array<(Symbol, Hash)>)

    a tuple of the final type and options hash



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/castkit/attributes/definition.rb', line 50

def define(type, **options, &block)
  @__castkit_attribute_dsl = true

  definition[:type] = type
  instance_eval(&block)
  definition[:options] = definition[:options].merge(options)

  definition
ensure
  @__castkit_attribute_dsl = false
end

.definitionHash

Returns the internal definition hash, containing the type and options.

Returns:

  • (Hash)

    the internal definition hash, containing the type and options



26
27
28
29
30
31
# File 'lib/castkit/attributes/definition.rb', line 26

def definition
  @definition ||= {
    type: nil,
    options: Castkit::Attributes::Options::DEFAULTS.dup
  }
end

.optionsHash

Returns the attribute options defined on this class.

Returns:

  • (Hash)

    the attribute options defined on this class



34
35
36
# File 'lib/castkit/attributes/definition.rb', line 34

def options
  definition[:options]
end