Class: Cattri::AttributeRegistry
- Inherits:
-
Object
- Object
- Cattri::AttributeRegistry
- 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
-
#context ⇒ Cattri::Context
readonly
The context this registry operates within.
Instance Method Summary collapse
-
#define_attribute(name, value, **options) {|*args| ... } ⇒ Array<Symbol>
Defines a new attribute and registers it on the current context.
-
#defined_attributes(with_ancestors: false) ⇒ Hash{Symbol => Cattri::Attribute}
Returns all known attributes, optionally including inherited definitions.
-
#fetch_attribute(name, with_ancestors: false) ⇒ Cattri::Attribute?
Fetches an attribute by name, or returns nil.
-
#fetch_attribute!(name, with_ancestors: false) ⇒ Cattri::Attribute
Fetches an attribute by name, or raises if not found.
-
#initialize(context) ⇒ AttributeRegistry
constructor
Initializes a new registry for the provided context.
-
#registered_attributes ⇒ Hash{Symbol => Cattri::Attribute}
Returns the attributes registered directly on this context.
Constructor Details
#initialize(context) ⇒ AttributeRegistry
Initializes a new registry for the provided context.
19 20 21 |
# File 'lib/cattri/attribute_registry.rb', line 19 def initialize(context) @context = context end |
Instance Attribute Details
#context ⇒ Cattri::Context (readonly)
Returns the context this registry operates within.
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.
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, **, &block) name = name.to_sym validate_unique!(name) = .merge(default: value) attribute = Cattri::Attribute.new( name, defined_in: context.target, **, &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.
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.
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.
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_attributes ⇒ Hash{Symbol => Cattri::Attribute}
Returns the attributes registered directly on this context.
26 27 28 |
# File 'lib/cattri/attribute_registry.rb', line 26 def registered_attributes (@__cattri_registered_attributes ||= {}).dup.freeze # steep:ignore end |