Module: Cattri::Dsl

Defined in:
lib/cattri/dsl.rb

Overview

Provides the primary DSL for defining class-level and instance-level attributes.

This module is extended into any class or module that includes Cattri, and exposes methods like ‘cattri` and `final_cattri` for concise attribute declaration.

All attributes are defined through the underlying attribute registry and are associated with method accessors (reader, writer, predicate) based on options.

Instance Method Summary collapse

Instance Method Details

#cattri(name, value = nil, **options) { ... } ⇒ Array<Symbol>

Defines a new attribute with optional default, coercion, and visibility options.

The attribute can be defined with a static value, a lazy-evaluated block, or with additional options like ‘final`, `predicate`, or `expose`.

The attribute will be defined as either class-level or instance-level depending on the ‘scope:` option.

Parameters:

  • name (Symbol, String)

    the attribute name

  • value (Object, nil) (defaults to: nil)

    optional static value or default

  • options (Hash)

    additional attribute configuration

Options Hash (**options):

  • :scope (Boolean)

    whether this is a class-level (:class) or instance-level (:instance) attribute

  • :final (Boolean)

    whether the attribute is write-once

  • :predicate (Boolean)

    whether to define a predicate method

  • :expose (Symbol)

    whether to expose ‘:read`, `:write`, `:read_write`, or `:none`

  • :visibility (Symbol)

    the visibility for generated methods (‘:public`, `:protected`, `:private`)

Yields:

  • optional block to lazily evaluate the attribute’s default value

Returns:

  • (Array<Symbol>)

    the defined methods



30
31
32
33
# File 'lib/cattri/dsl.rb', line 30

def cattri(name, value = nil, **options, &block)
  options = { visibility: __cattri_visibility }.merge(options) # steep:ignore
  attribute_registry.define_attribute(name, value, **options, &block) # steep:ignore
end

#final_cattri(name, value, **options) { ... } ⇒ Array<Symbol>

Defines a write-once (final) attribute.

Final attributes can be written only once and raise on re-assignment. This is equivalent to ‘cattri(…, final: true)`.

Parameters:

  • name (Symbol, String)

    the attribute name

  • value (Object, nil)

    static or lazy default value

  • options (Hash)

    additional attribute configuration

Options Hash (**options):

  • :scope (Boolean)

    whether this is a class-level (:class) or instance-level (:instance) attribute

  • :final (Boolean)

    whether the attribute is write-once

  • :predicate (Boolean)

    whether to define a predicate method

  • :expose (Symbol)

    whether to expose ‘:read`, `:write`, `:read_write`, or `:none`

  • :visibility (Symbol)

    the visibility for generated methods (‘:public`, `:protected`, `:private`)

Yields:

  • optional block to lazily evaluate the default

Returns:

  • (Array<Symbol>)

    the defined methods



50
51
52
# File 'lib/cattri/dsl.rb', line 50

def final_cattri(name, value, **options, &block)
  cattri(name, value, **options.merge(final: true), &block) # steep:ignore
end