JasonKing-grep 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown ADDED
@@ -0,0 +1,40 @@
1
+ grep
2
+ ====
3
+
4
+ Yes, there are already a couple of grep gems around - but they didn't do things
5
+ the way I wanted, and weren't easy to fork. So here's mine.
6
+
7
+ Installation
8
+ ------------
9
+
10
+ To perform a system wide installation:
11
+
12
+ gem source -a http://gems.github.com
13
+ gem install JasonKing-grep
14
+
15
+ Just require it, it just reopens Dir and File and adds the grep instance method
16
+ to each of them. You pass it a regular expression and it returns an array of
17
+ hashes, very simple:
18
+
19
+ :match => Contains the MatchData object (so you can add parens and capture parts)
20
+ :file => Contains the File#path where the pattern matched (mainly useful for the Dir)
21
+ :lineno => Contains the line number where the pattern matched
22
+ :line => Contains the full line from the file where the pattern matched
23
+
24
+ That's it, I think that's the best grep you can have. Call it on a Dir object
25
+ and it will drill down through the directory, matching as it goes. Call it on a
26
+ File object and it will just match in that file.
27
+
28
+ Usage
29
+ -----
30
+
31
+ require 'grep'
32
+ f = File.open('foo')
33
+ m = f.grep(/^\s*Include\s+(\S+)/)
34
+ m.first[:match][1] # => captured parens expression
35
+
36
+
37
+ Contributors
38
+ ------------
39
+
40
+ * Jason King (JasonKing)
data/lib/grep.rb ADDED
@@ -0,0 +1,27 @@
1
+ class Dir
2
+ def grep(re)
3
+ res = []
4
+
5
+ require 'find'
6
+ Find.find(path) do |f|
7
+ if File.file? f
8
+ File.open(f) do |io|
9
+ res += io.grep(re)
10
+ end
11
+ end
12
+ end
13
+ res
14
+ end
15
+ end
16
+
17
+ class File
18
+ def grep(re)
19
+ res = []
20
+ each_line do |line|
21
+ if m = line.match(re)
22
+ res << { :match => m, :file => path, :lineno => $., :line => line }
23
+ end
24
+ end
25
+ res
26
+ end
27
+ end
@@ -0,0 +1,7 @@
1
+
2
+
3
+
4
+ foobar
5
+
6
+
7
+
@@ -0,0 +1,6 @@
1
+
2
+
3
+ foobar
4
+
5
+
6
+
data/test/grep.rb ADDED
@@ -0,0 +1,20 @@
1
+ require "test/unit"
2
+
3
+ DIR = File.dirname(__FILE__)
4
+ require File.join(DIR, '..', 'lib', 'grep')
5
+
6
+ T = File.join( DIR, 'data')
7
+
8
+ class Grep < Test::Unit::TestCase
9
+ def setup
10
+ @dir = Dir.open(T)
11
+ end
12
+
13
+ def test_dir_grep
14
+ m = @dir.grep(/foobar/)
15
+ assert_equal 2, m.size
16
+ assert_equal 4, m.find{|e|e[:file] == './test/data/first.txt'}[:lineno]
17
+ assert_equal 3, m.find{|e|e[:file] == './test/data/third/first.txt'}[:lineno]
18
+ end
19
+
20
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: JasonKing-grep
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Jason King
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-03-12 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: jk@handle.it
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - README.markdown
26
+ - lib/grep.rb
27
+ has_rdoc: true
28
+ homepage: http://github.com/JasonKing/grep
29
+ post_install_message:
30
+ rdoc_options:
31
+ - --inline-source
32
+ - --charset=UTF-8
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: "0"
40
+ version:
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ requirements: []
48
+
49
+ rubyforge_project:
50
+ rubygems_version: 1.2.0
51
+ signing_key:
52
+ specification_version: 2
53
+ summary: grep - grep library done the Ruby way by extending Dir and File
54
+ test_files:
55
+ - test/data/first.txt
56
+ - test/data/third/first.txt
57
+ - test/grep.rb