module Delfos::FileSystem::CommonPath

Constants

SEPARATOR

Public Class Methods

common_parent(path_a, path_b) click to toggle source
# File lib/delfos/file_system/common_path.rb, line 17
def common_parent(path_a, path_b)
  dirs = [
    File.expand_path(path_a.to_s),
    File.expand_path(path_b.to_s),
  ]

  dir1, dir2 = dirs.minmax.map { |dir| dir.split(SEPARATOR) }

  path_from(dir1, dir2, path_a, path_b)
end
included_in?(p1, paths) click to toggle source
# File lib/delfos/file_system/common_path.rb, line 10
def included_in?(p1, paths)
  paths.any? do |p2|
    common = common_parent(p1, p2)
    common.to_s.length >= p2.to_s.length
  end
end

Private Class Methods

append_trailing_slashes!(*paths) click to toggle source
# File lib/delfos/file_system/common_path.rb, line 50
def append_trailing_slashes!(*paths)
  paths.map do |path|
    if Pathname.new(path).directory?
      path += SEPARATOR if path && path.to_s[-1] != SEPARATOR
    end

    path
  end
end
common_path(dir1, dir2) click to toggle source
# File lib/delfos/file_system/common_path.rb, line 42
def common_path(dir1, dir2)
  dir1.
    zip(dir2).
    take_while { |dn1, dn2| dn1 == dn2 }.
    map(&:first).
    join(SEPARATOR)
end
path_from(dir1, dir2, path_a, path_b) click to toggle source
# File lib/delfos/file_system/common_path.rb, line 30
def path_from(dir1, dir2, path_a, path_b)
  common_path = common_path(dir1, dir2)
  common_path, path_a, path_b = append_trailing_slashes!(common_path, path_a, path_b)

  Pathname.new(common_path) if valid_length?(common_path, path_a, path_b)
end
valid_length?(common_path, path_a, path_b) click to toggle source
# File lib/delfos/file_system/common_path.rb, line 37
def valid_length?(common_path, path_a, path_b)
  l = common_path.to_s.length
  (l <= path_a.to_s.length) || (l <= path_b.to_s.length)
end