Class: KUtil::FileHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/k_util/file_helper.rb

Overview

Helper methods attached to the namespace for working with Files

Instance Method Summary collapse

Instance Method Details

#expand_path(filename, base_path = nil) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/k_util/file_helper.rb', line 7

def expand_path(filename, base_path = nil)
  if pathname_absolute?(filename)
    filename
  elsif filename.start_with?('~/')
    File.expand_path(filename)
  else
    File.expand_path(filename, base_path)
  end
end

#home?(path) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/k_util/file_helper.rb', line 17

def home?(path)
  path.start_with?('~/')
end

#home_or_absolute?(path) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/k_util/file_helper.rb', line 26

def home_or_absolute?(path)
  home?(path) || absolute?(path)
end

#parse_uri(uri) ⇒ Object



30
31
32
33
34
35
# File 'lib/k_util/file_helper.rb', line 30

def parse_uri(uri)
  return uri if uri.is_a?(URI)
  return URI.parse(uri) if uri =~ URI::ABS_URI # https://stackoverflow.com/questions/1805761/how-to-check-if-a-url-is-valid

  URI.join('file:///', uri)
end

#pathname_absolute?(pathname) ⇒ Boolean Also known as: absolute?

Returns:

  • (Boolean)


21
22
23
# File 'lib/k_util/file_helper.rb', line 21

def pathname_absolute?(pathname)
  Pathname.new(pathname).absolute?
end