file_iterator 0.0.1 → 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 +4 -4
- data/Rakefile +9 -0
- data/lib/file_iterator/version.rb +1 -1
- data/lib/file_iterator.rb +42 -47
- data/spec/file_iterator_spec.rb +46 -0
- data/spec/file_with_comment.txt +6 -0
- data/spec/numbers.txt +4 -0
- metadata +9 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ab885aa71c469845ed127ebc18183651bbb5e9e9
|
4
|
+
data.tar.gz: 9ef5bceac991488c68d3dda3e99d419c4daf14c8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 781f797127d92b9b494b817fb430c4adf94a40c80704e82bad648cd7bc3f3000751ea5ae7d92dfebb203fee85e5e0e97d2951474f0bc8cd6abf19792bee4d32c
|
7
|
+
data.tar.gz: 7bc961dfee8e4c08fa1c9154cd0fba80dee9cee5d126bd16bcda4d79dc5264ef08bffd2997671c3f1b020a39fc66510ff5cbf2b9ee9e5a76912cea5083c9b07e
|
data/Rakefile
CHANGED
data/lib/file_iterator.rb
CHANGED
@@ -1,62 +1,57 @@
|
|
1
1
|
require "file_iterator/version"
|
2
2
|
|
3
3
|
module FileIterator
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
@inputfile = input
|
9
|
-
else
|
10
|
-
@inputfile = ARGV[0]
|
11
|
-
unless @inputfile
|
12
|
-
puts "usage: #{__FILE__} inputfile [outputfile]"
|
13
|
-
exit 1
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
4
|
+
class FileIterator
|
5
|
+
def initialize(input: nil, output: ARGV[1], comment_symbol: nil)
|
6
|
+
@comment_symbol = comment_symbol
|
7
|
+
@outputfile = output
|
17
8
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
9
|
+
if input
|
10
|
+
@inputfile = input
|
11
|
+
else
|
12
|
+
@inputfile = ARGV[0]
|
13
|
+
unless @inputfile
|
14
|
+
puts "usage: #{__FILE__} inputfile [outputfile]"
|
15
|
+
exit 1
|
25
16
|
end
|
26
|
-
|
27
|
-
|
17
|
+
end
|
18
|
+
iterate(&Proc.new) if block_given?
|
19
|
+
end
|
20
|
+
|
21
|
+
def is_comment_line(line)
|
22
|
+
(@comment_symbol != nil) && line[0] == @comment_symbol
|
23
|
+
end
|
24
|
+
|
25
|
+
def iterate
|
26
|
+
File.open(@inputfile,'r') do | file|
|
27
|
+
begin
|
28
|
+
if (@outputfile)
|
29
|
+
out = File.open(@outputfile,'w')
|
30
|
+
else
|
31
|
+
out = $stdout
|
32
|
+
end
|
33
|
+
file.each_line do | line |
|
34
|
+
yield(line, out) unless is_comment_line(line)
|
35
|
+
end
|
36
|
+
rescue
|
37
|
+
raise
|
38
|
+
ensure
|
39
|
+
out.close if @outputfile
|
28
40
|
end
|
29
|
-
rescue
|
30
|
-
raise
|
31
|
-
ensure
|
32
|
-
out.close if @outputfile
|
33
41
|
end
|
34
42
|
end
|
35
|
-
|
36
|
-
|
37
|
-
File.open(@inputfile,'r') do | file|
|
43
|
+
|
44
|
+
def to_a
|
38
45
|
result = []
|
39
|
-
|
40
|
-
if
|
41
|
-
|
46
|
+
iterate do | line |
|
47
|
+
if block_given?
|
48
|
+
result << yield(line)
|
42
49
|
else
|
43
|
-
|
44
|
-
end
|
45
|
-
file.each_line do | line |
|
46
|
-
if block_given?
|
47
|
-
result << yield(line)
|
48
|
-
else
|
49
|
-
result << line
|
50
|
-
end
|
50
|
+
result << line
|
51
51
|
end
|
52
|
-
rescue
|
53
|
-
raise
|
54
|
-
ensure
|
55
|
-
out.close if @outputfile
|
56
52
|
end
|
57
53
|
result
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
54
|
+
end
|
61
55
|
|
56
|
+
end
|
62
57
|
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'file_iterator'
|
2
|
+
describe FileIterator::FileIterator do
|
3
|
+
describe "collections" do
|
4
|
+
it "just initializes if no block is given" do
|
5
|
+
fi = FileIterator::FileIterator.new(input: 'numbers.txt')
|
6
|
+
end
|
7
|
+
it "iterates if block is given" do
|
8
|
+
result = []
|
9
|
+
FileIterator::FileIterator.new(input: 'spec/numbers.txt') do | line |
|
10
|
+
result << line.to_i
|
11
|
+
end
|
12
|
+
expect(result).to eq [1,2,3,4]
|
13
|
+
end
|
14
|
+
it "iterates" do
|
15
|
+
result = []
|
16
|
+
fi = FileIterator::FileIterator.new(input: 'spec/numbers.txt')
|
17
|
+
fi.iterate do | line |
|
18
|
+
result << line.to_i
|
19
|
+
end
|
20
|
+
expect(result).to eq [1,2,3,4]
|
21
|
+
end
|
22
|
+
it "converts to array" do
|
23
|
+
result = FileIterator::FileIterator.new(input: 'spec/numbers.txt').to_a.map(&:to_i)
|
24
|
+
expect(result).to eq [1,2,3,4]
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "error handling" do
|
29
|
+
it "throws an exception if file is missing" do
|
30
|
+
expect{FileIterator::FileIterator.new(input: 'not there').to_a}.to raise_error
|
31
|
+
end
|
32
|
+
it "throws an exception if used with block" do
|
33
|
+
expect do
|
34
|
+
a = []
|
35
|
+
FileIterator::FileIterator.new('not there') do | line |
|
36
|
+
a << line
|
37
|
+
end
|
38
|
+
end.to raise_error
|
39
|
+
end
|
40
|
+
end
|
41
|
+
describe "comments" do
|
42
|
+
it "ignores lines with comments" do
|
43
|
+
expect(FileIterator::FileIterator.new(input: 'spec/file_with_comment.txt', comment_symbol: '#').to_a(&:to_i)).to eq [1, 2, 3, 4]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/spec/numbers.txt
ADDED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: file_iterator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- B Blinken
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-11-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -55,6 +55,9 @@ files:
|
|
55
55
|
- file_iterator.gemspec
|
56
56
|
- lib/file_iterator.rb
|
57
57
|
- lib/file_iterator/version.rb
|
58
|
+
- spec/file_iterator_spec.rb
|
59
|
+
- spec/file_with_comment.txt
|
60
|
+
- spec/numbers.txt
|
58
61
|
homepage: https://github.com/drblinken/file_iterator
|
59
62
|
licenses:
|
60
63
|
- MIT
|
@@ -79,4 +82,7 @@ rubygems_version: 2.2.2
|
|
79
82
|
signing_key:
|
80
83
|
specification_version: 4
|
81
84
|
summary: Eases iterating through a file
|
82
|
-
test_files:
|
85
|
+
test_files:
|
86
|
+
- spec/file_iterator_spec.rb
|
87
|
+
- spec/file_with_comment.txt
|
88
|
+
- spec/numbers.txt
|