Class: Toys::StandardMiddleware::ApplyConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/toys/standard_middleware/apply_config.rb

Overview

A middleware that applies the given block to all tool configurations.

Instance Method Summary collapse

Constructor Details

#initialize(parent_source: nil, source_name: nil, &block) ⇒ ApplyConfig

Create an ApplyConfig middleware.

This middleware works in one of two modes:

  1. Tied to a provided parent_source. This is generally used when the middleware is invoked during tool loading, e.g. via the DSL::Tool#subtool_apply DSL directive. The block will be associated with a new "proc" source under the given parent, and will apply only to tools from the same source tree (i.e. the same priority.)
  2. Not tied to a source, i.e. the no parent_source provided. This is generally used when the middleware is installed globally in the loader. The block will be applied to every tool, regardless of the tool's source, and will be associated with a separate "proc" source for every tool source tree (i.e. every priority).

Your block cannot create or modify subtools of the tool being modified. (This is because the same middleware could be expected to apply to that subtool as well, and the semantics of the resulting recursion would be messy and ambiguous.) Thus, within the block:

  • You cannot use the tool directive.
  • You cannot create a Toys::Tool subclass.
  • You cannot use a subtool_apply directive.
  • You cannot pass the as: parameter to any load or related directive.
  • You cannot use any load or related directive that references a directory. (You can, however, load a file directly, as long as it does not use the as: parameter, and the file's contents do not violate any of these rules.)

Parameters:

  • parent_source (Toys::SourceInfo, nil) (defaults to: nil)

    The SourceInfo corresponding to the source where this block is provided, to apply this block only to that source tree. Or, omit to apply this block to every tool regardless of source.

  • source_name (String) (defaults to: nil)

    A user-visible name for the source, or nil to use the default.

  • block (Proc)

    The configuration to apply.



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/toys/standard_middleware/apply_config.rb', line 49

def initialize(parent_source: nil, source_name: nil, &block)
  @block = block
  if parent_source
    @source = parent_source.proc_child(block, source_name: source_name)
    @mutex = @sources_by_root = @source_name = nil
  else
    @source_name = source_name
    @sources_by_root = {}
    @mutex = ::Mutex.new
    @source = nil
  end
end