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 ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
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
- * http://github.com/jingweno/ruby_grep
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
- == DESCRIPTION:
5
+ == Note on Patches/Pull Requests
6
6
 
7
- Module to search file(s) for lines that match a given pattern.
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
- == FEATURES/PROBLEMS:
13
+ == Install
14
+ $ gem install ruby_grep
10
15
 
11
- * Simulates the "grep" command in Unix/Linux system.
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
- == SYNOPSIS:
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
- FIX (code sample of usage)
30
+ == Copyright
16
31
 
17
- == INSTALL:
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
- gem 'hoe', '>= 2.1.0'
3
- require 'hoe'
4
- require 'fileutils'
5
- require './lib/ruby_grep'
6
-
7
- Hoe.plugin :newgem
8
- # Hoe.plugin :website
9
- # Hoe.plugin :cucumberfeatures
10
-
11
- # Generate all the Rake tasks
12
- # Run 'rake -T' to see list of generated tasks (from gem root directory)
13
- $hoe = Hoe.spec 'ruby_grep' do
14
- self.developer 'Jingwen Owen Ou', 'jingweno@gmail.com'
15
- self.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
16
- self.rubyforge_name = self.name # TODO this is default value
17
- self.test_globs = ['spec/**/*_spec.rb']
18
- # self.extra_deps = [['activesupport','>= 2.0.2']]
19
-
20
- end
21
-
22
- github_username= 'jingweno'
23
-
24
- require 'newgem/tasks'
25
- Dir['tasks/**/*.rake'].each { |t| load t }
26
-
27
- # TODO - want other tests/tasks run by default? Add them to the list
28
- # remove_task :default
29
- # task :default => [:spec, :features]
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
@@ -0,0 +1,3 @@
1
+ module RubyGrep
2
+ Version = '0.0.3'
3
+ 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
- module RubyGrep
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
- # If no block is given, print the path and the matched content
20
- if !block_given?
21
- block = lambda { |p, m| puts "#{p}: #{m}" }
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
- result = Hash.new{ |hash, key| hash[key] = [] }
25
- Find.find(path) do |p|
26
- IO.readlines(p).grep(reg_exp) do |match|
27
- match.strip!
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
- result
33
+ end
34
+
35
+ matches
33
36
  end
34
37
  end
35
38
  end
@@ -1,11 +1,7 @@
1
- require File.dirname(__FILE__) + '/spec_helper.rb'
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
- # Time to add your specs!
4
- # http://rspec.info/
5
- describe "Place your specs here" do
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
- --colour
1
+ --color
data/spec/spec_helper.rb CHANGED
@@ -1,10 +1,9 @@
1
- begin
2
- require 'spec'
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
- - 2
9
- version: 0.0.2
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: rubyforge
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
- - 0
30
- - 4
31
- version: 2.0.4
30
+ - 9
31
+ version: 1.2.9
32
32
  type: :development
33
33
  version_requirements: *id001
34
- - !ruby/object:Gem::Dependency
35
- name: hoe
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
- - History.txt
57
- - Manifest.txt
58
- - PostInstall.txt
41
+ - LICENSE
42
+ - README.rdoc
59
43
  files:
60
- - History.txt
61
- - Manifest.txt
62
- - PostInstall.txt
44
+ - .gitignore
45
+ - LICENSE
63
46
  - README.rdoc
64
47
  - Rakefile
65
48
  - lib/ruby_grep.rb
66
- - script/console
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: PostInstall.txt
57
+ post_install_message:
78
58
  rdoc_options:
79
- - --main
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: ruby_grep
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
@@ -1,4 +0,0 @@
1
- === 0.0.1 2010-04-23
2
-
3
- * 1 major enhancement:
4
- * Initial release
data/Manifest.txt DELETED
@@ -1,13 +0,0 @@
1
- History.txt
2
- Manifest.txt
3
- PostInstall.txt
4
- README.rdoc
5
- Rakefile
6
- lib/ruby_grep.rb
7
- script/console
8
- script/destroy
9
- script/generate
10
- spec/ruby_grep_spec.rb
11
- spec/spec.opts
12
- spec/spec_helper.rb
13
- tasks/rspec.rake
data/PostInstall.txt DELETED
@@ -1,7 +0,0 @@
1
-
2
- For more information on ruby_grep, see http://ruby_grep.rubyforge.org
3
-
4
- NOTE: Change this information in PostInstall.txt
5
- You can also delete it if you don't want it.
6
-
7
-
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