Skip to content

Instantly share code, notes, and snippets.

@erikh
Created March 4, 2011 00:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erikh/22fa600b8c98bdf8c785 to your computer and use it in GitHub Desktop.
Save erikh/22fa600b8c98bdf8c785 to your computer and use it in GitHub Desktop.
module Gem
class FileSystem
attr_reader :path
def initialize(*parts)
@path = Path.new(*parts)
end
def bin
path.add 'bin'
end
def cache
path.add 'cache'
end
def specifications
path.add 'specifications'
end
def gems
path.add 'gems'
end
def doc
path.add 'doc'
end
class Path
def initialize(*parts)
@path = File.join(*parts)
end
def readable?
File.readable?(@path)
end
def writable?
File.writable?(@path)
end
def path
@path.dup
end
alias to_s path
alias to_str path
def add(filename)
self.class.new(@path, filename)
end
end
end
end
if __FILE__ == $0
module Gem
def self.os
FileSystem.new('/tmp')
end
def self.user
FileSystem.new(File.expand_path("~/.gems"))
end
end
p Gem.os.path #==> /tmp
p Gem.user.path #==> ~/.gems
path = Gem.os.gems.add("some-gem-0.1.2.gem")
p path #==> /tmp/gems/some-gem-0.1.2.gem
require 'fileutils'
FileUtils.mkdir_p Gem.os.gems
p path.writable? #==> false (file doesn't exist)
FileUtils.touch path
p path.writable? #==> true
p path.readable? #==> true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment