Class: Fancypath

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

Instance Method Summary collapse

Methods inherited from Pathname

#to_fancypath

Instance Method Details

#all_childrenObject



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

#childrenObject



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_dirObject 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

Returns:

  • (Boolean)


97
98
99
# File 'lib/fancypath.rb', line 97

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

#inspectObject



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

#parentObject



101
102
103
# File 'lib/fancypath.rb', line 101

def parent
  super.to_path
end

#removeObject 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_pathObject



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

def to_path
  self
end

#touchObject

make file



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

def touch
  FileUtils.touch self.to_s
  self.to_path
end

#without_extensionObject



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