Module: Modulation
- Defined in:
- lib/modulation/core.rb,
lib/modulation.rb,
lib/modulation/paths.rb,
lib/modulation/builder.rb,
lib/modulation/exports.rb,
lib/modulation/version.rb,
lib/modulation/module_mixin.rb,
lib/modulation/export_default.rb,
lib/modulation/export_from_receiver.rb
Overview
Implements main Modulation functionality
Defined Under Namespace
Modules: Builder, ExportDefault, ExportFromReceiver, Exports, ModuleMixin, Paths
Constant Summary collapse
- DIR =
__dir__- RE_CONST =
/^[A-Z]/.freeze
- CALLER_RANGE =
(1..1).freeze
- GEM_REQUIRE_ERROR_MESSAGE =
<<~MSG Can't import from a gem that doesn't depend on Modulation. Please use `require` instead of `import`. MSG
- VERSION =
'1.1'
Class Attribute Summary collapse
-
.loaded_modules ⇒ Hash
readonly
Hash of loaded modules, mapping absolute paths to modules.
Class Method Summary collapse
- .add_tags(tags) ⇒ Object
- .auto_import_map(path, options = {}, caller_location = caller(CALLER_RANGE).first) ⇒ Object
- .create(arg = nil, &block) ⇒ Object
-
.create_module_from_file(path, import_caller) ⇒ Module
Creates a new module from a source file.
- .find_auto_import_module(filename, path, options) ⇒ Object
-
.full_backtrace! ⇒ Object
Show full backtrace for errors occuring while loading a module.
-
.import(path, caller_location = caller(CALLER_RANGE).first) ⇒ Module
Imports a module from a file If the module is already loaded, returns the loaded module.
-
.import_all(path, caller_location = caller(CALLER_RANGE).first) ⇒ Array
Imports all source files in given directory @ param path [String] relative directory path.
-
.import_map(path, options = {}, caller_location = caller(CALLER_RANGE).first) ⇒ Hash
Imports all source files in given directory, returning a hash mapping filenames to modules @ param path [String] relative directory path @ param options [Hash] options.
-
.mock(path, mod, caller_location = caller(CALLER_RANGE).first) ⇒ void
Maps the given path to the given mock module, restoring the previously loaded module (if any) after calling the given block.
-
.raise_error(error, backtrace = nil) ⇒ void
(Re-)raises an error, potentially filtering its backtrace to remove stack frames occuring in Modulation code.
-
.reload(mod) ⇒ Module
Reloads the given module from its source file.
-
.reset! ⇒ Object
Resets the loaded modules hash.
Class Attribute Details
.loaded_modules ⇒ Hash (readonly)
Returns hash of loaded modules, mapping absolute paths to modules.
15 16 17 |
# File 'lib/modulation/core.rb', line 15 def loaded_modules @loaded_modules end |
Class Method Details
.add_tags(tags) ⇒ Object
153 154 155 |
# File 'lib/modulation/core.rb', line 153 def () Paths.(, caller(CALLER_RANGE).first) end |
.auto_import_map(path, options = {}, caller_location = caller(CALLER_RANGE).first) ⇒ Object
79 80 81 82 83 84 85 86 |
# File 'lib/modulation/core.rb', line 79 def auto_import_map(path, = {}, 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, ) end end |
.create(arg = nil, &block) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 |
# 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) ⇒ Module
Creates a new module from a source file
102 103 104 |
# 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) ⇒ Object
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/modulation/core.rb', line 88 def find_auto_import_module(filename, path, ) if filename return @loaded_modules[filename] || create_module_from_file(filename, caller) end return [:not_found] if .key?(:not_found) raise "Module not found #{path}" end |
.full_backtrace! ⇒ Object
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.
25 26 27 |
# File 'lib/modulation/core.rb', line 25 def full_backtrace! @full_backtrace = true end |
.import(path, caller_location = caller(CALLER_RANGE).first) ⇒ Module
Imports a module from a file If the module is already loaded, returns the loaded module.
38 39 40 41 42 43 44 45 46 47 48 49 |
# 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) ⇒ Array
Imports all source files in given directory @ param path [String] relative directory path
55 56 57 58 59 60 |
# 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) ⇒ Hash
Imports all source files in given directory, returning a hash mapping filenames to modules @ param path [String] relative directory path @ param options [Hash] options
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/modulation/core.rb', line 68 def import_map(path, = {}, caller_location = caller(CALLER_RANGE).first) abs_path = Paths.absolute_dir_path(path, caller_location) use_symbols = [: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) ⇒ void
This method returns an undefined value.
Maps the given path to the given mock module, restoring the previously loaded module (if any) after calling the given block
144 145 146 147 148 149 150 151 |
# 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) ⇒ void
This method returns an undefined value.
(Re-)raises an error, potentially filtering its backtrace to remove stack frames occuring in Modulation code
111 112 113 114 115 116 117 118 119 120 121 122 |
# 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) ⇒ Module
Reloads the given module from its source file
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/modulation/core.rb', line 127 def reload(mod) if mod.is_a?(String) path = mod mod = @loaded_modules[File.(mod)] raise "No module loaded from #{path}" unless mod end Builder.reload_module_code(mod) mod end |
.reset! ⇒ Object
Resets the loaded modules hash
18 19 20 |
# File 'lib/modulation/core.rb', line 18 def reset! @loaded_modules = {} end |