rubygrep 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1c838b17156364b9342c90840746c64351f30129
4
+ data.tar.gz: a423599ac57581506b04012e7175a9d789625b56
5
+ SHA512:
6
+ metadata.gz: 3fc0f69e927d0ed753cc527f1f22a826456d0f58945af3c9399d82c129839c8629760a8e7037c877a8857288622448e5f0bdf7aec58ee1cbc143e92fe9578b28
7
+ data.tar.gz: 1913cc0bb2554f01c6243531f72893cd4534336a923e9a694a79a646b7d5924ee11aa71ad47eedd59cfb0bb8e69498bc289f03a4d66060e196a7374adacc95d4
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .idea
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rubygrep.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 andrey
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Rubygrep
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rubygrep'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rubygrep
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( http://github.com/<my-github-username>/rubygrep/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :test => :spec
@@ -0,0 +1,68 @@
1
+ module Rubygrep
2
+ class FileReader
3
+ attr_accessor :file_names, :skip_current_file, :options
4
+
5
+ def initialize(file_names, options = {})
6
+ @options = options
7
+ @file_names = []
8
+ process_with_options(file_names)
9
+ end
10
+
11
+ def each_line
12
+ file_names.each do |file_name|
13
+ next_file = open_file(file_name)
14
+ num = 0
15
+ if next_file
16
+ next_file.each_line do |str|
17
+ yield({str: str, path: file_name, str_num: num+=1})
18
+ if skip_current_file
19
+ @skip_current_file = false
20
+ break
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ def next_file!
28
+ @skip_current_file = true
29
+ end
30
+
31
+ private
32
+
33
+ def process_with_options(file_names, current_folder = '.')
34
+ file_names.each do |file_name|
35
+ if File.directory?(file_name) && options[:recursive]
36
+ process_with_options(inner_files(file_name), relative_path(file_name, current_folder))
37
+ elsif File.file?(file_name)
38
+ @file_names << relative_path(file_name, current_folder)
39
+ else
40
+ puts "No such file or directory #{file_name}"
41
+ end
42
+ end
43
+ end
44
+
45
+ def inner_files(folder_name)
46
+ Dir.entries(folder_name).delete_if {|file| file =~ /^\./}
47
+ end
48
+
49
+ def relative_path(file_name, folder)
50
+ if file_name =~ /^\\|\./
51
+ file_name
52
+ else
53
+ "#{folder}/#{file_name}"
54
+ end
55
+ end
56
+
57
+ def open_file(file_name)
58
+ File.open(File.expand_path(file_name, Dir.getwd))
59
+ rescue Errno::ENOENT
60
+ puts "No such file or directory #{file_name}"
61
+ false
62
+ rescue Errno::EACCES
63
+ puts "Permission denied: #{file_name}"
64
+ false
65
+ end
66
+
67
+ end
68
+ end
@@ -0,0 +1,20 @@
1
+ module Rubygrep
2
+ class GrepManager
3
+ attr_reader :file_reader, :matcher, :outputter
4
+
5
+ def initialize(options, expression, file_names)
6
+ @file_reader = FileReader.new(file_names, options.file_reader_options)
7
+ @matcher = Matcher.new(expression, options.matcher_options)
8
+ @outputter = Outputter.new(options.outputter_options)
9
+ end
10
+
11
+ def run
12
+ file_reader.each_line do |line_data|
13
+ match_data = matcher.matches?(line_data)
14
+ if match_data
15
+ outputter.out(match_data, line_data)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,21 @@
1
+ module Rubygrep
2
+ class Matcher
3
+ attr_accessor :options, :regexp
4
+
5
+ def initialize(regexp, options = {})
6
+ @options = options
7
+ @regexp = process_with_options(regexp)
8
+ end
9
+
10
+ def matches?(string_data)
11
+ regexp.match(string_data[:str])
12
+ end
13
+
14
+ private
15
+
16
+ def process_with_options(regexp)
17
+ regexp = "^((?!#{regexp}).)*$" if options[:invert_selection]
18
+ Regexp.compile(regexp, options[:ignore_case])
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,25 @@
1
+ module Rubygrep
2
+ class Outputter
3
+ attr_accessor :options
4
+
5
+ def initialize(options = {})
6
+ @options = options
7
+ end
8
+
9
+ def out(match_data, line_data)
10
+ $stdout << format_with_options(match_data, line_data)
11
+ end
12
+
13
+ private
14
+
15
+ # puts line_data[:str_num].to_s + ' ' + line_data[:str].insert(match_data.begin(0),'|').insert(match_data.end(0) + 1,'|')
16
+ def format_with_options(match_data, line_data)
17
+ result = "#{line_data[:str]}"
18
+ if options[:line_number]
19
+ result = "#{line_data[:str_num]}: #{result}"
20
+ end
21
+ result
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,55 @@
1
+ module Rubygrep
2
+ class RubyGrepOptions
3
+ attr_accessor :file_reader_options, :matcher_options, :outputter_options,
4
+ :general_output_options, :file_names, :expression
5
+
6
+ def initialize(args)
7
+ @file_reader_options, @matcher_options, @outputter_options = {}, {}, {}
8
+ parse_options(args)
9
+ end
10
+
11
+ private
12
+
13
+ def parse_options(args)
14
+ options_parser.parse!(args)
15
+ end_grep('Expression is required') unless args[0]
16
+ @expression = args.shift
17
+ end_grep('At least one file in required') unless args[0]
18
+ @file_names = args
19
+ end
20
+
21
+ def end_grep(message)
22
+ raise message
23
+ end
24
+
25
+ def options_parser
26
+ OptionParser.new do |opts|
27
+ opts.banner = 'Usage: rubygrep.grep [options] expression [file1 file2] ...'
28
+
29
+ #file_reader_options
30
+ opts.on( '-r', '--recursive', 'recursively read directories' ) do
31
+ file_reader_options[:recursive] = true
32
+ end
33
+
34
+ #matcher_options
35
+ opts.on( '-i', '--ignore-case', 'Ignore case when matching strings.' ) do
36
+ matcher_options[:ignore_case] = true
37
+ end
38
+
39
+ opts.on( '-v', '--invert-selection', 'Invert the sense of matching, to select non-matching lines.') do
40
+ matcher_options[:invert_selection] = true
41
+ end
42
+
43
+ #outputter_options
44
+ # opts.on( '-m', '--mark', 'mark matched string' ) do |symbol|
45
+ # outputter_options[:mark_selection] = symbol || '|'
46
+ # end
47
+
48
+ opts.on('-n','--line-number','Prefix each line of output with the 1-based line number within its input file.') do
49
+ outputter_options[:line_number] = true
50
+ end
51
+ end
52
+ end
53
+
54
+ end
55
+ end
@@ -0,0 +1,3 @@
1
+ module Rubygrep
2
+ VERSION = "0.0.2"
3
+ end
data/lib/rubygrep.rb ADDED
@@ -0,0 +1,16 @@
1
+ require 'rubygrep/version'
2
+ require 'rubygrep/grep_manager'
3
+ require 'rubygrep/ruby_grep_options'
4
+ require 'rubygrep/file_reader'
5
+ require 'rubygrep/matcher'
6
+ require 'rubygrep/outputter'
7
+ require 'optparse'
8
+
9
+ module Rubygrep
10
+
11
+ def self.grep
12
+ options = RubyGrepOptions.new(ARGV)
13
+ GrepManager.new(options, options.expression, options.file_names).run
14
+ end
15
+
16
+ end
data/rubygrep.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rubygrep/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rubygrep"
8
+ spec.version = Rubygrep::VERSION
9
+ spec.authors = ["andrey"]
10
+ spec.email = ["om08uhkn@gmail.com"]
11
+ spec.summary = %q{Analog of unix grep utility.}
12
+ spec.description = %q{Globally searches regexp and prints it}
13
+ spec.homepage = "https://github.com/fatbeard2/rubygrep"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.5'
22
+ spec.add_development_dependency 'rake'
23
+ spec.add_development_dependency 'rspec'
24
+ end
@@ -0,0 +1,30 @@
1
+ RSpec.describe Rubygrep::FileReader do
2
+
3
+ let (:one_file) { 'text1.txt' }
4
+ let (:several_files) {%w(text1.txt ./text2.txt ../text3.txt ../folder/text4.txt /root/woop/foo/bar/text5.txt)}
5
+
6
+ it 'lookup for one file' do
7
+ allow(File).to receive(:file?).with(one_file).and_return(true)
8
+ reader = Rubygrep::FileReader.new([one_file])
9
+ expect(reader.file_names).to eq(%w(text1.txt))
10
+ end
11
+
12
+ context 'with several files' do
13
+ let (:reader) {Rubygrep::FileReader.new(several_files)}
14
+ before do
15
+ allow(File).to receive(:file?) { |file_name| several_files.include?(file_name) }
16
+ end
17
+
18
+ it 'should lookup for several files' do
19
+ expect(reader.file_names).to eq(several_files)
20
+ end
21
+
22
+ it 'should lookup for several files' do
23
+ expect { reader.each_line }.to yield_with_args(several_files)
24
+ end
25
+
26
+ end
27
+
28
+
29
+
30
+ end
@@ -0,0 +1,45 @@
1
+ RSpec.describe Rubygrep::Matcher do
2
+
3
+ def string_data(str)
4
+ {str: str}
5
+ end
6
+
7
+ context 'with no options' do
8
+ let(:matcher) { Rubygrep::Matcher.new('\d\d\d\d') }
9
+
10
+ it 'should not match string' do
11
+ expect(matcher.matches?(string_data('123l2'))).to be(nil)
12
+ end
13
+
14
+ it 'should match string' do
15
+ expect(matcher.matches?(string_data('123l2234'))).to be_instance_of(MatchData)
16
+ expect(matcher.matches?(string_data('123l2234')).to_s).to eq('2234')
17
+ end
18
+
19
+ end
20
+
21
+ context 'with -i option on' do
22
+ let(:i_matcher) { Rubygrep::Matcher.new('test', {ignore_case: true}) }
23
+
24
+ it 'should match string' do
25
+ expect(i_matcher.matches?(string_data('aAzDfTeStS'))).to be_instance_of(MatchData)
26
+ expect(i_matcher.matches?(string_data('aAzDfTeStS')).to_s).to eq('TeSt')
27
+ end
28
+
29
+ end
30
+
31
+ context 'with -v option on' do
32
+ let(:v_matcher) { Rubygrep::Matcher.new('test', {invert_selection: true}) }
33
+
34
+ it 'should match string' do
35
+ expect(v_matcher.matches?(string_data('aAzDfTeSS'))).to be_instance_of(MatchData)
36
+ expect(v_matcher.matches?(string_data('aAzDfTeSS')).to_s).to eq('aAzDfTeSS')
37
+ end
38
+
39
+ it 'should not match string' do
40
+ expect(v_matcher.matches?(string_data('testaAzDfS'))).to eq(nil)
41
+ end
42
+
43
+ end
44
+
45
+ end
@@ -0,0 +1,15 @@
1
+ RSpec.describe Rubygrep::Outputter do
2
+
3
+ let (:line_data) {{str: 'test', str_num: 5} }
4
+
5
+ it 'should output string data' do
6
+ outputter = Rubygrep::Outputter.new
7
+ expect { outputter.out(nil, line_data) }.to output('test').to_stdout
8
+ end
9
+
10
+ it 'should output string data with string number' do
11
+ outputter = Rubygrep::Outputter.new({line_number: true})
12
+ expect { outputter.out(nil, line_data) }.to output('5: test').to_stdout
13
+ end
14
+
15
+ end
@@ -0,0 +1,44 @@
1
+ RSpec.describe Rubygrep::RubyGrepOptions do
2
+
3
+ def create_options(args)
4
+ Rubygrep::RubyGrepOptions.new args
5
+ end
6
+
7
+ context 'with missing params' do
8
+ it 'output throw error without expression' do
9
+ expect {create_options([])}.to raise_error(RuntimeError, 'Expression is required')
10
+ end
11
+
12
+ it 'output throw error without files' do
13
+ expect {create_options(['test'])}.to raise_error(RuntimeError, 'At least one file in required')
14
+ end
15
+ end
16
+
17
+ context 'with no additional options' do
18
+ subject(:options_object) {create_options(%w(exp text text2))}
19
+
20
+ it 'should parse expression' do
21
+ expect(options_object.expression).to eq('exp')
22
+ end
23
+
24
+ it 'should parse file_names' do
25
+ expect(options_object.file_names).to eq(%w(text text2))
26
+ end
27
+
28
+ it 'should have blank options' do
29
+ expect(options_object.file_reader_options).to eq({})
30
+ expect(options_object.matcher_options).to eq({})
31
+ expect(options_object.outputter_options).to eq({})
32
+ end
33
+ end
34
+
35
+ context 'with all additional options' do
36
+ subject(:options_object) {create_options(%w(-r -i -v -n exp text text2))}
37
+ it 'should have parsed options' do
38
+ expect(options_object.file_reader_options).to eq({recursive: true})
39
+ expect(options_object.matcher_options).to eq({ignore_case: true, invert_selection: true})
40
+ expect(options_object.outputter_options).to eq({line_number: true})
41
+ end
42
+ end
43
+
44
+ end
@@ -0,0 +1,8 @@
1
+ require 'rubygrep/version'
2
+ require 'rubygrep/grep_manager'
3
+ require 'rubygrep/ruby_grep_options'
4
+ require 'rubygrep/file_reader'
5
+ require 'rubygrep/matcher'
6
+ require 'rubygrep/outputter'
7
+ require 'optparse'
8
+
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubygrep
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - andrey
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Globally searches regexp and prints it
56
+ email:
57
+ - om08uhkn@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/rubygrep.rb
69
+ - lib/rubygrep/file_reader.rb
70
+ - lib/rubygrep/grep_manager.rb
71
+ - lib/rubygrep/matcher.rb
72
+ - lib/rubygrep/outputter.rb
73
+ - lib/rubygrep/ruby_grep_options.rb
74
+ - lib/rubygrep/version.rb
75
+ - rubygrep.gemspec
76
+ - spec/file_reader_spec.rb
77
+ - spec/matcher_spec.rb
78
+ - spec/outputter_spec.rb
79
+ - spec/ruby_grep_options_spec.rb
80
+ - spec/spec_helper.rb
81
+ homepage: https://github.com/fatbeard2/rubygrep
82
+ licenses:
83
+ - MIT
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.2.1
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: Analog of unix grep utility.
105
+ test_files:
106
+ - spec/file_reader_spec.rb
107
+ - spec/matcher_spec.rb
108
+ - spec/outputter_spec.rb
109
+ - spec/ruby_grep_options_spec.rb
110
+ - spec/spec_helper.rb