Class: Fancypath

Inherits:
Pathname show all
Defined in:
lib/fancypath/fancypath.rb

Defined Under Namespace

Modules: Helpers

Instance Method Summary collapse

Methods included from Helpers

#to_expanded_fancypath, #to_tilde_expanded_path

Instance Method Details

#==(other) ⇒ Object


42
43
44
45
46
47
48
# File 'lib/fancypath/fancypath.rb', line 42

def == other
  if other.is_a? String
    to_s == other
  else
    super
  end
end

#append(contents) ⇒ Object


195
196
197
198
# File 'lib/fancypath/fancypath.rb', line 195

def append(contents)
  write(contents,'a+')
  self
end

#childrenObject Also known as: all_children


88
89
90
# File 'lib/fancypath/fancypath.rb', line 88

def children
  super.reject { |c| c.basename.to_s =~ /^\./ }
end

#copy(dest) ⇒ Object Also known as: cp


168
169
170
171
# File 'lib/fancypath/fancypath.rb', line 168

def copy(dest)
  `cp -pPR '#{self}' '#{dest}'`
  self
end

#create_dirObject Also known as: create, mkdir


161
162
163
164
# File 'lib/fancypath/fancypath.rb', line 161

def create_dir
  mkpath unless exist?
  self
end

#empty?Boolean

Querying the path.

Returns:

  • (Boolean)

57
58
59
# File 'lib/fancypath/fancypath.rb', line 57

def empty?
  directory? ? children.size == 0 : self.size == 0
end

#glob(expr = nil, flags = File::FNM_CASEFOLD, &block) ⇒ Object


133
134
135
# File 'lib/fancypath/fancypath.rb', line 133

def glob expr = nil, flags = File::FNM_CASEFOLD, &block
  Dir.glob((expr.nil? ? self : (self / expr)).to_s, flags, &block)
end

#grep(*args) ⇒ Object

Querying the tree below the dir the path refers to.


126
127
128
129
130
131
# File 'lib/fancypath/fancypath.rb', line 126

def grep *args
  if exists?
    matches = read.split("\n").grep(*args)
    matches unless matches.empty?
  end
end

#groupObject


65
66
67
# File 'lib/fancypath/fancypath.rb', line 65

def group
  Etc.getgrgid(File.stat(to_s).gid).name
end

#has_extension?(ext) ⇒ Boolean

Returns:

  • (Boolean)

149
150
151
# File 'lib/fancypath/fancypath.rb', line 149

def has_extension?(ext)
  !!(self.to_s =~ /\.#{ext}$/)
end

#hypothetically_writable?Boolean

True if the path is writable (and already exists), or createable by the current user (i.e. if its closest existing parent is writable).

Returns:

  • (Boolean)

71
72
73
# File 'lib/fancypath/fancypath.rb', line 71

def hypothetically_writable?
  writable_real? || (!exists? && !root? && parent.hypothetically_writable?)
end

#join(path) ⇒ Object Also known as: /

Path traversal & manipulation.


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

def join(path)
  path_str = path.to_s
  super(path_str[0..0] == '/' ? path_str[1..-1] : path_str).p
end

#lengthObject


50
51
52
# File 'lib/fancypath/fancypath.rb', line 50

def length
  to_s.length
end

#move(dest) ⇒ Object Also known as: mv


174
175
176
177
# File 'lib/fancypath/fancypath.rb', line 174

def move(dest)
  self.rename(dest)
  dest.p
end

#ownerObject


61
62
63
# File 'lib/fancypath/fancypath.rb', line 61

def owner
  Etc.getpwuid(File.stat(to_s).uid).name
end

#parentObject


84
85
86
# File 'lib/fancypath/fancypath.rb', line 84

def parent
  super.p
end

#puts(str) ⇒ Object


200
201
202
203
# File 'lib/fancypath/fancypath.rb', line 200

def puts str
  append(str[/\n$/].nil? ? "#{str}\n" : str)
  self
end

#readObject


107
108
109
# File 'lib/fancypath/fancypath.rb', line 107

def read
  super if exists?
end

#read!Object

Querying the file the path refers to.


105
# File 'lib/fancypath/fancypath.rb', line 105

alias_method :read!, :read

93
94
95
96
97
98
99
100
# File 'lib/fancypath/fancypath.rb', line 93

def readlink
  if !symlink?
    self
  elsif
    target = super
    target.absolute? ? target : (dir / target)
  end
end

#removeObject Also known as: rm


180
181
182
183
# File 'lib/fancypath/fancypath.rb', line 180

def remove
  directory? ? rmtree : delete if exist?
  self
end

#set_extension(ext) ⇒ Object Also known as: change_extension

Filename manupulation.


140
141
142
# File 'lib/fancypath/fancypath.rb', line 140

def set_extension(ext)
  "#{without_extension}.#{ext}".p
end

#tail(bytes) ⇒ Object


111
112
113
114
115
116
117
# File 'lib/fancypath/fancypath.rb', line 111

def tail(bytes)
  return self.read if self.size < bytes
  open('r') do |f|
    f.seek(-bytes, IO::SEEK_END)
    f.read
  end
end

#to_fancypathObject

About this Fancypath object.


38
39
40
# File 'lib/fancypath/fancypath.rb', line 38

def to_fancypath
  self
end

#touchObject

Changing the file or dir the path refers to.


156
157
158
159
# File 'lib/fancypath/fancypath.rb', line 156

def touch
  `touch '#{self}'`
  self
end

#without_extensionObject


145
146
147
# File 'lib/fancypath/fancypath.rb', line 145

def without_extension
  to_s[/^ (.+?) (\. ([^\.]+))? $/x, 1].p
end

#write(contents, mode = 'wb') ⇒ Object

Changing the contents of the file the path refers to.


189
190
191
192
193
# File 'lib/fancypath/fancypath.rb', line 189

def write(contents, mode='wb')
  dirname.create
  open(mode) { |f| f.write contents }
  self
end

#yamlObject


119
120
121
# File 'lib/fancypath/fancypath.rb', line 119

def yaml
  Babushka::LogHelpers.removed! :instead => "YAML.load_file", :example => "YAML.load_file(#{self.to_s.inspect})"
end