adamh-glob_fu 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -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.
data/README.rdoc ADDED
@@ -0,0 +1,55 @@
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
+
27
+ # Finds /*.txt but not /*.anything.txt
28
+ GlobFu.find('/*', :suffix => 'txt')
29
+
30
+ # Finds /*.txt, including /*.anything.txt -- just don't use :suffix
31
+ GlobFu.find('/*.txt')
32
+
33
+ # Finds /*.txt(.gz)?, preferring .gz but falling back to plain .txt
34
+ GlobFu.find('/*', :suffix => 'txt', :optional_suffix => 'gz')
35
+
36
+ # Finds /*.something.txt
37
+ GlobFu.find('/*', :suffix => 'txt', :extra_suffix => 'something')
38
+
39
+ == Features
40
+
41
+ * Removes duplicates
42
+ * Sorts each glob alphabetically, for consistent results
43
+
44
+ == Copyright
45
+
46
+ I believe in software freedom, not any abomination thereof. This project is
47
+ released under the Public Domain, meaning I relinquish my copyright (to any
48
+ extend the law allows) and grant you all rights to use, modify, sell, or
49
+ otherwise take advantage of my software.
50
+
51
+ However, I do kindly request that, as a favor, you refrain from using my
52
+ software as part of an evil plan involving velociraptors and mind-controlling
53
+ robots (even though I would not be legally entitled to sue you for doing so).
54
+
55
+ - Adam Hooper
data/Rakefile ADDED
@@ -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.2
data/glob_fu.gemspec ADDED
@@ -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.2"
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 = %q{TODO}
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
data/lib/glob_fu.rb ADDED
@@ -0,0 +1,65 @@
1
+ module GlobFu
2
+ class << self
3
+ def find(*args, &block)
4
+ options = if Hash === args.last
5
+ args.pop
6
+ else
7
+ {}
8
+ end
9
+ results = do_find(args, options)
10
+
11
+ if block
12
+ results.each(&block)
13
+ end
14
+ results
15
+ end
16
+
17
+ private
18
+
19
+ def do_find(args, options)
20
+ if [:root, :suffix].any? { |k| options[k] }
21
+ args = args.dup
22
+ end
23
+
24
+ if options[:root]
25
+ args.collect! { |a| File.join(options[:root], a) }
26
+ end
27
+
28
+ if options[:suffix]
29
+ args.collect! { |a| "#{a}.*" }
30
+ end
31
+
32
+ ret = args.inject([]) { |r,a| r.concat(do_single_find(a, options)) }
33
+ ret.uniq!
34
+ ret
35
+ end
36
+
37
+ def do_single_find(path, options)
38
+ ret = Dir[path]
39
+
40
+ if options[:suffix]
41
+ n_dots = File.basename(path).count('.')
42
+ basename_regex = (['[^\.]+'] * n_dots).join('\.')
43
+
44
+ suffix_regex = ""
45
+ if options[:extra_suffix]
46
+ suffix_regex << "\.#{Regexp.quote(options[:extra_suffix])}"
47
+ end
48
+ suffix_regex << "\.#{Regexp.quote(options[:suffix])}"
49
+ if options[:optional_suffix]
50
+ suffix_regex << "(\.#{Regexp.quote(options[:optional_suffix])})?"
51
+ end
52
+
53
+ regex = %r{(/|^)#{basename_regex}#{suffix_regex}$}
54
+ ret.reject! { |f| !(f =~ regex) }
55
+ if options[:optional_suffix]
56
+ ret.reject! { |f| f =~ /\.#{Regexp.quote(options[:suffix])}$/ && File.exist?("#{f}.#{options[:optional_suffix]}") }
57
+ end
58
+ end
59
+
60
+ ret.sort!
61
+
62
+ ret
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,126 @@
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 remove duplicates') do
52
+ fs('x')
53
+ f('x', 'x', :root => prefix).should == ["#{prefix}/x"]
54
+ end
55
+
56
+ it('should use :suffix') do
57
+ fs('x.txt')
58
+ f('x', :root => prefix, :suffix => 'txt').should == ["#{prefix}/x.txt"]
59
+ end
60
+
61
+ it('should ignore double-suffixed when using :suffix') do
62
+ fs('x.X.txt')
63
+ f('*', :root => prefix, :suffix => 'txt').should == []
64
+ end
65
+
66
+ it('should use :optional_suffix to find files') do
67
+ fs('x.txt.gz')
68
+ f('x', :root => prefix, :suffix => 'txt', :optional_suffix => 'gz').should == ["#{prefix}/x.txt.gz"]
69
+ end
70
+
71
+ it('should prefer :optional_suffix over other') do
72
+ fs('x.txt', 'x.txt.gz')
73
+ f('x', :root => prefix, :suffix => 'txt', :optional_suffix => 'gz').should == ["#{prefix}/x.txt.gz"]
74
+ end
75
+
76
+ it('should fall back to non-:optional_suffix if it is not there') do
77
+ fs('x.txt')
78
+ f('x', :root => prefix, :suffix => 'txt', :optional_suffix => 'gz').should == ["#{prefix}/x.txt"]
79
+ end
80
+
81
+ it('should allow :extra_suffix') do
82
+ fs('x.X.txt')
83
+ f('x', :root => prefix, :suffix => 'txt', :extra_suffix => 'X').should == ["#{prefix}/x.X.txt"]
84
+ end
85
+
86
+ it('should ignore un-:extra_suffixed') do
87
+ fs('x.txt')
88
+ f('x', :root => prefix, :suffix => 'txt', :extra_suffix => 'X').should == []
89
+ end
90
+
91
+ it('should allow :extra_suffix and :optional_suffix in combination') do
92
+ fs('x.X.txt.gz')
93
+ f('x', :root => prefix, :suffix => 'txt', :extra_suffix => 'X', :optional_suffix => 'gz').should == ["#{prefix}/x.X.txt.gz"]
94
+ end
95
+ end
96
+
97
+ private
98
+
99
+ def f(*args, &block)
100
+ GlobFu.find(*args, &block)
101
+ end
102
+
103
+ def prefix
104
+ @prefix ||= File.dirname(__FILE__) + '/deleteme'
105
+ end
106
+
107
+ def fs(*files)
108
+ wipe_fs
109
+ FileUtils.mkdir(prefix)
110
+
111
+ files.each do |file|
112
+ path = File.join(prefix, file)
113
+ dir = File.dirname(path)
114
+ unless File.exist?(dir)
115
+ FileUtils.mkdir_p(dir)
116
+ end
117
+ File.open(path, 'w') { |f| f.write("stub\n") }
118
+ end
119
+ end
120
+
121
+ def wipe_fs
122
+ if File.exist?(prefix)
123
+ FileUtils.rm_r(prefix)
124
+ end
125
+ end
126
+ 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,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: adamh-glob_fu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - adamh
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-08-19 00:00:00 -07: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
+ post_install_message:
40
+ rdoc_options:
41
+ - --charset=UTF-8
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ requirements: []
57
+
58
+ rubyforge_project:
59
+ rubygems_version: 1.3.5
60
+ signing_key:
61
+ specification_version: 2
62
+ summary: TODO
63
+ test_files:
64
+ - spec/glob_fu_spec.rb
65
+ - spec/spec_helper.rb