Module: Rubycom::BaseInclusionRunner

Defined in:
lib/rubycom/base_inclusion_runner.rb

Class Method Summary collapse

Class Method Details

.is_executed_by_gem?(base_file_path) ⇒ Boolean

Determines whether the including module was executed by a gem binary

Parameters:

  • base_file_path (String)

    the path to the including module’s source file

Returns:

  • (Boolean)

    true|false



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

Parameters:

  • caller (Array)

    the result from Kernel.caller as called from an included module within base

  • run_fn (Method|Proc)

    to be run if run criteria are satisfied

  • base (Module)

    the base Module to test against and to send to the run_fn

  • args (Array)

    a String Array representing the arguments to be passed to run_fn

Returns:

  • nil



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