Class: Fancypath
Instance Method Summary collapse
- #all_children ⇒ Object
- #append(contents) ⇒ Object
- #children ⇒ Object
- #copy(dest) ⇒ Object (also: #cp)
- #create_dir ⇒ Object (also: #create)
- #has_extension?(ext) ⇒ Boolean
- #inspect ⇒ Object
- #join(path) ⇒ Object (also: #/)
- #move(dest) ⇒ Object (also: #mv)
- #parent ⇒ Object
-
#remove ⇒ Object
(also: #rm)
file or dir.
-
#select(arg) ⇒ Object
only takes sym atm.
- #set_extension(ext) ⇒ Object (also: #change_extension)
- #tail(bytes) ⇒ Object
- #to_path ⇒ Object
-
#touch ⇒ Object
make file.
- #without_extension ⇒ Object
- #write(contents, mode = 'wb') ⇒ Object
Methods inherited from Pathname
Instance Method Details
#all_children ⇒ Object
105 |
# File 'lib/fancypath.rb', line 105 alias_method :all_children, :children |
#append(contents) ⇒ Object
68 69 70 71 |
# File 'lib/fancypath.rb', line 68 def append(contents) write(contents,'a+') self end |
#children ⇒ Object
107 108 109 |
# File 'lib/fancypath.rb', line 107 def children super.reject { |c| c.basename.to_s =~ /^\./ } end |
#copy(dest) ⇒ Object Also known as: cp
48 49 50 51 |
# File 'lib/fancypath.rb', line 48 def copy(dest) FileUtils.cp(self, dest) self end |
#create_dir ⇒ Object Also known as: create
41 42 43 44 |
# File 'lib/fancypath.rb', line 41 def create_dir mkpath unless exist? self.to_path end |
#has_extension?(ext) ⇒ Boolean
97 98 99 |
# File 'lib/fancypath.rb', line 97 def has_extension?(ext) !!(self.to_s =~ /\.#{ext}$/) end |
#inspect ⇒ Object
119 120 121 |
# File 'lib/fancypath.rb', line 119 def inspect super.sub('Pathname','Fancypath') end |
#join(path) ⇒ Object Also known as: /
29 30 31 |
# File 'lib/fancypath.rb', line 29 def join(path) super(path).to_path end |
#move(dest) ⇒ Object Also known as: mv
73 74 75 76 |
# File 'lib/fancypath.rb', line 73 def move(dest) self.rename(dest) dest.to_path end |
#parent ⇒ Object
101 102 103 |
# File 'lib/fancypath.rb', line 101 def parent super.to_path end |
#remove ⇒ Object Also known as: rm
file or dir
56 57 58 59 |
# File 'lib/fancypath.rb', line 56 def remove directory? ? rmtree : delete if exist? self.to_path end |
#select(arg) ⇒ Object
only takes sym atm
112 113 114 115 116 117 |
# File 'lib/fancypath.rb', line 112 def select(arg) case arg when Symbol ; Dir["#{self}/*.#{arg}"].map { |p| self.class.new(p) } else ; Dir["#{self}/#{arg}"].map { |p| self.class.new(p) } end end |
#set_extension(ext) ⇒ Object Also known as: change_extension
88 89 90 |
# File 'lib/fancypath.rb', line 88 def set_extension(ext) "#{without_extension}.#{ext}".to_path end |
#tail(bytes) ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/fancypath.rb', line 78 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_path ⇒ Object
123 124 125 |
# File 'lib/fancypath.rb', line 123 def to_path self end |
#touch ⇒ Object
make file
36 37 38 39 |
# File 'lib/fancypath.rb', line 36 def touch FileUtils.touch self.to_s self.to_path end |
#without_extension ⇒ Object
93 94 95 |
# File 'lib/fancypath.rb', line 93 def without_extension to_s[/^ (.+?) (\. ([^\.]+))? $/x, 1].to_path end |
#write(contents, mode = 'wb') ⇒ Object
62 63 64 65 66 |
# File 'lib/fancypath.rb', line 62 def write(contents, mode='wb') dirname.create open(mode) { |f| f.write contents } self.to_path end |