tred-fancypath 0.5.11

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Myles Byrne, Chris Lloyd
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ == fancypath
2
+
3
+ Extensions to the Pathname library to model file IO in an OO mannor.
data/Rakefile ADDED
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+ require 'rubygems/specification'
4
+ require 'date'
5
+ require 'spec/rake/spectask'
6
+
7
+ GEM = "fancypath"
8
+ GEM_VERSION = "0.5.11"
9
+ AUTHORS = ["Myles Byrne", "Chris Lloyd"]
10
+ EMAIL = "myles@ducknewmedia.com"
11
+ HOMEPAGE = "http://ducknewmedia.com/fancypath"
12
+ SUMMARY = "Extensions for the Pathname library."
13
+
14
+ spec = Gem::Specification.new do |s|
15
+ s.name = GEM
16
+ s.version = GEM_VERSION
17
+ s.platform = Gem::Platform::RUBY
18
+ s.has_rdoc = true
19
+ s.extra_rdoc_files = ['README.rdoc', 'LICENSE']
20
+ s.summary = SUMMARY
21
+ s.description = s.summary
22
+ s.authors = AUTHORS
23
+ s.email = EMAIL
24
+ s.homepage = HOMEPAGE
25
+
26
+ # Uncomment this to add a dependency
27
+ # s.add_dependency "foo"
28
+
29
+ s.require_path = 'lib'
30
+ s.autorequire = GEM
31
+ s.files = %w(LICENSE README.rdoc Rakefile) + Dir.glob("{lib,spec}/**/*")
32
+ end
33
+
34
+ task :default => :spec
35
+
36
+ desc "Run specs"
37
+ Spec::Rake::SpecTask.new do |t|
38
+ t.spec_files = FileList['spec/**/*_spec.rb']
39
+ t.spec_opts = %w(-fs --color)
40
+ end
41
+
42
+ Rake::GemPackageTask.new(spec) do |pkg|
43
+ pkg.gem_spec = spec
44
+ end
45
+
46
+ desc "install the gem locally"
47
+ task :install => [:package] do
48
+ sh %{sudo gem install pkg/#{GEM}-#{GEM_VERSION}}
49
+ end
50
+
51
+ desc "create a gemspec file"
52
+ task :make_spec do
53
+ File.open("#{GEM}.gemspec", "w") do |file|
54
+ file.puts spec.to_ruby
55
+ end
56
+ end
data/lib/fancypath.rb ADDED
@@ -0,0 +1,143 @@
1
+ require 'pathname'
2
+
3
+ class Pathname
4
+
5
+ def to_fancypath
6
+ Fancypath.new(self)
7
+ end
8
+
9
+ alias_method :to_path, :to_fancypath
10
+
11
+ end
12
+
13
+ class String
14
+
15
+ def to_fancypath
16
+ Fancypath.new(self)
17
+ end
18
+
19
+ alias_method :to_path, :to_fancypath
20
+
21
+ end
22
+
23
+ class Fancypath < Pathname
24
+ # methods are chainable and do what you think they do
25
+
26
+ alias_method :dir, :dirname
27
+ alias_method :directory, :dirname
28
+
29
+ alias_method :expand, :expand_path
30
+ alias_method :abs, :expand_path
31
+ alias_method :absolute, :expand_path
32
+
33
+ alias_method :exists?, :exist?
34
+ alias_method :rename_to, :rename
35
+
36
+ def join(path)
37
+ super(path).to_path
38
+ end
39
+
40
+ alias_method :/, :join
41
+
42
+ # make file
43
+ def touch
44
+ FileUtils.touch self.to_s
45
+ self
46
+ end
47
+
48
+ def create_dir
49
+ mkpath unless exist?
50
+ self
51
+ end
52
+
53
+ alias_method :create, :create_dir
54
+
55
+ def copy(dest)
56
+ FileUtils.cp(self, dest)
57
+ self
58
+ end
59
+
60
+ alias_method :cp, :copy
61
+
62
+ # file or dir
63
+ def remove
64
+ directory? ? rmtree : delete if exist?
65
+ self
66
+ end
67
+
68
+ alias_method :rm, :remove
69
+ def write(contents, mode='wb')
70
+ dirname.create
71
+ open(mode) { |f| f.write contents }
72
+ self
73
+ end
74
+
75
+ def append(contents)
76
+ write(contents,'a+')
77
+ self
78
+ end
79
+
80
+ def move(dest)
81
+ self.rename(dest)
82
+ dest.to_path
83
+ end
84
+
85
+ def tail(bytes)
86
+ return self.read if self.size < bytes
87
+ open('r') do |f|
88
+ f.seek(-bytes, IO::SEEK_END)
89
+ f.read
90
+ end
91
+ end
92
+
93
+ alias_method :mv, :move
94
+
95
+ def set_extension(ext)
96
+ "#{without_extension}.#{ext}".to_path
97
+ end
98
+
99
+ alias_method :change_extension, :set_extension
100
+
101
+ def without_extension
102
+ to_s[/^ (.+?) (\. ([^\.]+))? $/x, 1].to_path
103
+ end
104
+
105
+ def has_extension?(ext)
106
+ !!(self.to_s =~ /\.#{ext}$/)
107
+ end
108
+
109
+ def parent
110
+ super.to_path
111
+ end
112
+
113
+ alias_method :all_children, :children
114
+
115
+ def children
116
+ super.reject { |c| c.basename.to_s =~ /^\./ }
117
+ end
118
+
119
+ # only takes sym atm
120
+ def select(*args)
121
+ return args.map { |arg| select(arg) }.flatten.uniq if args.size > 1
122
+
123
+ case arg = args.first
124
+ when Symbol
125
+ Dir["#{self}/*.#{arg}"].map { |p| self.class.new(p) }
126
+ when Regexp
127
+ children.select { |child| child.to_s =~ arg }
128
+ else
129
+ Dir["#{self}/#{arg}"].map { |p| self.class.new(p) }
130
+ end
131
+ end
132
+
133
+ def inspect
134
+ super.sub('Pathname','Fancypath')
135
+ end
136
+
137
+ def to_path
138
+ self
139
+ end
140
+
141
+ end
142
+
143
+ def Fancypath(path); Fancypath.new(path) end
@@ -0,0 +1,156 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe Fancypath do
4
+
5
+ before do
6
+ TMP_DIR.rmtree if TMP_DIR.exist?
7
+ TMP_DIR.mkpath
8
+ @file = TMP_DIR.to_path/'testfile'
9
+ @dir = TMP_DIR.to_path/'testdir'
10
+ end
11
+ after { TMP_DIR.rmtree }
12
+
13
+ describe '#join', 'aliased to #/' do
14
+
15
+ it('returns a Fancypath') { (@dir/'somefile').class.should == Fancypath }
16
+ it('joins paths') { (@dir/'somefile').to_s.should =~ /\/somefile$/ }
17
+
18
+ end
19
+
20
+ describe '#parent' do
21
+
22
+ it('returns parent') { @file.parent.should == TMP_DIR.to_path }
23
+ it('returns Fancypath') { @file.parent.should be_instance_of(Fancypath) }
24
+
25
+ end
26
+
27
+ describe '#touch', 'file does not exist' do
28
+
29
+ it('returns self') { @file.touch.should == @file }
30
+ it('returns a Fancypath') { @file.touch.should be_instance_of(Fancypath) }
31
+ it('creates file') { @file.touch.should be_file }
32
+
33
+ end
34
+
35
+ describe '#create', 'dir does not exist' do
36
+
37
+ it('returns self') { @dir.create.should == @dir }
38
+ it('returns a Fancypath') { @dir.create.should be_instance_of(Fancypath) }
39
+ it('creates directory') { @dir.create.should be_directory }
40
+
41
+ end
42
+
43
+ describe '#remove' do
44
+
45
+ it('returns self') { @file.remove.should == @file }
46
+ it('returns a Fancypath') { @file.remove.should be_instance_of(Fancypath) }
47
+ it('removes file') { @file.touch.remove.should_not exist }
48
+ it('removes directory') { @dir.create.remove.should_not exist }
49
+
50
+ end
51
+
52
+ describe '#write' do
53
+
54
+ it('returns self') { @file.write('').should == @file }
55
+ it('returns a Fancypath') { @file.write('').should be_instance_of(Fancypath) }
56
+ it('writes contents to file') { @file.write('test').read.should == 'test' }
57
+
58
+ end
59
+
60
+ describe '#copy' do
61
+
62
+ before { @file.touch }
63
+ it('returns a Fancypath') { @file.copy(TMP_DIR/'foo').should be_instance_of(Fancypath) }
64
+ it('creates a new file') { @file.copy(TMP_DIR/'foo').should exist }
65
+ it('keeps the original') { @file.copy(TMP_DIR/'foo'); @file.should exist }
66
+ it('copies the contents') { @file.copy(TMP_DIR/'foo').read.should == @file.read }
67
+
68
+ end
69
+
70
+ describe '#set_extension' do
71
+
72
+ example "file without extension" do
73
+ Fancypath('/tmp/foo').set_extension('rb').should == Fancypath('/tmp/foo.rb')
74
+ end
75
+
76
+ example "single extension" do
77
+ Fancypath('/tmp/foo.py').set_extension('rb').should == Fancypath('/tmp/foo.rb')
78
+ end
79
+
80
+ example "multi extension" do
81
+ Fancypath('/tmp/foo.py.z').set_extension('rb').should == Fancypath('/tmp/foo.py.rb')
82
+ end
83
+
84
+ end
85
+
86
+ describe '#move' do
87
+
88
+ example "destination has the file contents, source does not exist" do
89
+ @file.write('foo')
90
+ dest = TMP_DIR/'newfile'
91
+ @file.move( dest )
92
+ @file.should_not exist
93
+ dest.read.should == 'foo'
94
+ end
95
+
96
+ end
97
+
98
+ describe '#has_extension?' do
99
+
100
+ example do
101
+ Fancypath('/tmp/foo.bar').has_extension?('bar').should be_true
102
+ end
103
+
104
+ example do
105
+ Fancypath('/tmp/foo.bar').has_extension?('foo').should be_false
106
+ end
107
+
108
+ end
109
+
110
+ describe '#select' do
111
+
112
+ example 'with symbol' do
113
+ @dir.create_dir
114
+ %W(a.jpg b.jpg c.gif).each { |f| (@dir/f).touch }
115
+
116
+ @dir.select(:jpg).should == [@dir/'a.jpg', @dir/'b.jpg']
117
+ end
118
+
119
+ example 'with glob' do
120
+ @dir.create_dir
121
+ %W(a.jpg b.jpg c.gif).each { |f| (@dir/f).touch }
122
+
123
+ @dir.select("*.jpg").should == [@dir/'a.jpg', @dir/'b.jpg']
124
+ end
125
+
126
+ example 'with regex' do
127
+ @dir.create_dir
128
+ %W(a.jpg b.jpg c.gif 1.jpg).each { |f| (@dir/f).touch }
129
+
130
+ @dir.select(/[^\d]\.(jpg|gif)$/).should == [@dir/'a.jpg', @dir/'b.jpg', @dir/'c.gif']
131
+ end
132
+
133
+ example 'with multiple args' do
134
+ @dir.create_dir
135
+ %W(a.jpg b.jpg c.gif).each { |f| (@dir/f).touch }
136
+
137
+ @dir.select(:jpg, '*.gif').should == [@dir/'a.jpg', @dir/'b.jpg', @dir/'c.gif']
138
+ end
139
+
140
+ # todo: with block
141
+
142
+ end
143
+
144
+ end #/Fancypath
145
+
146
+ describe "String#to_path" do
147
+
148
+ it('returns a Fancypath') { 'test'.to_path.should be_instance_of(Fancypath) }
149
+
150
+ end
151
+
152
+ describe "Pathname#to_path" do
153
+
154
+ it('returns a Fancypath') { Fancypath.new('/').to_path.should be_instance_of(Fancypath) }
155
+
156
+ end
@@ -0,0 +1,6 @@
1
+ $TESTING=true
2
+ $:.push File.join(File.dirname(__FILE__), '..', 'lib')
3
+
4
+ require 'fancypath'
5
+
6
+ TMP_DIR = __FILE__.to_path.dirname/'..'/'tmp'/'fancypath'
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tred-fancypath
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.11
5
+ platform: ruby
6
+ authors:
7
+ - Myles Byrne
8
+ - Chris Lloyd
9
+ autorequire: fancypath
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-06-08 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: Extensions for the Pathname library.
18
+ email: myles@ducknewmedia.com
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files:
24
+ - README.rdoc
25
+ - LICENSE
26
+ files:
27
+ - LICENSE
28
+ - README.rdoc
29
+ - Rakefile
30
+ - lib/fancypath.rb
31
+ - spec/fancypath_spec.rb
32
+ - spec/spec_helper.rb
33
+ has_rdoc: false
34
+ homepage: http://ducknewmedia.com/fancypath
35
+ post_install_message:
36
+ rdoc_options: []
37
+
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ requirements: []
53
+
54
+ rubyforge_project:
55
+ rubygems_version: 1.2.0
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: Extensions for the Pathname library.
59
+ test_files: []
60
+