Module: Cattri::DeferredAttributes
- Defined in:
- lib/cattri/deferred_attributes.rb
Overview
Provides support for defining attributes within a module that should be applied later to any class or module that includes or extends it.
This allows DSL modules to define Cattri attributes without prematurely applying them to themselves, deferring application to the including/extending context.
Defined Under Namespace
Modules: Hook
Class Method Summary collapse
-
.extended(base) ⇒ void
Hook into the module extension lifecycle to ensure deferred attributes are applied when the module is included or extended.
Instance Method Summary collapse
-
#apply_deferred_attributes(target) ⇒ void
Applies all deferred attributes to the target class or module.
-
#defer_attribute(attribute) ⇒ void
Registers an attribute to be applied later when this module is included or extended.
Class Method Details
.extended(base) ⇒ void
This method returns an undefined value.
Hook into the module extension lifecycle to ensure deferred attributes are applied when the module is included or extended.
17 18 19 20 21 |
# File 'lib/cattri/deferred_attributes.rb', line 17 def self.extended(base) return if base.singleton_class.ancestors.include?(Hook) base.singleton_class.prepend(Hook) end |
Instance Method Details
#apply_deferred_attributes(target) ⇒ void
This method returns an undefined value.
Applies all deferred attributes to the target class or module.
This is triggered automatically by the Hook on ‘included` or `extended`.
56 57 58 59 60 61 62 |
# File 'lib/cattri/deferred_attributes.rb', line 56 def apply_deferred_attributes(target) context = Cattri::Context.new(target) deferred_attributes.each_value do |attribute| Cattri::AttributeCompiler.define_accessor(attribute, context) end end |
#defer_attribute(attribute) ⇒ void
This method returns an undefined value.
Registers an attribute to be applied later when this module is included or extended.
46 47 48 |
# File 'lib/cattri/deferred_attributes.rb', line 46 def defer_attribute(attribute) deferred_attributes[attribute.name] = attribute end |