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



46
47
48
49
50
51
52
# File 'lib/fancypath/fancypath.rb', line 46

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

#append(contents) ⇒ Object



214
215
216
217
# File 'lib/fancypath/fancypath.rb', line 214

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

#childrenObject Also known as: all_children



92
93
94
# File 'lib/fancypath/fancypath.rb', line 92

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

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



187
188
189
190
# File 'lib/fancypath/fancypath.rb', line 187

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

#create_dirObject Also known as: create, mkdir



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

def create_dir
  mkpath unless exist?
  self
end

#empty?Boolean

Querying the path.

Returns:

  • (Boolean)


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

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

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



138
139
140
# File 'lib/fancypath/fancypath.rb', line 138

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.



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

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

#groupObject



69
70
71
# File 'lib/fancypath/fancypath.rb', line 69

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

#has_extension?(ext) ⇒ Boolean

Returns:

  • (Boolean)


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

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)


75
76
77
# File 'lib/fancypath/fancypath.rb', line 75

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

#inspectObject

About this Fancypath object.



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

def inspect
  super.sub('Pathname','Fancypath')
end

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

Path traversal & manipulation.



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

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

#lengthObject



54
55
56
# File 'lib/fancypath/fancypath.rb', line 54

def length
  to_s.length
end

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



193
194
195
196
# File 'lib/fancypath/fancypath.rb', line 193

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

#ownerObject



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

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

#parentObject



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

def parent
  super.p
end

#readObject



111
112
113
# File 'lib/fancypath/fancypath.rb', line 111

def read
  super if exists?
end

#read!Object

Querying the file the path refers to.



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

alias_method :read!, :read


97
98
99
100
101
102
103
104
# File 'lib/fancypath/fancypath.rb', line 97

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

#removeObject Also known as: rm



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

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

#select(*args) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/fancypath/fancypath.rb', line 142

def select(*args)
  Babushka::LogHelpers.deprecated! '2012-10-23', :instead => 'Fancypath#glob', :example => "#{to_s.inspect}.p.glob(#{args.first.inspect})"
  return args.map { |arg| select(arg) }.flatten.uniq if args.size > 1

  case arg = args.first
  when Symbol
    Dir["#{self}/*.#{arg}"].map { |p| self.class.new(p) }
  when Regexp
    children.select { |child| child.to_s =~ arg }
  else
    Dir["#{self}/#{arg}"].map { |p| self.class.new(p) }
  end
end

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

Filename manupulation.



159
160
161
# File 'lib/fancypath/fancypath.rb', line 159

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

#tail(bytes) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/fancypath/fancypath.rb', line 115

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



42
43
44
# File 'lib/fancypath/fancypath.rb', line 42

def to_fancypath
  self
end

#touchObject

Changing the file or dir the path refers to.



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

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

#without_extensionObject



164
165
166
# File 'lib/fancypath/fancypath.rb', line 164

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

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

Changing the contents of the file the path refers to.



208
209
210
211
212
# File 'lib/fancypath/fancypath.rb', line 208

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

#yamlObject



123
124
125
126
# File 'lib/fancypath/fancypath.rb', line 123

def yaml
  require 'yaml'
  YAML.load_file self
end