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



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

alias_method :all_children, :children

#append(contents) ⇒ Object



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

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

#childrenObject



115
116
117
# File 'lib/fancypath.rb', line 115

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

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



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

def copy(dest)
  FileUtils.cp(self, dest)
  self
end

#create_dirObject Also known as: create



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

def create_dir
  mkpath unless exist?
  self
end

#empty?Boolean

Returns:

  • (Boolean)


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

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

#has_extension?(ext) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#inspectObject



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

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

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



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

def join(path)
  super(path).to_path
end

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



80
81
82
83
# File 'lib/fancypath.rb', line 80

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

#parentObject



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

def parent
  super.to_path
end

#removeObject Also known as: rm

file or dir



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

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

#select(*args) ⇒ Object

only takes sym atm



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/fancypath.rb', line 120

def select(*args)
  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



95
96
97
# File 'lib/fancypath.rb', line 95

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

#tail(bytes) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/fancypath.rb', line 85

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



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

def to_path
  self
end

#touchObject

make file



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

def touch
  FileUtils.touch self.to_s
  self
end

#without_extensionObject



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

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

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



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

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