Module: Castkit::Core::Attributes

Defined in:
lib/castkit/core/attributes.rb

Overview

Provides DSL and implementation for declaring attributes within a Castkit::DataObject.

Supports reusable attribute definitions, transient fields, composite readers, and grouped declarations such as readonly, optional, and transient blocks.

This module is included into Castkit::DataObject and handles attribute registration, accessor generation, and typed writing behavior.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object

rubocop:disable Metrics/ModuleLength



15
16
17
# File 'lib/castkit/core/attributes.rb', line 15

def self.extended(base)
  base.include(Cattri)
end

Instance Method Details

#attribute(field, type = nil, definition = nil, using: nil, **options) ⇒ void Also known as: attr

This method returns an undefined value.

Declares an attribute on the data object.

Accepts either inline options or a reusable attribute definition (using or definition). If :transient is true, defines only standard accessors and skips serialization logic.

Parameters:

  • field (Symbol)

    the attribute name

  • type (Symbol, Class) (defaults to: nil)

    the attribute’s declared type

  • definition (Hash, nil) (defaults to: nil)

    an optional pre-built definition object (‘{ type:, options: }`)

  • using (Castkit::Attributes::Base, nil) (defaults to: nil)

    an optional class-based definition (.definition)

  • options (Hash)

    additional options like default, access, required, etc.

Raises:



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

def attribute(field, type = nil, definition = nil, using: nil, **options)
  field = field.to_sym
  raise Castkit::DataObjectError, "Attribute '#{field}' already defined" if attributes.key?(field)

  type, options = use_definition(field, definition || using&.definition, type, options)
  return define_attribute(field, type, **options) unless options[:transient]

  define_transient_accessor(field)
end

#attributesHash{Symbol => Castkit::Attribute}

Returns all non-transient attributes defined on the class.

Returns:



105
106
107
# File 'lib/castkit/core/attributes.rb', line 105

def attributes
  cattri_variable_memoize(:__castkit_attributes_registry) { {} }
end

#composite(field, type, **options, &block) ⇒ void Also known as: property

This method returns an undefined value.

Declares a composite (computed) attribute.

Parameters:

  • field (Symbol)

    the name of the attribute

  • type (Symbol, Class)

    the attribute type

  • options (Hash)

    additional attribute options

Yield Returns:

  • (Object)

    the value to return when the reader is called



48
49
50
51
# File 'lib/castkit/core/attributes.rb', line 48

def composite(field, type, **options, &block)
  attribute(field, type, **options, composite: true)
  define_method(field, &block)
end

#inherited(subclass) ⇒ Object



109
110
111
112
113
114
# File 'lib/castkit/core/attributes.rb', line 109

def inherited(subclass)
  super

  parent_attributes = cattri_variable_get(:__castkit_attributes_registry)
  subclass.cattri_variable_set(:__castkit_attributes_registry, parent_attributes.dup) if parent_attributes
end

#optional(**options) { ... } ⇒ void

This method returns an undefined value.

Declares a group of optional attributes.

Parameters:

  • options (Hash)

    shared options for all attributes inside the block

Yields:

  • a block containing attribute calls



98
99
100
# File 'lib/castkit/core/attributes.rb', line 98

def optional(**options, &block)
  with_required(false, options, &block)
end

#readonly(**options) { ... } ⇒ void

This method returns an undefined value.

Declares a group of readonly attributes (accessible for read only).

Parameters:

  • options (Hash)

    shared options for all attributes inside the block

Yields:

  • a block containing attribute calls



71
72
73
# File 'lib/castkit/core/attributes.rb', line 71

def readonly(**options, &block)
  with_access([:read], options, &block)
end

#required(**options) { ... } ⇒ void

This method returns an undefined value.

Declares a group of required attributes.

Parameters:

  • options (Hash)

    shared options for all attributes inside the block

Yields:

  • a block containing attribute calls



89
90
91
# File 'lib/castkit/core/attributes.rb', line 89

def required(**options, &block)
  with_required(true, options, &block)
end

#transient { ... } ⇒ void

This method returns an undefined value.

Declares a group of transient attributes within a block.

These attributes are excluded from serialization (to_h) and not stored.

Yields:

  • a block containing attribute calls



59
60
61
62
63
64
# File 'lib/castkit/core/attributes.rb', line 59

def transient(&block)
  @__transient_context = true
  instance_eval(&block)
ensure
  @__transient_context = nil
end

#writeonly(**options) { ... } ⇒ void

This method returns an undefined value.

Declares a group of writeonly attributes (accessible for write only).

Parameters:

  • options (Hash)

    shared options for all attributes inside the block

Yields:

  • a block containing attribute calls



80
81
82
# File 'lib/castkit/core/attributes.rb', line 80

def writeonly(**options, &block)
  with_access([:write], options, &block)
end