Module: RVGP::Application::DescendantRegistry::ClassMethods

Defined in:
lib/rvgp/application/descendant_registry.rb

Overview

This module defines the parent’s class methods, which are attached to a parent class, at the time it includes the DescendantRegistry

Instance Method Summary collapse

Instance Method Details

#nameString

The name of this base class, after applying its to_s to :name_capture, and underscore’izing the capture

Returns:

  • (String)

    A string that can be used for various metaprogramming requirements of this DescendantRegistry



105
106
107
108
109
110
111
# File 'lib/rvgp/application/descendant_registry.rb', line 105

def name
  name_capture = superclass.descendant_registry[:name_capture]
  name = name_capture.match(to_s) ? ::Regexp.last_match(1) : to_s

  # underscorize the capture:
  name.scan(/[A-Z][^A-Z]*/).join('_').downcase
end

#register_descendants(in_klass, with_name, opts = {}) ⇒ Object

This method is the main entrypoint for all of the descendent registry features. This method installs a registry, into the provided namespace, given the provided options

Parameters:

  • in_klass (Object)

    This is class, under which, this registry will be created

  • with_name (Symbol)

    The name of the registry, which will be the name of the reader, created in in_klass

  • opts (Hash) (defaults to: {})

    what options to configure this registry with

Options Hash (opts):

  • :accessors (Hash<String, Proc>)

    A list of public methods to create, alongside their implementation,in the base of the newly created collection.

  • :name_capture (Regexp)

    This regex is expected to contain a single capture, that will be used to construct a class name, given a classes #to_s output, and before sending to underscorize.



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rvgp/application/descendant_registry.rb', line 88

def register_descendants(in_klass, with_name, opts = {})
  @descendant_registry = { klass: in_klass,
                           name: with_name,
                           name_capture: opts.key?(:name_capture) ? opts[:name_capture] : /\A.*:(.+)\Z/ }
  define_singleton_method(:descendant_registry) { @descendant_registry }

  in_klass.instance_eval do
    iv_sym = "@#{with_name}".to_sym
    instance_variable_set iv_sym, ClassRegistry.new(opts)
    define_singleton_method(with_name) { instance_variable_get iv_sym }
  end
end