module Toys::InputFile

This module is a namespace for constant scopes. Whenever a configuration file is parsed, a module is created under this parent for that file's constants.

Public Class Methods

__binding() click to toggle source

@private

# File lib/toys/input_file.rb, line 9
def self.__binding
  binding
end
build_eval_string(module_name, string) click to toggle source

@private

# File lib/toys/input_file.rb, line 37
def self.build_eval_string(module_name, string)
  index = string.index(/^\s*[^#\s]/)
  return nil if index.nil?
  "#{string[0, index]}\n" \
    "module #{module_name}\n" \
    "@__tool_class.class_eval do\n" \
    "#{string[index..-1]}\n" \
    "end\n" \
    "end\n"
end
evaluate(tool_class, words, priority, remaining_words, source, loader) click to toggle source

@private

# File lib/toys/input_file.rb, line 14
def self.evaluate(tool_class, words, priority, remaining_words, source, loader)
  namespace = ::Module.new
  namespace.module_eval do
    include ::Toys::Context::Key
    @__tool_class = tool_class
  end
  path = source.source_path
  basename = ::File.basename(path).tr(".-", "_").gsub(/\W/, "")
  name = "M#{namespace.object_id}_#{basename}"
  str = build_eval_string(name, ::IO.read(path))
  if str
    const_set(name, namespace)
    ::Toys::DSL::Internal.prepare(tool_class, words, priority, remaining_words, source, loader) do
      ::Toys::ContextualError.capture_path("Error while loading Toys config!", path) do
        # rubocop:disable Security/Eval
        eval(str, __binding, path, -2)
        # rubocop:enable Security/Eval
      end
    end
  end
end