Module: Cattri::Introspection::ClassMethods

Defined in:
lib/cattri/introspection.rb

Overview

Class-level introspection methods exposed via ‘.attribute_defined?`, `.attribute`, etc.

Instance Method Summary collapse

Instance Method Details

#attribute(name) ⇒ Cattri::Attribute?

Returns the attribute definition for the given name.

Includes inherited definitions if available.

Parameters:

  • name (Symbol, String)

    the attribute name

Returns:



35
36
37
# File 'lib/cattri/introspection.rb', line 35

def attribute(name)
  attribute_registry.defined_attributes(with_ancestors: true)[name.to_sym] # steep:ignore
end

#attribute_defined?(name) ⇒ Boolean

Returns true if the given attribute has been defined on this class or any ancestor.

Parameters:

  • name (Symbol, String)

    the attribute name

Returns:

  • (Boolean)


25
26
27
# File 'lib/cattri/introspection.rb', line 25

def attribute_defined?(name)
  !!attribute(name)
end

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

Returns a hash of attribute definitions defined on this class.

Includes inherited attributes if ‘with_ancestors` is true.

Parameters:

  • with_ancestors (Boolean) (defaults to: false)

Returns:



55
56
57
# File 'lib/cattri/introspection.rb', line 55

def attribute_definitions(with_ancestors: false)
  attribute_registry.defined_attributes(with_ancestors: with_ancestors) # steep:ignore
end

#attribute_methodsHash{Symbol => Set<Symbol>}

Returns a hash of all methods defined by Cattri attributes.

This includes accessors, writers, and predicates where applicable.

Returns:

  • (Hash{Symbol => Set<Symbol>})


64
65
66
# File 'lib/cattri/introspection.rb', line 64

def attribute_methods
  context.defined_methods # steep:ignore
end

#attribute_source(name) ⇒ Module?

Returns the original class or module where the given attribute was defined.

Parameters:

  • name (Symbol, String)

    the attribute name

Returns:

  • (Module, nil)


72
73
74
# File 'lib/cattri/introspection.rb', line 72

def attribute_source(name)
  attribute(name)&.defined_in
end

#attributes(with_ancestors: false) ⇒ Array<Symbol>

Returns a list of attribute names defined on this class.

Includes inherited attributes if ‘with_ancestors` is true.

Parameters:

  • with_ancestors (Boolean) (defaults to: false)

Returns:

  • (Array<Symbol>)


45
46
47
# File 'lib/cattri/introspection.rb', line 45

def attributes(with_ancestors: false)
  attribute_registry.defined_attributes(with_ancestors: with_ancestors).keys # steep:ignore
end