class Contraption::Location

Attributes

pn[R]

Public Class Methods

new(path=".") click to toggle source
# File lib/contraption/location.rb, line 6
def initialize path="."
  @pn = Pathname.new path
end

Public Instance Methods

==(other_location) click to toggle source
# File lib/contraption/location.rb, line 70
def == other_location
  path == other_location.path
end
cd(directory) click to toggle source
# File lib/contraption/location.rb, line 44
def cd directory
  l = Location.new(pn+directory)
  l.create!  unless l.pn.directory?
  l
end
create!() click to toggle source
# File lib/contraption/location.rb, line 50
def create!
  pn.mkpath unless exist?
end
destroy() click to toggle source
# File lib/contraption/location.rb, line 40
def destroy
  FileUtils.rm_rf pn.expand_path
end
entries() click to toggle source
# File lib/contraption/location.rb, line 19
def entries
  pn.entries.map{|e| e.expand_path(pn) }
end
executable?() click to toggle source
# File lib/contraption/location.rb, line 86
def executable?
  pn.executable?
end
exist?() click to toggle source
# File lib/contraption/location.rb, line 74
def exist?
  pn.exist?
end
list(ext=/.*/) click to toggle source
# File lib/contraption/location.rb, line 10
def list ext=/.*/
  ext = Array(ext).join('|')
  ext = Regexp.new ext
  pn.find
    .reject { |f| f == pn }
    .select { |f| ext.match f.extname }
    .map{ |f| f.basename.to_s }
end
path() click to toggle source
# File lib/contraption/location.rb, line 23
def path
  pn.to_s
end
read(file_name) { |f| ... } click to toggle source
# File lib/contraption/location.rb, line 31
def read file_name
  complete_name = pn+file_name
  return :file_does_not_exist unless complete_name.exist?
  return :file_not_readable unless complete_name.readable?

  return complete_name.read unless block_given?
  complete_name.open("r") { |f| yield f }
end
readable?() click to toggle source
# File lib/contraption/location.rb, line 78
def readable?
  pn.readable?
end
remove(file_name) click to toggle source
# File lib/contraption/location.rb, line 54
def remove file_name
  complete_name = pn+file_name
  return :file_does_not_exist unless complete_name.exist?

  complete_name.delete
end
to_s() click to toggle source
# File lib/contraption/location.rb, line 27
def to_s
  pn.expand_path.to_s
end
writable?() click to toggle source
# File lib/contraption/location.rb, line 82
def writable?
  pn.writable?
end
write(file_name, content) click to toggle source
# File lib/contraption/location.rb, line 61
def write file_name, content
  complete_name = pn+file_name
  return :file_already_exists if complete_name.exist?

  complete_name.dirname.mkpath unless complete_name.dirname.exist?

  complete_name.open("w:UTF-8") {|f| f.print content}
end