Class: KUtil::FileHelper
- Inherits:
-
Object
- Object
- KUtil::FileHelper
- Defined in:
- lib/k_util/file_helper.rb
Overview
Helper methods attached to the namespace for working with Files
Instance Method Summary collapse
- #expand_path(filename, base_path = nil) ⇒ Object
- #home?(path) ⇒ Boolean
- #home_or_absolute?(path) ⇒ Boolean
- #parse_uri(uri) ⇒ Object
- #pathname_absolute?(pathname) ⇒ Boolean (also: #absolute?)
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 (filename, base_path = nil) if pathname_absolute?(filename) filename elsif filename.start_with?('~/') File.(filename) else File.(filename, base_path) end end |
#home?(path) ⇒ 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
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?
21 22 23 |
# File 'lib/k_util/file_helper.rb', line 21 def pathname_absolute?(pathname) Pathname.new(pathname).absolute? end |