module Modulation

Implements main Modulation functionality

Constants

CALLER_RANGE
DIR
GEM_REQUIRE_ERROR_MESSAGE
RE_CONST
VERSION

Attributes

loaded_modules[R]

@return [Hash] hash of loaded modules, mapping absolute paths to modules

Public Class Methods

add_tags(tags) click to toggle source
# File lib/modulation/core.rb, line 153
def add_tags(tags)
  Paths.add_tags(tags, caller(CALLER_RANGE).first)
end
auto_import_map(path, options = {}, caller_location = caller(CALLER_RANGE).first) click to toggle source
# File lib/modulation/core.rb, line 79
def auto_import_map(path, options = {},
                    caller_location = caller(CALLER_RANGE).first)
  abs_path = Paths.absolute_dir_path(path, caller_location)
  Hash.new do |h, k|
    fn = Paths.check_path(File.join(abs_path, k.to_s))
    h[k] = find_auto_import_module(fn, path, options)
  end
end
create(arg = nil, &block) click to toggle source
# File lib/modulation/core.rb, line 157
def create(arg = nil, &block)
  creator = import '@modulation/creator'
  return creator.from_block(block) if block

  case arg
  when Hash
    creator.from_hash(arg)
  when String
    creator.from_string(arg)
  else
    raise 'Invalid argument'
  end
end
create_module_from_file(path, import_caller) click to toggle source

Creates a new module from a source file @param path [String] source file name @return [Module] module

# File lib/modulation/core.rb, line 102
def create_module_from_file(path, import_caller)
  Builder.make(location: path, caller: import_caller)
end
find_auto_import_module(filename, path, options) click to toggle source
# File lib/modulation/core.rb, line 88
def find_auto_import_module(filename, path, options)
  if filename
    return @loaded_modules[filename] ||
           create_module_from_file(filename, caller)
  end

  return options[:not_found] if options.key?(:not_found)

  raise "Module not found #{path}"
end
full_backtrace!() click to toggle source

Show full backtrace for errors occuring while loading a module. Normally Modulation will remove stack frames occurring inside the modulation.rb code in order to make backtraces more readable when debugging.

# File lib/modulation/core.rb, line 25
def full_backtrace!
  @full_backtrace = true
end
import(path, caller_location = caller(CALLER_RANGE).first) click to toggle source

Imports a module from a file If the module is already loaded, returns the loaded module. @param path [String] unqualified file name @param caller_location [String] caller location @return [Module] loaded module object

# File lib/modulation/core.rb, line 38
def import(path, caller_location = caller(CALLER_RANGE).first)
  abs_path = Paths.process(path, caller_location)

  case abs_path
  when String
    @loaded_modules[abs_path] || create_module_from_file(abs_path, caller)
  when :require_gem
    raise_error(LoadError.new(GEM_REQUIRE_ERROR_MESSAGE), caller)
  else
    raise_error(LoadError.new("Module not found: #{path}"), caller)
  end
end
import_all(path, caller_location = caller(CALLER_RANGE).first) click to toggle source

Imports all source files in given directory @ param path [String] relative directory path @param caller_location [String] caller location @return [Array] array of module objects

# File lib/modulation/core.rb, line 55
def import_all(path, caller_location = caller(CALLER_RANGE).first)
  abs_path = Paths.absolute_dir_path(path, caller_location)
  Dir["#{abs_path}/**/*.rb"].map do |fn|
    @loaded_modules[fn] || create_module_from_file(fn, caller)
  end
end
import_map(path, options = {}, caller_location = caller(CALLER_RANGE).first) click to toggle source

Imports all source files in given directory, returning a hash mapping filenames to modules @ param path [String] relative directory path @ param options [Hash] options @param caller_location [String] caller location @return [Hash] hash mapping filenames to modules

# File lib/modulation/core.rb, line 68
def import_map(path, options = {},
               caller_location = caller(CALLER_RANGE).first)
  abs_path = Paths.absolute_dir_path(path, caller_location)
  use_symbols = options[:symbol_keys]
  Dir["#{abs_path}/*.rb"].each_with_object({}) do |fn, h|
    mod = @loaded_modules[fn] || create_module_from_file(fn, caller)
    name = File.basename(fn) =~ /^(.+)\.rb$/ && Regexp.last_match(1)
    h[use_symbols ? name.to_sym : name] = mod
  end
end
mock(path, mod, caller_location = caller(CALLER_RANGE).first) { || ... } click to toggle source

Maps the given path to the given mock module, restoring the previously loaded module (if any) after calling the given block @param path [String] module path @param mod [Module] module @param caller_location [String] caller location @return [void]

# File lib/modulation/core.rb, line 144
def mock(path, mod, caller_location = caller(CALLER_RANGE).first)
  path = Paths.absolute_path(path, caller_location)
  old_module = @loaded_modules[path]
  @loaded_modules[path] = mod
  yield if block_given?
ensure
  @loaded_modules[path] = old_module if block_given?
end
raise_error(error, backtrace = nil) click to toggle source

(Re-)raises an error, potentially filtering its backtrace to remove stack frames occuring in Modulation code @param error [Error] raised error @param caller [Array] error backtrace @return [void]

# File lib/modulation/core.rb, line 111
def raise_error(error, backtrace = nil)
  if backtrace
    unless @full_backtrace
      i = 0
      backtrace = backtrace.reject do |l|
        (i += 1) > 1 && l =~ /^#{Modulation::DIR}/
      end
    end
    error.set_backtrace(backtrace)
  end
  raise error
end
reload(mod) click to toggle source

Reloads the given module from its source file @param mod [Module, String] module to reload @return [Module] module

# File lib/modulation/core.rb, line 127
def reload(mod)
  if mod.is_a?(String)
    path = mod
    mod = @loaded_modules[File.expand_path(mod)]
    raise "No module loaded from #{path}" unless mod
  end

  Builder.reload_module_code(mod)
  mod
end
reset!() click to toggle source

Resets the loaded modules hash

# File lib/modulation/core.rb, line 18
def reset!
  @loaded_modules = {}
end