Module: Lookbook::PathUtils

Defined in:
lib/lookbook/support/utils/path_utils.rb

Class Method Summary collapse

Class Method Details

.determine_full_path(rel_path, search_dirs = []) ⇒ Object



45
46
47
48
49
# File 'lib/lookbook/support/utils/path_utils.rb', line 45

def determine_full_path(rel_path, search_dirs = [])
  base_path = search_dirs.detect { |p| Dir["#{p}/#{rel_path}"].first }
  path = Dir["#{base_path}/#{rel_path}"].first
  Pathname(path) if path
end

.normalize_paths(paths, allow_root: false) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/lookbook/support/utils/path_utils.rb', line 28

def normalize_paths(paths, allow_root: false)
  Array(paths).map do |path|
    full_path = to_absolute(path)
    if File.exist?(full_path)
      full_path if allow_root || !root_path?(full_path)
    end
  end.compact.uniq
end

.root_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/lookbook/support/utils/path_utils.rb', line 37

def root_path?(path)
  Rails.application.root.to_s == path.to_s
end

.strip_slashes(path) ⇒ Object



41
42
43
# File 'lib/lookbook/support/utils/path_utils.rb', line 41

def strip_slashes(path)
  path.to_s.gsub(/\A\/|\/\z/, "")
end

.to_absolute(path) ⇒ Object



4
5
6
# File 'lib/lookbook/support/utils/path_utils.rb', line 4

def to_absolute(path)
  File.absolute_path(path.to_s, Rails.root)
end

.to_lookup_path(file_path) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/lookbook/support/utils/path_utils.rb', line 8

def to_lookup_path(file_path)
  path = file_path.to_s.downcase

  directory_path = File.dirname(path)
  directory_path = nil if directory_path.start_with?(".")

  file_name = File.basename(path).split(".").first

  segments = [*directory_path&.split("/"), file_name].compact
  segments.map! do |segment|
    PriorityPrefixParser.call(segment).last.tr("-", "_")
  end

  to_path(segments)
end

.to_path(*args) ⇒ Object



24
25
26
# File 'lib/lookbook/support/utils/path_utils.rb', line 24

def to_path(*args)
  args.flatten.compact.map(&:to_s).join("/")
end