Class: Cattri::AttributeRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/cattri/attribute_registry.rb

Overview

Cattri::AttributeRegistry is responsible for managing attribute definitions for a given context (class or module). It validates uniqueness, applies definition logic, and supports inheritance and introspection.

It handles both eager and deferred attribute compilation and ensures correct behavior for scope: :class, final: true, and other attribute options.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ AttributeRegistry

Initializes a new registry for the provided context.

Parameters:



19
20
21
# File 'lib/cattri/attribute_registry.rb', line 19

def initialize(context)
  @context = context
end

Instance Attribute Details

#contextCattri::Context (readonly)

Returns the context this registry operates within.

Returns:



14
15
16
# File 'lib/cattri/attribute_registry.rb', line 14

def context
  @context
end

Instance Method Details

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

Defines a new attribute and registers it on the current context.

Parameters:

  • name (String, Symbol)

    the attribute name

  • value (Object, Proc, nil)

    default value or initializer

  • options (Hash)

    attribute options (:class, :final, etc.)

Yields:

  • (*args)

    optional transformation block used as setter

Returns:

  • (Array<Symbol>)

    list of methods defined by this attribute

Raises:



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/cattri/attribute_registry.rb', line 74

def define_attribute(name, value, **options, &block)
  name = name.to_sym
  validate_unique!(name)

  options_with_default = options.merge(default: value)
  attribute = Cattri::Attribute.new(
    name,
    defined_in: context.target,
    **options_with_default,
    &block
  )

  register_attribute(attribute)
  attribute.allowed_methods
end

#defined_attributes(with_ancestors: false) ⇒ Hash{Symbol => Cattri::Attribute}

Returns all known attributes, optionally including inherited definitions.

Parameters:

  • with_ancestors (Boolean) (defaults to: false)

    whether to include ancestors

Returns:



34
35
36
37
38
39
40
41
42
43
# File 'lib/cattri/attribute_registry.rb', line 34

def defined_attributes(with_ancestors: false)
  return registered_attributes unless with_ancestors

  context.attribute_lookup_sources
         .select { |mod| mod.respond_to?(:attribute_registry, true) }
         .flat_map { |mod| mod.send(:attribute_registry).registered_attributes.to_a }
         .to_h
         .merge(registered_attributes)
         .freeze
end

#fetch_attribute(name, with_ancestors: false) ⇒ Cattri::Attribute?

Fetches an attribute by name, or returns nil.

Parameters:

  • name (String, Symbol)
  • with_ancestors (Boolean) (defaults to: false)

Returns:



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

def fetch_attribute(name, with_ancestors: false)
  defined_attributes(with_ancestors: with_ancestors)[name.to_sym]
end

#fetch_attribute!(name, with_ancestors: false) ⇒ Cattri::Attribute

Fetches an attribute by name, or raises if not found.

Parameters:

  • name (String, Symbol)
  • with_ancestors (Boolean) (defaults to: false)

Returns:

Raises:



60
61
62
63
64
# File 'lib/cattri/attribute_registry.rb', line 60

def fetch_attribute!(name, with_ancestors: false)
  defined_attributes(with_ancestors: with_ancestors).fetch(name.to_sym) do
    raise Cattri::AttributeError, "Attribute :#{name} has not been defined"
  end
end

#registered_attributesHash{Symbol => Cattri::Attribute}

Returns the attributes registered directly on this context.

Returns:



26
27
28
# File 'lib/cattri/attribute_registry.rb', line 26

def registered_attributes
  (@__cattri_registered_attributes ||= {}).dup.freeze # steep:ignore
end