Module: Apex::Utils::Configurable

Defined in:
lib/apex/configurable.rb

Overview

Mixin providing a simple registry of configurable extensions, modeled after Kramdown::Utils::Configurable.

Examples:

module Apex
  extend Apex::Utils::Configurable

  configurable :syntax_highlighter
end

Apex.add_syntax_highlighter(:rouge, ->(code, lang, opts) { ... })
Apex.syntax_highlighter(:rouge) # => registered callable

Instance Method Summary collapse

Instance Method Details

#configurable(name) ⇒ void

This method returns an undefined value.

Declare a configurable extension point.

This defines three singleton methods on the receiver:

  • configurables – a Hash-of-Hashes registry.

  • name(ext_name) – retrieve the registered data for ext_name.

  • add_name(ext_name, data = nil, &block) – register ext_name.

Parameters:

  • name (Symbol, String)

    the logical name of the configurable



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/apex/configurable.rb', line 28

def configurable(name)
  unless respond_to?(:configurables)
    singleton_class.send(:define_method, :configurables) do
      @_configurables ||= Hash.new { |h, k| h[k] = {} }
    end
  end

  singleton_class.send(:define_method, name) do |ext_name|
    configurables[name][ext_name]
  end

  singleton_class.send(:define_method, :"add_#{name}") do |ext_name, data = nil, &block|
    configurables[name][ext_name] = data || block
  end
end