Class: FSPath

Inherits:
Pathname
  • Object
show all
Defined in:
lib/fspath.rb

Defined Under Namespace

Classes: Tempfile

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.common_dir(*paths) ⇒ Object

Returns common dir for paths



26
27
28
29
30
# File 'lib/fspath.rb', line 26

def common_dir(*paths)
  paths.map do |path|
    new(path).dirname.ascend
  end.inject(:&).first
end

.temp_dir(*args) ⇒ Object

Returns or yields FSPath with temp directory created by Dir.mktmpdir



55
56
57
58
59
60
61
62
63
# File 'lib/fspath.rb', line 55

def temp_dir(*args)
  if block_given?
    Dir.mktmpdir(*args) do |dir|
      yield new(dir)
    end
  else
    new(Dir.mktmpdir(*args))
  end
end

.temp_file(*args, &block) ⇒ Object

Returns or yields temp file created by Tempfile.new with path returning FSPath



33
34
35
36
# File 'lib/fspath.rb', line 33

def temp_file(*args, &block)
  args = %w[f] if args.empty?
  Tempfile.open(self, *args, &block)
end

.temp_file_path(*args) ⇒ Object

Returns or yields path as FSPath of temp file created by Tempfile.new WARNING: loosing reference to returned object will remove file on nearest GC run



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/fspath.rb', line 40

def temp_file_path(*args)
  if block_given?
    temp_file(*args) do |file|
      yield file.path
    end
  else
    file = temp_file(*args)
    file.close
    path = file.path
    path.instance_variable_set(:@__temp_file, file)
    path
  end
end

.~(name = nil) ⇒ Object

Return current user home path if called without argument. If called with argument return specified user home path.



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

def ~(name = nil)
  new(File.expand_path("~#{name}"))
end

Instance Method Details

#+(other) ⇒ Object

Fixing Pathname.+



73
74
75
# File 'lib/fspath.rb', line 73

def +(other)
  self.class.new(plus(@path, other.to_s))
end

#/(other) ⇒ Object

Join paths using File.join



67
68
69
# File 'lib/fspath.rb', line 67

def /(other)
  self.class.new(File.join(@path, other.to_s))
end

#append(data) ⇒ Object

Append data to file



91
92
93
94
95
# File 'lib/fspath.rb', line 91

def append(data)
  open('ab') do |f|
    f.write(data)
  end
end

#ascend(&block) ⇒ Object

Iterates over and yields each element in the given path in ascending order



111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/fspath.rb', line 111

def ascend(&block)
  ascendants = []
  path = @path
  ascendants << self
  while r = chop_basename(path)
    path, name = r
    break if path.empty?
    ascendants << self.class.new(del_trailing_separator(path))
  end
  if block
    ascendants.each(&block)
  end
  ascendants
end

#descend(&block) ⇒ Object

Iterates over and yields each element in the given path in descending order



127
128
129
130
131
132
133
# File 'lib/fspath.rb', line 127

def descend(&block)
  descendants = ascend.reverse
  if block
    descendants.each(&block)
  end
  descendants
end

#escape_globObject

Escape characters in glob pattern



98
99
100
# File 'lib/fspath.rb', line 98

def escape_glob
  self.class.new(escape_glob_string)
end

#glob(*args, &block) ⇒ Object

Expand glob



103
104
105
106
107
108
# File 'lib/fspath.rb', line 103

def glob(*args, &block)
  flags = args.last.is_a?(Fixnum) ? args.pop : nil
  args = [File.join(escape_glob_string, *args)]
  args << flags if flags
  self.class.glob(*args, &block)
end

#parts(&block) ⇒ Object

Returns path parts



136
137
138
# File 'lib/fspath.rb', line 136

def parts(&block)
  split_names(@path).flatten
end

#relative_path_from(other) ⇒ Object

Fixing Pathname.relative_path_from



79
80
81
# File 'lib/fspath.rb', line 79

def relative_path_from(other)
  self.class.new(super(self.class.new(other)))
end

#write(data) ⇒ Object

Write data to file



84
85
86
87
88
# File 'lib/fspath.rb', line 84

def write(data)
  open('wb') do |f|
    f.write(data)
  end
end