adamh-rglob 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Manifest +6 -0
- data/README.rdoc +45 -0
- data/Rakefile +11 -0
- data/lib/rglob.rb +106 -0
- data/rglob.gemspec +33 -0
- data/test/rglob/glob_test.rb +27 -0
- metadata +72 -0
data/Manifest
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
Glob-syntax pattern matching library
|
2
|
+
|
3
|
+
Example:
|
4
|
+
g = RGlob::Glob.new('a/b*.rb')
|
5
|
+
g.match('a/b.rb') # returns true
|
6
|
+
g.match('a/c.rb') # returns false
|
7
|
+
|
8
|
+
The patterns hold the exact same meaning as in Dir.glob, but this library
|
9
|
+
does not rely upon the filesystem. Glob objects have the exact same
|
10
|
+
interface as Regexp.
|
11
|
+
|
12
|
+
RGlob can be used to create a substitute to Dir.glob, useful in unit tests. For
|
13
|
+
instance:
|
14
|
+
|
15
|
+
require 'rglob'
|
16
|
+
require 'set'
|
17
|
+
class MyTest < Test::Unit::TestCase
|
18
|
+
def setup
|
19
|
+
@fs = Set.new(['a.rb', 'b.rb', 'c/d.rb'])
|
20
|
+
|
21
|
+
Dir.class_eval do
|
22
|
+
alias_method(:glob_without_rglob, :glob)
|
23
|
+
def glob(pattern) do
|
24
|
+
glob = RGlob::Glob.new(pattern)
|
25
|
+
returning [] do |ret|
|
26
|
+
@fs.each do |f|
|
27
|
+
ret << f if glob.match(pattern)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def teardown
|
35
|
+
Dir.class_eval do
|
36
|
+
alias_method(:glob, :glob_without_rglob)
|
37
|
+
remove_method(:glob_without_rglob)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_something_with_a_slightly_different_filesystem
|
42
|
+
@fs.add('a/b/c/d/e.rb')
|
43
|
+
call_my_code_which_uses_dir_glob('with', 'applicable', 'arguments')
|
44
|
+
end
|
45
|
+
end
|
data/Rakefile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'echoe'
|
4
|
+
|
5
|
+
Echoe.new('rglob', '0.0.3') do |p|
|
6
|
+
p.summary = 'Glob-syntax pattern matching'
|
7
|
+
p.description = 'Glob-syntax pattern matching'
|
8
|
+
p.url = 'http://github.com/adamh/rglob'
|
9
|
+
p.author = 'Adam Hooper'
|
10
|
+
p.email = 'adam@adamhooper.com'
|
11
|
+
end
|
data/lib/rglob.rb
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
# Glob-syntax pattern matching library
|
2
|
+
#
|
3
|
+
# Example:
|
4
|
+
# g = RGlob::Glob.new('a/b*.rb')
|
5
|
+
# g.match('a/b.rb') # returns true
|
6
|
+
# g.match('a/c.rb') # returns false
|
7
|
+
#
|
8
|
+
# The patterns hold the exact same meaning as in Dir.glob, but this library
|
9
|
+
# does not rely upon the filesystem. Glob objects have the exact same
|
10
|
+
# interface as Regexp.
|
11
|
+
|
12
|
+
module RGlob
|
13
|
+
class Glob
|
14
|
+
attr_reader :pattern, :regexp
|
15
|
+
|
16
|
+
def initialize(pattern)
|
17
|
+
if pattern.is_a?(Regexp)
|
18
|
+
@pattern = nil
|
19
|
+
@regexp = pattern
|
20
|
+
else
|
21
|
+
@pattern = pattern
|
22
|
+
@regexp = RGlob::pattern_to_regexp(pattern)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def &(other)
|
27
|
+
Glob.new(regexp & other.regexp)
|
28
|
+
end
|
29
|
+
|
30
|
+
def ==(other)
|
31
|
+
if other.is_a?(Glob)
|
32
|
+
other = other.regexp
|
33
|
+
end
|
34
|
+
|
35
|
+
regexp == other
|
36
|
+
end
|
37
|
+
alias_method :eql?, :==
|
38
|
+
|
39
|
+
def ===(str)
|
40
|
+
regexp === str
|
41
|
+
end
|
42
|
+
|
43
|
+
def casefold?
|
44
|
+
false
|
45
|
+
end
|
46
|
+
|
47
|
+
def hash
|
48
|
+
regexp.hash
|
49
|
+
end
|
50
|
+
|
51
|
+
def kcode
|
52
|
+
regexp.kcode
|
53
|
+
end
|
54
|
+
|
55
|
+
def match(str)
|
56
|
+
regexp.match(str)
|
57
|
+
end
|
58
|
+
|
59
|
+
def options
|
60
|
+
0
|
61
|
+
end
|
62
|
+
|
63
|
+
def source
|
64
|
+
pattern || regexp.source
|
65
|
+
end
|
66
|
+
|
67
|
+
def to_s
|
68
|
+
pattern || regexp.to_s
|
69
|
+
end
|
70
|
+
|
71
|
+
class << self
|
72
|
+
def compile(pattern)
|
73
|
+
Glob.new(pattern)
|
74
|
+
end
|
75
|
+
|
76
|
+
def escape(str)
|
77
|
+
raise NotImplementedError.new
|
78
|
+
end
|
79
|
+
alias_method :quote, :escape
|
80
|
+
|
81
|
+
def last_match(*args)
|
82
|
+
raise NotImplementedError.new
|
83
|
+
end
|
84
|
+
|
85
|
+
def union(*patterns)
|
86
|
+
raise NotImplementedError.new
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def self.pattern_to_regexp(pattern)
|
92
|
+
s = Regexp.escape(pattern)
|
93
|
+
PatternReplacements.each do |from, to|
|
94
|
+
s.gsub!(from, to)
|
95
|
+
end
|
96
|
+
return Regexp.new("^#{s}$")
|
97
|
+
end
|
98
|
+
|
99
|
+
private
|
100
|
+
|
101
|
+
PatternReplacements = [
|
102
|
+
[ '\\*\\*', '.*' ],
|
103
|
+
[ '\\*', '[^/]*'],
|
104
|
+
[ '\\?', '[^/]' ],
|
105
|
+
]
|
106
|
+
end
|
data/rglob.gemspec
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = %q{rglob}
|
3
|
+
s.version = "0.0.2"
|
4
|
+
|
5
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
6
|
+
s.authors = ["Adam Hooper"]
|
7
|
+
s.date = %q{2009-01-20}
|
8
|
+
s.description = %q{Glob-syntax pattern matching}
|
9
|
+
s.email = %q{adam@adamhooper.com}
|
10
|
+
s.extra_rdoc_files = ["README.rdoc", "lib/rglob.rb"]
|
11
|
+
s.files = ["README.rdoc", "Rakefile", "test/rglob/glob_test.rb", "lib/rglob.rb", "Manifest", "rglob.gemspec"]
|
12
|
+
s.has_rdoc = true
|
13
|
+
s.homepage = %q{http://github.com/adamh/rglob}
|
14
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Rglob", "--main", "README.rdoc"]
|
15
|
+
s.require_paths = ["lib"]
|
16
|
+
s.rubyforge_project = %q{rglob}
|
17
|
+
s.rubygems_version = %q{1.2.0}
|
18
|
+
s.summary = %q{Glob-syntax pattern matching}
|
19
|
+
s.test_files = ["test/rglob/glob_test.rb"]
|
20
|
+
|
21
|
+
if s.respond_to? :specification_version then
|
22
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
23
|
+
s.specification_version = 2
|
24
|
+
|
25
|
+
if current_version >= 3 then
|
26
|
+
s.add_development_dependency(%q<echoe>, [">= 0"])
|
27
|
+
else
|
28
|
+
s.add_dependency(%q<echoe>, [">= 0"])
|
29
|
+
end
|
30
|
+
else
|
31
|
+
s.add_dependency(%q<echoe>, [">= 0"])
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require File.dirname(__FILE__) + '/../../lib/rglob'
|
3
|
+
|
4
|
+
class RGlob::GlobTest < Test::Unit::TestCase
|
5
|
+
def self.define_simple_test(pattern, query, should_match, description)
|
6
|
+
define_method("test_#{description}".to_sym) do
|
7
|
+
glob = RGlob::Glob.new(pattern)
|
8
|
+
assert_equal(should_match, !!glob.match(query), description)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
define_simple_test('a', 'a', true, 'normal pattern matching')
|
13
|
+
define_simple_test('a', 'b', false, 'normal pattern not matching')
|
14
|
+
define_simple_test('a', 'aa', false, 'normal pattern not matching substring')
|
15
|
+
define_simple_test('a?', 'ab', true, '? matching')
|
16
|
+
define_simple_test('a?', 'a-', true, '? matching symbol')
|
17
|
+
define_simple_test('a?', 'abc', false, '? not matching longer string')
|
18
|
+
define_simple_test('a?', 'a/', false, '? not matching /')
|
19
|
+
define_simple_test('a*', 'abcd', true, '* matching end of string')
|
20
|
+
define_simple_test('*a', 'abcda', true, '* matching beginning of string')
|
21
|
+
define_simple_test('a*a', 'abcda', true, '* matching middle of string')
|
22
|
+
define_simple_test('*a*', 'bbbabb', true, '* matching in two places')
|
23
|
+
define_simple_test('a*b', 'ab', true, '* matching nothing')
|
24
|
+
define_simple_test('a*b', 'aa', false, '* not matching')
|
25
|
+
define_simple_test('a*b', 'a/b', false, '* not matching /')
|
26
|
+
define_simple_test('a**b', 'a/b', true, '** matching /')
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: adamh-rglob
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Adam Hooper
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-01-20 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: echoe
|
17
|
+
version_requirement:
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: "0"
|
23
|
+
version:
|
24
|
+
description: Glob-syntax pattern matching
|
25
|
+
email: adam@adamhooper.com
|
26
|
+
executables: []
|
27
|
+
|
28
|
+
extensions: []
|
29
|
+
|
30
|
+
extra_rdoc_files:
|
31
|
+
- README.rdoc
|
32
|
+
- lib/rglob.rb
|
33
|
+
files:
|
34
|
+
- README.rdoc
|
35
|
+
- Rakefile
|
36
|
+
- test/rglob/glob_test.rb
|
37
|
+
- lib/rglob.rb
|
38
|
+
- Manifest
|
39
|
+
- rglob.gemspec
|
40
|
+
has_rdoc: true
|
41
|
+
homepage: http://github.com/adamh/rglob
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options:
|
44
|
+
- --line-numbers
|
45
|
+
- --inline-source
|
46
|
+
- --title
|
47
|
+
- Rglob
|
48
|
+
- --main
|
49
|
+
- README.rdoc
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
version:
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: "1.2"
|
63
|
+
version:
|
64
|
+
requirements: []
|
65
|
+
|
66
|
+
rubyforge_project: rglob
|
67
|
+
rubygems_version: 1.2.0
|
68
|
+
signing_key:
|
69
|
+
specification_version: 2
|
70
|
+
summary: Glob-syntax pattern matching
|
71
|
+
test_files:
|
72
|
+
- test/rglob/glob_test.rb
|