Module: RubyModKit::CoreExt::Load

Included in:
RubyModKit
Defined in:
lib/ruby_mod_kit/core_ext/load.rb

Overview

the extension for load/require

Constant Summary collapse

LOADABLE_EXTS =

: Array

%w[.rb .rbm .so .o .dll].freeze

Class Method Summary collapse

Class Method Details

.load(path, wrap = false) ⇒ Boolean

Parameters:

  • path (String)
  • wrap (Boolean) (defaults to: false)

Returns:

  • (Boolean)

rbs:

  • path: String

  • wrap: bool

  • return: bool



23
24
25
26
27
28
29
# File 'lib/ruby_mod_kit/core_ext/load.rb', line 23

def load(path, wrap = false) # rubocop:disable Style/OptionalBooleanParameter
  return super unless path.end_with?(".rbm")

  b = wrap ? binding : TOPLEVEL_BINDING
  RubyModKit::CoreExt::Eval.eval(File.read(path), b, path)
  true
end

.load_pathArray<String>

Returns:

  • (Array<String>)

rbs:



54
55
56
# File 'lib/ruby_mod_kit/core_ext/load.rb', line 54

def load_path
  $LOAD_PATH
end

.loaded_featuresArray<String>

Returns:

  • (Array<String>)

rbs:



48
49
50
# File 'lib/ruby_mod_kit/core_ext/load.rb', line 48

def loaded_features
  $LOADED_FEATURES
end

.require(path) ⇒ Boolean

Parameters:

  • path (String)

Returns:

  • (Boolean)

rbs:

  • path: String

  • return: bool



35
36
37
38
39
40
41
42
43
# File 'lib/ruby_mod_kit/core_ext/load.rb', line 35

def require(path)
  require_path = Load.require_path(path)
  return super unless require_path&.end_with?(".rbm")
  return false if Load.loaded_features.include?(require_path)

  Load.loaded_features << require_path
  load(require_path)
  true
end

.require_path(path, expanded: false) ⇒ String?

Parameters:

  • path (String)
  • expanded (Boolean) (defaults to: false)

Returns:

  • (String, nil)

rbs:

  • path: String

  • expanded: bool

  • return: String | nil



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ruby_mod_kit/core_ext/load.rb', line 64

def require_path(path, expanded: false)
  if !expanded && !File.absolute_path?(path)
    return load_path.each.lazy.map { require_path(File.join(_1, path), expanded: true) }.find(&:itself)
  end

  pathes = if path.end_with?(*LOADABLE_EXTS)
    [path]
  else
    LOADABLE_EXTS.map { "#{path}#{_1}" }
  end
  pathes.find { File.exist?(_1) }
end