-
-
Save erikh/22fa600b8c98bdf8c785 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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