Module: Cattri::Dsl
- Defined in:
- lib/cattri/dsl.rb
Overview
Provides the primary DSL for defining class-level and instance-level attributes.
This module is extended into any class or module that includes Cattri, and exposes methods like ‘cattri` and `final_cattri` for concise attribute declaration.
All attributes are defined through the underlying attribute registry and are associated with method accessors (reader, writer, predicate) based on options.
Instance Method Summary collapse
-
#cattri(name, value = nil, **options) { ... } ⇒ Array<Symbol>
Defines a new attribute with optional default, coercion, and visibility options.
-
#final_cattri(name, value, **options) { ... } ⇒ Array<Symbol>
Defines a write-once (final) attribute.
Instance Method Details
#cattri(name, value = nil, **options) { ... } ⇒ Array<Symbol>
Defines a new attribute with optional default, coercion, and visibility options.
The attribute can be defined with a static value, a lazy-evaluated block, or with additional options like ‘final`, `predicate`, or `expose`.
The attribute will be defined as either class-level or instance-level depending on the ‘scope:` option.
30 31 32 33 |
# File 'lib/cattri/dsl.rb', line 30 def cattri(name, value = nil, **, &block) = { visibility: __cattri_visibility }.merge() # steep:ignore attribute_registry.define_attribute(name, value, **, &block) # steep:ignore end |
#final_cattri(name, value, **options) { ... } ⇒ Array<Symbol>
Defines a write-once (final) attribute.
Final attributes can be written only once and raise on re-assignment. This is equivalent to ‘cattri(…, final: true)`.
50 51 52 |
# File 'lib/cattri/dsl.rb', line 50 def final_cattri(name, value, **, &block) cattri(name, value, **.merge(final: true), &block) # steep:ignore end |