Module: Rubycom::BaseInclusionRunner
- Defined in:
- lib/rubycom/base_inclusion_runner.rb
Class Method Summary collapse
-
.is_executed_by_gem?(base_file_path) ⇒ Boolean
Determines whether the including module was executed by a gem binary.
-
.run(caller, run_fn, base, args) ⇒ Object
Calls the given run_fn with the given base and args if the base was run from the command line in it’s home folder or from an installed gem library.
Class Method Details
.is_executed_by_gem?(base_file_path) ⇒ Boolean
Determines whether the including module was executed by a gem binary
27 28 29 30 31 32 33 |
# File 'lib/rubycom/base_inclusion_runner.rb', line 27 def self.is_executed_by_gem?(base_file_path) Gem.loaded_specs.map { |k, s| {k => {name: "#{s.name}-#{s.version}", executables: s.executables}} }.reduce({}, &:merge).map { |_, s| base_file_path.include?(s[:name]) && s[:executables].include?(File.basename(base_file_path)) }.flatten.reduce(&:|) end |
.run(caller, run_fn, base, args) ⇒ Object
Calls the given run_fn with the given base and args if the base was run from the command line in it’s home folder or from an installed gem library
if the first caller entry matches $0 then the base module was executed from it’s home folder
13 14 15 16 17 18 19 20 21 |
# File 'lib/rubycom/base_inclusion_runner.rb', line 13 def self.run(caller, run_fn, base, args) base_file_path = caller.first.gsub(/:\d+:.+/, '') if base.class == Module && (base_file_path == $0 || self.is_executed_by_gem?(base_file_path)) base.module_eval { run_fn.call(base, args) } end nil end |