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
-
#attribute(name) ⇒ Cattri::Attribute?
Returns the attribute definition for the given name.
-
#attribute_defined?(name) ⇒ Boolean
Returns true if the given attribute has been defined on this class or any ancestor.
-
#attribute_definitions(with_ancestors: false) ⇒ Hash{Symbol => Cattri::Attribute}
Returns a hash of attribute definitions defined on this class.
-
#attribute_methods ⇒ Hash{Symbol => Set<Symbol>}
Returns a hash of all methods defined by Cattri attributes.
-
#attribute_source(name) ⇒ Module?
Returns the original class or module where the given attribute was defined.
-
#attributes(with_ancestors: false) ⇒ Array<Symbol>
Returns a list of attribute names defined on this class.
Instance Method Details
#attribute(name) ⇒ Cattri::Attribute?
Returns the attribute definition for the given name.
Includes inherited definitions if available.
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.
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.
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_methods ⇒ Hash{Symbol => Set<Symbol>}
Returns a hash of all methods defined by Cattri attributes.
This includes accessors, writers, and predicates where applicable.
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.
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.
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 |