Module: FilePath

Defined in:
lib/piggy-core/file_info.rb

Overview

Some utility methods for path handling.

Class Method Summary collapse

Class Method Details

.intern_path(path_string) ⇒ Object

Ruby path notation for a given operation system/file system specific path



12
13
14
15
16
# File 'lib/piggy-core/file_info.rb', line 12

def FilePath.intern_path(path_string)
  return '' unless path_string
  new_path = path_string.gsub(/\\/, File::SEPARATOR)
  return new_path =~ /:$/ ? new_path + File::SEPARATOR : new_path
end

.join(string1, string2) ⇒ Object

Just as File.join but handles at least one possible error.

Example

FilePath.join('bla/', '/blubb') # => 'bla/blubb'


23
24
25
26
27
28
# File 'lib/piggy-core/file_info.rb', line 23

def FilePath.join(string1, string2)
  return string2 unless string1 && !string1.empty?
  return string1 unless string2 && !string2.empty?
  no_leading_seps2 = string2.sub(Regexp.compile("#{File::SEPARATOR}*"), '')
  return File.join(string1, no_leading_seps2)
end

.one_dir_up(path_string) ⇒ Object

Removes the last section of a given pathString. Won’t remove a section of a file system root. Possible roots are ‘/’, ‘C:’, ‘X:/’, …



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/piggy-core/file_info.rb', line 38

def FilePath.one_dir_up(path_string)
  return '' unless path_string
  sections = FilePath.split(path_string)
  return '' unless sections || !sections.empty?
  if sections.size == 1
    return path_string if path_string =~ /:/
    return path_string if path_string == '/'
    return ''
  end
  return '' unless sections[-1]
  num_of_chars_to_strip = sections[-1].size + 2
  return FilePath.intern_path(path_string[0..-num_of_chars_to_strip])
end

.split(path_string) ⇒ Object

Sections of a given pathString



31
32
33
# File 'lib/piggy-core/file_info.rb', line 31

def FilePath.split(path_string)
  path_string.split(File::SEPARATOR)
end