glob_fu 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 adamh
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.
@@ -0,0 +1,56 @@
1
+ = glob_fu
2
+
3
+ Matches files using Dir.glob... and so much more.
4
+
5
+ == Usage
6
+
7
+ Some examples:
8
+
9
+ # Finds /home/adam/x -- same as Dir.glob
10
+ GlobFu.find('/home/adam/x')
11
+
12
+ # Finds /home/adam/* -- same as Dir.glob
13
+ GlobFu.find('/home/adam/*')
14
+
15
+ # Iterates over /home/adam/* -- same as Dir.glob
16
+ GlobFu.find('/home/adam/*') { |f| puts f }
17
+
18
+ # Finds /home/adam/.../x -- same as Dir.glob
19
+ GlobFu.find('/home/adam/**/x')
20
+
21
+ # Finds /home/adam/*
22
+ GlobFu.find('*', :root => '/home/adam')
23
+
24
+ # Finds /home/adam/* and /home/adam/subdir/*
25
+ GlobFu.find('*', 'subdir/*', :root => '/home/adam')
26
+ GlobFu.find(['*', 'subdir/*'], :root => '/home/adam')
27
+
28
+ # Finds /*.txt but not /*.anything.txt
29
+ GlobFu.find('/*', :suffix => 'txt')
30
+
31
+ # Finds /*.txt, including /*.anything.txt -- just don't use :suffix
32
+ GlobFu.find('/*.txt')
33
+
34
+ # Finds /*.txt(.gz)?, preferring .gz but falling back to plain .txt
35
+ GlobFu.find('/*', :suffix => 'txt', :optional_suffix => 'gz')
36
+
37
+ # Finds /*.something.txt
38
+ GlobFu.find('/*', :suffix => 'txt', :extra_suffix => 'something')
39
+
40
+ == Features
41
+
42
+ * Removes duplicates
43
+ * Sorts each glob alphabetically, for consistent results
44
+
45
+ == Copyright
46
+
47
+ I believe in software freedom, not any abomination thereof. This project is
48
+ released under the Public Domain, meaning I relinquish my copyright (to any
49
+ extend the law allows) and grant you all rights to use, modify, sell, or
50
+ otherwise take advantage of my software.
51
+
52
+ However, I do kindly request that, as a favor, you refrain from using my
53
+ software as part of an evil plan involving velociraptors and mind-controlling
54
+ robots (even though I would not be legally entitled to sue you for doing so).
55
+
56
+ - Adam Hooper
@@ -0,0 +1,49 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "glob_fu"
8
+ gem.summary = %Q{TODO}
9
+ gem.email = "adam@adamhooper.com"
10
+ gem.homepage = "http://github.com/adamh/glob_fu"
11
+ gem.authors = ["adamh"]
12
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
13
+ end
14
+
15
+ rescue LoadError
16
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
17
+ end
18
+
19
+ require 'spec/rake/spectask'
20
+ Spec::Rake::SpecTask.new(:spec) do |spec|
21
+ spec.libs << 'lib' << 'spec'
22
+ spec.spec_files = FileList['spec/**/*_spec.rb']
23
+ end
24
+
25
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
26
+ spec.libs << 'lib' << 'spec'
27
+ spec.pattern = 'spec/**/*_spec.rb'
28
+ spec.rcov = true
29
+ end
30
+
31
+ task :spec => :check_dependencies
32
+
33
+ task :default => :spec
34
+
35
+ require 'rake/rdoctask'
36
+ Rake::RDocTask.new do |rdoc|
37
+ if File.exist?('VERSION.yml')
38
+ config = YAML.load(File.read('VERSION.yml'))
39
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
40
+ else
41
+ version = ""
42
+ end
43
+
44
+ rdoc.rdoc_dir = 'rdoc'
45
+ rdoc.title = "glob_fu #{version}"
46
+ rdoc.rdoc_files.include('README*')
47
+ rdoc.rdoc_files.include('lib/**/*.rb')
48
+ end
49
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.4
@@ -0,0 +1,50 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{glob_fu}
8
+ s.version = "0.0.4"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["adamh"]
12
+ s.date = %q{2009-08-19}
13
+ s.email = %q{adam@adamhooper.com}
14
+ s.extra_rdoc_files = [
15
+ "LICENSE",
16
+ "README.rdoc"
17
+ ]
18
+ s.files = [
19
+ ".document",
20
+ ".gitignore",
21
+ "LICENSE",
22
+ "README.rdoc",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "glob_fu.gemspec",
26
+ "lib/glob_fu.rb",
27
+ "spec/glob_fu_spec.rb",
28
+ "spec/spec_helper.rb"
29
+ ]
30
+ s.has_rdoc = true
31
+ s.homepage = %q{http://github.com/adamh/glob_fu}
32
+ s.rdoc_options = ["--charset=UTF-8"]
33
+ s.require_paths = ["lib"]
34
+ s.rubygems_version = %q{1.3.1}
35
+ s.summary = ""
36
+ s.test_files = [
37
+ "spec/glob_fu_spec.rb",
38
+ "spec/spec_helper.rb"
39
+ ]
40
+
41
+ if s.respond_to? :specification_version then
42
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
43
+ s.specification_version = 2
44
+
45
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
46
+ else
47
+ end
48
+ else
49
+ end
50
+ end
@@ -0,0 +1,63 @@
1
+ module GlobFu
2
+ class << self
3
+ def find(*args, &block)
4
+ options = (Hash === args.last) ? args.pop : {}
5
+ args = args.first if Array === args.first
6
+
7
+ results = do_find(args, options)
8
+
9
+ if block
10
+ results.each(&block)
11
+ end
12
+ results
13
+ end
14
+
15
+ private
16
+
17
+ def do_find(args, options)
18
+ if [:root, :suffix].any? { |k| options[k] }
19
+ args = args.dup
20
+ end
21
+
22
+ if options[:root]
23
+ args.collect! { |a| File.join(options[:root], a) }
24
+ end
25
+
26
+ if options[:suffix]
27
+ args.collect! { |a| "#{a}.*" }
28
+ end
29
+
30
+ ret = args.inject([]) { |r,a| r.concat(do_single_find(a, options)) }
31
+ ret.uniq!
32
+ ret
33
+ end
34
+
35
+ def do_single_find(path, options)
36
+ ret = Dir[path]
37
+
38
+ if options[:suffix]
39
+ n_dots = File.basename(path).count('.')
40
+ basename_regex = (['[^\.]+'] * n_dots).join('\.')
41
+
42
+ suffix_regex = ""
43
+ if options[:extra_suffix]
44
+ suffix_regex << "\\.#{Regexp.quote(options[:extra_suffix])}"
45
+ end
46
+ suffix_regex << "\\.#{Regexp.quote(options[:suffix])}"
47
+ if options[:optional_suffix]
48
+ suffix_regex << "(\\.#{Regexp.quote(options[:optional_suffix])})?"
49
+ end
50
+
51
+ regex = %r{(/|^)#{basename_regex}#{suffix_regex}$}
52
+ ret.reject! { |f| !(f =~ regex) }
53
+ if options[:optional_suffix]
54
+ ret.reject! { |f| f =~ /\.#{Regexp.quote(options[:suffix])}$/ && File.exist?("#{f}.#{options[:optional_suffix]}") }
55
+ end
56
+ end
57
+
58
+ ret.sort!
59
+
60
+ ret
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,136 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe(GlobFu) do
4
+ after(:each) do
5
+ wipe_fs
6
+ end
7
+
8
+ describe('#find') do
9
+ it('should find /full/path/to/x') do
10
+ fs('x')
11
+ f("#{prefix}/x").should == ["#{prefix}/x"]
12
+ end
13
+
14
+ it('should order x before y') do
15
+ Dir.should_receive(:[]).and_return(["#{prefix}/y", "#{prefix}/x"])
16
+ f("#{prefix}/*").should == ["#{prefix}/x", "#{prefix}/y"]
17
+ end
18
+
19
+ it('should order first-glob before second-glob') do
20
+ fs('y', 'x')
21
+ f('y', 'x', :root => prefix).should == ["#{prefix}/y", "#{prefix}/x"]
22
+ end
23
+
24
+ it('should find /full/path/to/*') do
25
+ fs('x')
26
+ f("#{prefix}/*").should == ["#{prefix}/x"]
27
+ end
28
+
29
+ it('should iterate over results') do
30
+ fs('x')
31
+ yielded = nil
32
+ f("#{prefix}/x") { |f| yielded = f }
33
+ yielded.should == "#{prefix}/x"
34
+ end
35
+
36
+ it('should glob "**"') do
37
+ fs('x/y')
38
+ f("#{prefix}/**/y").should == ["#{prefix}/x/y"]
39
+ end
40
+
41
+ it('should use :root') do
42
+ fs('x')
43
+ f('x', :root => prefix).should == ["#{prefix}/x"]
44
+ end
45
+
46
+ it('should allow two arguments') do
47
+ fs('x', 'y')
48
+ f('x', 'y', :root => prefix).should == ["#{prefix}/x", "#{prefix}/y"]
49
+ end
50
+
51
+ it('should allow Array argument') do
52
+ fs('x', 'y')
53
+ f(['x', 'y'], :root => prefix).should == ["#{prefix}/x", "#{prefix}/y"]
54
+ end
55
+
56
+ it('should remove duplicates') do
57
+ fs('x')
58
+ f('x', 'x', :root => prefix).should == ["#{prefix}/x"]
59
+ end
60
+
61
+ it('should use :suffix') do
62
+ fs('x.txt')
63
+ f('x', :root => prefix, :suffix => 'txt').should == ["#{prefix}/x.txt"]
64
+ end
65
+
66
+ it('should ignore double-suffixed when using :suffix') do
67
+ fs('x.X.txt')
68
+ f('*', :root => prefix, :suffix => 'txt').should == []
69
+ end
70
+
71
+ it('should use :optional_suffix to find files') do
72
+ fs('x.txt.gz')
73
+ f('x', :root => prefix, :suffix => 'txt', :optional_suffix => 'gz').should == ["#{prefix}/x.txt.gz"]
74
+ end
75
+
76
+ it('should prefer :optional_suffix over other') do
77
+ fs('x.txt', 'x.txt.gz')
78
+ f('x', :root => prefix, :suffix => 'txt', :optional_suffix => 'gz').should == ["#{prefix}/x.txt.gz"]
79
+ end
80
+
81
+ it('should fall back to non-:optional_suffix if it is not there') do
82
+ fs('x.txt')
83
+ f('x', :root => prefix, :suffix => 'txt', :optional_suffix => 'gz').should == ["#{prefix}/x.txt"]
84
+ end
85
+
86
+ it('should allow :extra_suffix') do
87
+ fs('x.X.txt')
88
+ f('x', :root => prefix, :suffix => 'txt', :extra_suffix => 'X').should == ["#{prefix}/x.X.txt"]
89
+ end
90
+
91
+ it('should ignore un-:extra_suffixed') do
92
+ fs('x.txt')
93
+ f('x', :root => prefix, :suffix => 'txt', :extra_suffix => 'X').should == []
94
+ end
95
+
96
+ it('should need a dot before :extra_suffix') do
97
+ fs('xX.txt', 'xX.X.txt')
98
+ f('xX', :root => prefix, :suffix => 'txt', :extra_suffix => 'X').should == [ "#{prefix}/xX.X.txt" ]
99
+ end
100
+
101
+ it('should allow :extra_suffix and :optional_suffix in combination') do
102
+ fs('x.X.txt.gz')
103
+ f('x', :root => prefix, :suffix => 'txt', :extra_suffix => 'X', :optional_suffix => 'gz').should == ["#{prefix}/x.X.txt.gz"]
104
+ end
105
+ end
106
+
107
+ private
108
+
109
+ def f(*args, &block)
110
+ GlobFu.find(*args, &block)
111
+ end
112
+
113
+ def prefix
114
+ @prefix ||= File.dirname(__FILE__) + '/deleteme'
115
+ end
116
+
117
+ def fs(*files)
118
+ wipe_fs
119
+ FileUtils.mkdir(prefix)
120
+
121
+ files.each do |file|
122
+ path = File.join(prefix, file)
123
+ dir = File.dirname(path)
124
+ unless File.exist?(dir)
125
+ FileUtils.mkdir_p(dir)
126
+ end
127
+ File.open(path, 'w') { |f| f.write("stub\n") }
128
+ end
129
+ end
130
+
131
+ def wipe_fs
132
+ if File.exist?(prefix)
133
+ FileUtils.rm_r(prefix)
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,4 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+
4
+ require File.dirname(__FILE__) + '/../lib/glob_fu'
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: glob_fu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - adamh
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-08-19 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: adam@adamhooper.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - .document
27
+ - .gitignore
28
+ - LICENSE
29
+ - README.rdoc
30
+ - Rakefile
31
+ - VERSION
32
+ - glob_fu.gemspec
33
+ - lib/glob_fu.rb
34
+ - spec/glob_fu_spec.rb
35
+ - spec/spec_helper.rb
36
+ has_rdoc: true
37
+ homepage: http://github.com/adamh/glob_fu
38
+ licenses: []
39
+
40
+ post_install_message:
41
+ rdoc_options:
42
+ - --charset=UTF-8
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ requirements: []
58
+
59
+ rubyforge_project:
60
+ rubygems_version: 1.3.5
61
+ signing_key:
62
+ specification_version: 2
63
+ summary: ""
64
+ test_files:
65
+ - spec/glob_fu_spec.rb
66
+ - spec/spec_helper.rb