Created
August 3, 2008 08:09
-
-
Save quackingduck/3810 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
# latest at: http://gist.github.com/3810 | |
require 'pathname' | |
class Pathname | |
def to_fancypath | |
Fancypath.new(self) | |
end | |
end | |
class String | |
def to_fancypath | |
Fancypath.new(self) | |
end | |
end | |
class Fancypath < Pathname | |
# methods are chainable and do what you think they do | |
def /(path) | |
self.join(path).to_fancypath | |
end | |
# make file | |
def touch | |
dirname.create | |
FileUtils.touch self.to_s | |
self | |
end | |
# make dir | |
def create | |
mkpath unless exist? | |
self | |
end | |
# file or dir | |
def remove | |
directory? ? rmtree : delete if exist? | |
self | |
end | |
def write(contents) | |
dirname.create | |
open('wb') { |f| f.write contents } | |
self | |
end | |
end | |
if __FILE__ == $0 | |
require 'rubygems' | |
require 'spec' | |
TMPDIR = Pathname('/tmp/fancypath') | |
TMPDIR.rmtree if TMPDIR.exist? | |
describe Fancypath do | |
def test_path; TMPDIR.to_fancypath end | |
def file; test_path/'testfile' end | |
def dir; test_path/'testdir' end | |
before { test_path.mkpath } | |
after { test_path.rmtree } | |
describe '#/' do | |
it('returns Fancypath') { (dir/'somefile').class.should == Fancypath } | |
it('joins paths') { (dir/'somefile').to_s.should =~ /\/somefile$/ } | |
end | |
describe '#touch', 'file does not exist' do | |
it('returns self') { file.touch.should == file } | |
it('creates file') { file.touch.should be_file } | |
end | |
describe '#create', 'dir does not exist' do | |
it('returns self') { dir.create.should == dir } | |
it('creates directory') { dir.create.should be_directory } | |
end | |
describe '#remove' do | |
it('returns self') { file.remove.should == file } | |
it('removes file') { file.touch.remove.should_not exist } | |
it('removes directory') { dir.create.remove.should_not exist } | |
end | |
describe '#write' do | |
it('returns self') { file.write('').should == file } | |
it('writes contents to file') { file.write('test').read.should == 'test' } | |
end | |
end #/Fancypath | |
end #/if |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment