Class: Cattri::AttributeCompiler

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

Overview

Responsible for defining methods on the target class/module based on the metadata in a Attribute.

This includes:

  • callable accessors (acting as both reader and writer)

  • predicate methods

  • explicit writers (‘:name=` methods)

Handles both instance and class-level attributes, including memoization and validation of default values for final attributes.

Class Method Summary collapse

Class Method Details

.define_accessor(attribute, context) ⇒ void

This method returns an undefined value.

Defines accessor methods for the given attribute in the provided context.

For ‘final` + `scope: :class` attributes, the default is eagerly assigned. Then, if permitted by `expose`, the reader, writer, and/or predicate methods are defined.

Parameters:



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cattri/attribute_compiler.rb', line 26

def define_accessor(attribute, context)
  if attribute.class_attribute? && attribute.final?
    value = attribute.evaluate_default
    context.storage_receiver_for(attribute) # steep:ignore
           .cattri_variable_set(attribute.ivar, value, final: attribute.final?) # steep:ignore
  end

  return if attribute.expose == :none

  define_accessor!(attribute, context)
  define_writer!(attribute, context) if attribute.writable?
  define_predicate!(attribute, context) if attribute.with_predicate?
end