ruby_grep 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +23 -35
- data/Rakefile +49 -29
- data/lib/ruby_grep/version.rb +3 -0
- data/lib/ruby_grep.rb +19 -16
- data/spec/ruby_grep_spec.rb +4 -8
- data/spec/spec.opts +1 -1
- data/spec/spec_helper.rb +8 -9
- metadata +17 -37
- data/History.txt +0 -4
- data/Manifest.txt +0 -13
- data/PostInstall.txt +0 -7
- data/script/console +0 -10
- data/script/destroy +0 -14
- data/script/generate +0 -14
- data/tasks/rspec.rake +0 -21
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Jingwen Owen Ou
|
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
CHANGED
@@ -1,44 +1,32 @@
|
|
1
1
|
= ruby_grep
|
2
2
|
|
3
|
-
|
3
|
+
Module to search file(s) for lines that match a given pattern. It (will) implements full functionality of the "grep" command in Unix/Linux system with Ruby. Specification goes to http://www.opengroup.org/onlinepubs/9699919799/utilities/grep.html.
|
4
4
|
|
5
|
-
==
|
5
|
+
== Note on Patches/Pull Requests
|
6
6
|
|
7
|
-
|
7
|
+
* Fork the project.
|
8
|
+
* Make your feature addition or bug fix.
|
9
|
+
* Add tests for it. This is important so I don't break it in a future version unintentionally.
|
10
|
+
* Commit, do not mess with rakefile, version, or history.(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
11
|
+
* Send me a pull request. Bonus points for topic branches.
|
8
12
|
|
9
|
-
==
|
13
|
+
== Install
|
14
|
+
$ gem install ruby_grep
|
10
15
|
|
11
|
-
|
16
|
+
== Examples
|
17
|
+
require 'ruby_grep'
|
18
|
+
|
19
|
+
# grep content in a file
|
20
|
+
file = File.open(file_path)
|
21
|
+
file.grep('example', {:recursive => false}) do |path, line|
|
22
|
+
puts "#{path}: #{line}"
|
23
|
+
end
|
12
24
|
|
13
|
-
|
25
|
+
# grep content by traversing a folder
|
26
|
+
File.grep(folder_path, 'example', {:recursive => true}).each do |match|
|
27
|
+
puts "#{match.path}: #{match.line}" # match is a Struct object with path and line as attributes
|
28
|
+
end
|
14
29
|
|
15
|
-
|
30
|
+
== Copyright
|
16
31
|
|
17
|
-
|
18
|
-
|
19
|
-
* sudo gem ruby_grep
|
20
|
-
|
21
|
-
== LICENSE:
|
22
|
-
|
23
|
-
(The MIT License)
|
24
|
-
|
25
|
-
Copyright (c) 2010 Ruby Grep
|
26
|
-
|
27
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
28
|
-
a copy of this software and associated documentation files (the
|
29
|
-
'Software'), to deal in the Software without restriction, including
|
30
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
31
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
32
|
-
permit persons to whom the Software is furnished to do so, subject to
|
33
|
-
the following conditions:
|
34
|
-
|
35
|
-
The above copyright notice and this permission notice shall be
|
36
|
-
included in all copies or substantial portions of the Software.
|
37
|
-
|
38
|
-
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
39
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
40
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
41
|
-
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
42
|
-
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
43
|
-
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
44
|
-
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
32
|
+
Copyright (c) 2010 Jingwen Owen Ou. See LICENSE for details.
|
data/Rakefile
CHANGED
@@ -1,29 +1,49 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
|
3
|
-
|
4
|
-
require '
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
require File.dirname(__FILE__) + '/lib/ruby_grep/version'
|
5
|
+
|
6
|
+
begin
|
7
|
+
require 'jeweler'
|
8
|
+
Jeweler::Tasks.new do |gem|
|
9
|
+
gem.name = "ruby_grep"
|
10
|
+
gem.summary = %Q{Module to search file(s) for lines that match a given pattern.}
|
11
|
+
gem.description = %Q{Implementing full functionality of the "grep" command in Unix/Linux system with Ruby. Specification goes to http://www.opengroup.org/onlinepubs/9699919799/utilities/grep.html.}
|
12
|
+
gem.email = "jingweno@gmail.com"
|
13
|
+
gem.homepage = "http://github.com/jingweno/ruby_grep"
|
14
|
+
gem.authors = ["Jingwen Owen Ou"]
|
15
|
+
gem.version = RubyGrep::Version
|
16
|
+
|
17
|
+
gem.add_development_dependency("rspec", ">= 1.2.9")
|
18
|
+
end
|
19
|
+
|
20
|
+
Jeweler::GemcutterTasks.new
|
21
|
+
rescue LoadError
|
22
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
23
|
+
end
|
24
|
+
|
25
|
+
require 'spec/rake/spectask'
|
26
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
27
|
+
spec.libs << 'lib' << 'spec'
|
28
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
29
|
+
end
|
30
|
+
|
31
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
32
|
+
spec.libs << 'lib' << 'spec'
|
33
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
34
|
+
spec.rcov = true
|
35
|
+
end
|
36
|
+
|
37
|
+
task :spec => :check_dependencies
|
38
|
+
|
39
|
+
task :default => :spec
|
40
|
+
|
41
|
+
require 'rake/rdoctask'
|
42
|
+
Rake::RDocTask.new do |rdoc|
|
43
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
44
|
+
|
45
|
+
rdoc.rdoc_dir = 'rdoc'
|
46
|
+
rdoc.title = "ruby_grep #{version}"
|
47
|
+
rdoc.rdoc_files.include('README*')
|
48
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
49
|
+
end
|
data/lib/ruby_grep.rb
CHANGED
@@ -1,35 +1,38 @@
|
|
1
|
-
$:.unshift(File.dirname(__FILE__)) unless
|
2
|
-
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
3
|
-
|
4
1
|
require 'find'
|
5
2
|
|
6
|
-
|
7
|
-
VERSION = '0.0.2'
|
3
|
+
MatchLine = Struct.new(:path, :line)
|
8
4
|
|
5
|
+
# The mixin provides methods to search file(s) for lines that match a given pattern.
|
6
|
+
module RubyGrep
|
9
7
|
def self.included(host_class)
|
10
8
|
host_class.extend(ClassMethods)
|
11
9
|
end
|
12
10
|
|
13
11
|
module ClassMethods
|
12
|
+
# Recursively grep the line(s) of file(s) in the specified path that
|
13
|
+
# match a given regular expression or a given string of pattern.
|
14
|
+
# An optional block can be passed to manipulate the match line(s).
|
15
|
+
# Any option is sliently muted for now.
|
14
16
|
def grep(path, reg_exp, options={}, &block)
|
15
17
|
if reg_exp.kind_of? String
|
16
18
|
reg_exp = /#{Regexp.escape(reg_exp)}/
|
17
19
|
end
|
18
20
|
|
19
|
-
|
20
|
-
|
21
|
-
|
21
|
+
matches = []
|
22
|
+
Find.find(path) do |p|
|
23
|
+
IO.readlines(p).grep(reg_exp) do |line|
|
24
|
+
matches << MatchLine.new(p, line.strip)
|
25
|
+
end
|
22
26
|
end
|
23
27
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
match.
|
28
|
-
block.call(p, match)
|
29
|
-
result[p] << match
|
28
|
+
# If block is given, manipulate the match lines in the block
|
29
|
+
if block_given?
|
30
|
+
matches.each do |match|
|
31
|
+
block.call(match.path, match.line)
|
30
32
|
end
|
31
|
-
end
|
32
|
-
|
33
|
+
end
|
34
|
+
|
35
|
+
matches
|
33
36
|
end
|
34
37
|
end
|
35
38
|
end
|
data/spec/ruby_grep_spec.rb
CHANGED
@@ -1,11 +1,7 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/spec_helper
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
it "find this spec in spec directory" do
|
8
|
-
# violated "Be sure to write your specs"
|
3
|
+
describe "RubyGrep" do
|
4
|
+
it "fails" do
|
5
|
+
fail "hey buddy, you should probably rename this file and start specing for real"
|
9
6
|
end
|
10
|
-
|
11
7
|
end
|
data/spec/spec.opts
CHANGED
@@ -1 +1 @@
|
|
1
|
-
--
|
1
|
+
--color
|
data/spec/spec_helper.rb
CHANGED
@@ -1,10 +1,9 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
rescue LoadError
|
4
|
-
require 'rubygems' unless ENV['NO_RUBYGEMS']
|
5
|
-
gem 'rspec'
|
6
|
-
require 'spec'
|
7
|
-
end
|
8
|
-
|
9
|
-
$:.unshift(File.dirname(__FILE__) + '/../lib')
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
10
3
|
require 'ruby_grep'
|
4
|
+
require 'spec'
|
5
|
+
require 'spec/autorun'
|
6
|
+
|
7
|
+
Spec::Runner.configure do |config|
|
8
|
+
|
9
|
+
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 3
|
9
|
+
version: 0.0.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Jingwen Owen Ou
|
@@ -18,66 +18,45 @@ date: 2010-04-28 00:00:00 -07:00
|
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
|
-
name:
|
21
|
+
name: rspec
|
22
22
|
prerelease: false
|
23
23
|
requirement: &id001 !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
25
|
- - ">="
|
26
26
|
- !ruby/object:Gem::Version
|
27
27
|
segments:
|
28
|
+
- 1
|
28
29
|
- 2
|
29
|
-
-
|
30
|
-
|
31
|
-
version: 2.0.4
|
30
|
+
- 9
|
31
|
+
version: 1.2.9
|
32
32
|
type: :development
|
33
33
|
version_requirements: *id001
|
34
|
-
|
35
|
-
|
36
|
-
prerelease: false
|
37
|
-
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
-
requirements:
|
39
|
-
- - ">="
|
40
|
-
- !ruby/object:Gem::Version
|
41
|
-
segments:
|
42
|
-
- 2
|
43
|
-
- 6
|
44
|
-
- 0
|
45
|
-
version: 2.6.0
|
46
|
-
type: :development
|
47
|
-
version_requirements: *id002
|
48
|
-
description: Module to search file(s) for lines that match a given pattern.
|
49
|
-
email:
|
50
|
-
- jingweno@gmail.com
|
34
|
+
description: Implementing full functionality of the "grep" command in Unix/Linux system with Ruby. Specification goes to http://www.opengroup.org/onlinepubs/9699919799/utilities/grep.html.
|
35
|
+
email: jingweno@gmail.com
|
51
36
|
executables: []
|
52
37
|
|
53
38
|
extensions: []
|
54
39
|
|
55
40
|
extra_rdoc_files:
|
56
|
-
-
|
57
|
-
-
|
58
|
-
- PostInstall.txt
|
41
|
+
- LICENSE
|
42
|
+
- README.rdoc
|
59
43
|
files:
|
60
|
-
-
|
61
|
-
-
|
62
|
-
- PostInstall.txt
|
44
|
+
- .gitignore
|
45
|
+
- LICENSE
|
63
46
|
- README.rdoc
|
64
47
|
- Rakefile
|
65
48
|
- lib/ruby_grep.rb
|
66
|
-
-
|
67
|
-
- script/destroy
|
68
|
-
- script/generate
|
49
|
+
- lib/ruby_grep/version.rb
|
69
50
|
- spec/ruby_grep_spec.rb
|
70
51
|
- spec/spec.opts
|
71
52
|
- spec/spec_helper.rb
|
72
|
-
- tasks/rspec.rake
|
73
53
|
has_rdoc: true
|
74
54
|
homepage: http://github.com/jingweno/ruby_grep
|
75
55
|
licenses: []
|
76
56
|
|
77
|
-
post_install_message:
|
57
|
+
post_install_message:
|
78
58
|
rdoc_options:
|
79
|
-
- --
|
80
|
-
- README.rdoc
|
59
|
+
- --charset=UTF-8
|
81
60
|
require_paths:
|
82
61
|
- lib
|
83
62
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -96,10 +75,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
96
75
|
version: "0"
|
97
76
|
requirements: []
|
98
77
|
|
99
|
-
rubyforge_project:
|
78
|
+
rubyforge_project:
|
100
79
|
rubygems_version: 1.3.6
|
101
80
|
signing_key:
|
102
81
|
specification_version: 3
|
103
82
|
summary: Module to search file(s) for lines that match a given pattern.
|
104
83
|
test_files:
|
105
84
|
- spec/ruby_grep_spec.rb
|
85
|
+
- spec/spec_helper.rb
|
data/History.txt
DELETED
data/Manifest.txt
DELETED
data/PostInstall.txt
DELETED
data/script/console
DELETED
@@ -1,10 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
# File: script/console
|
3
|
-
irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
|
4
|
-
|
5
|
-
libs = " -r irb/completion"
|
6
|
-
# Perhaps use a console_lib to store any extra methods I may want available in the cosole
|
7
|
-
# libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
|
8
|
-
libs << " -r #{File.dirname(__FILE__) + '/../lib/ruby_grep.rb'}"
|
9
|
-
puts "Loading ruby_grep gem"
|
10
|
-
exec "#{irb} #{libs} --simple-prompt"
|
data/script/destroy
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
-
|
4
|
-
begin
|
5
|
-
require 'rubigen'
|
6
|
-
rescue LoadError
|
7
|
-
require 'rubygems'
|
8
|
-
require 'rubigen'
|
9
|
-
end
|
10
|
-
require 'rubigen/scripts/destroy'
|
11
|
-
|
12
|
-
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
-
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
-
RubiGen::Scripts::Destroy.new.run(ARGV)
|
data/script/generate
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
-
|
4
|
-
begin
|
5
|
-
require 'rubigen'
|
6
|
-
rescue LoadError
|
7
|
-
require 'rubygems'
|
8
|
-
require 'rubigen'
|
9
|
-
end
|
10
|
-
require 'rubigen/scripts/generate'
|
11
|
-
|
12
|
-
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
-
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
-
RubiGen::Scripts::Generate.new.run(ARGV)
|
data/tasks/rspec.rake
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
begin
|
2
|
-
require 'spec'
|
3
|
-
rescue LoadError
|
4
|
-
require 'rubygems' unless ENV['NO_RUBYGEMS']
|
5
|
-
require 'spec'
|
6
|
-
end
|
7
|
-
begin
|
8
|
-
require 'spec/rake/spectask'
|
9
|
-
rescue LoadError
|
10
|
-
puts <<-EOS
|
11
|
-
To use rspec for testing you must install rspec gem:
|
12
|
-
gem install rspec
|
13
|
-
EOS
|
14
|
-
exit(0)
|
15
|
-
end
|
16
|
-
|
17
|
-
desc "Run the specs under spec/models"
|
18
|
-
Spec::Rake::SpecTask.new do |t|
|
19
|
-
t.spec_opts = ['--options', "spec/spec.opts"]
|
20
|
-
t.spec_files = FileList['spec/**/*_spec.rb']
|
21
|
-
end
|