Class: Yoda::Services::LoadablePathResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/yoda/services/loadable_path_resolver.rb

Instance Method Summary collapse

Constructor Details

#initializeLoadablePathResolver

Returns a new instance of LoadablePathResolver.



4
5
# File 'lib/yoda/services/loadable_path_resolver.rb', line 4

def initialize
end

Instance Method Details

#absolute_path?(path) ⇒ Boolean

This is a workaround for Ruby 2.6 (This version does not have ‘File.absolute_path?`)

Parameters:

  • path (String)

Returns:

  • (Boolean)


35
36
37
# File 'lib/yoda/services/loadable_path_resolver.rb', line 35

def absolute_path?(path)
  path[0] == "/"
end

#find_loadable_path(base_paths, pattern) ⇒ String?

Parameters:

  • base_paths (Array<String>)
  • pattern (String)

Returns:

  • (String, nil)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/yoda/services/loadable_path_resolver.rb', line 10

def find_loadable_path(base_paths, pattern)
  # TODO: Support absolute path
  return nil if absolute_path?(pattern)
  return nil if pattern.start_with?("~/")
  return nil if pattern.start_with?("./")
  return nil if pattern.start_with?("../")

  base_paths.each do |base_path|
    path = File.join(base_path, pattern)

    if File.extname(path).empty?
      paths_with_suffix = ::Gem.suffixes.map { |suffix| path + suffix }
      matched_path = paths_with_suffix.find { |path| File.file?(path) }
      return matched_path if matched_path
    else
      return path if File.file?(path)
    end
  end

  return nil
end