file_iterator 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: eed99b464c6ac875c87599da7510fd4fb24c445e
4
- data.tar.gz: d07a6c42de9ed0edf89da5f1cfefdeb9cbb1f31d
3
+ metadata.gz: ab885aa71c469845ed127ebc18183651bbb5e9e9
4
+ data.tar.gz: 9ef5bceac991488c68d3dda3e99d419c4daf14c8
5
5
  SHA512:
6
- metadata.gz: f281a708e002fa520ff0fb3d8d4ce4796fa3da20a200c6c9646ba139307f4adbcc8edebfbde0ad1fce890bf6dfe6fb280cf51177db79e561cdbd3b530a2f39e2
7
- data.tar.gz: 30cd278b121c478d401b7b93afbb52cfe01ecb74e9d1f435208336f11754aaff5d3889a0375d00993902c5ef90a3a4c1d67adec4f30fd38c0a2cf3b7e9edabc9
6
+ metadata.gz: 781f797127d92b9b494b817fb430c4adf94a40c80704e82bad648cd7bc3f3000751ea5ae7d92dfebb203fee85e5e0e97d2951474f0bc8cd6abf19792bee4d32c
7
+ data.tar.gz: 7bc961dfee8e4c08fa1c9154cd0fba80dee9cee5d126bd16bcda4d79dc5264ef08bffd2997671c3f1b020a39fc66510ff5cbf2b9ee9e5a76912cea5083c9b07e
data/Rakefile CHANGED
@@ -1,2 +1,11 @@
1
1
  require "bundler/gem_tasks"
2
+ #require "rake"
2
3
 
4
+ begin
5
+ require 'rspec/core/rake_task'
6
+ RSpec::Core::RakeTask.new(:spec)
7
+ rescue LoadError
8
+ end
9
+
10
+ desc "runs all examples"
11
+ task :default => :spec
@@ -1,3 +1,3 @@
1
1
  module FileIterator
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/file_iterator.rb CHANGED
@@ -1,62 +1,57 @@
1
1
  require "file_iterator/version"
2
2
 
3
3
  module FileIterator
4
- class FileIterator
5
- def initialize(input = nil, output = ARGV[1])
6
- @outputfile = output
7
- if input
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
- def iterate
19
- File.open(@inputfile,'r') do | file|
20
- begin
21
- if (@outputfile)
22
- out = File.open(@outputfile,'w')
23
- else
24
- out = $stdout
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
- file.each_line do | line |
27
- yield(line, out)
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
- end
36
- def to_a
37
- File.open(@inputfile,'r') do | file|
43
+
44
+ def to_a
38
45
  result = []
39
- begin
40
- if (@outputfile)
41
- out = File.open(@outputfile,'w')
46
+ iterate do | line |
47
+ if block_given?
48
+ result << yield(line)
42
49
  else
43
- out = $stdout
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
@@ -0,0 +1,6 @@
1
+ # comment
2
+ 1
3
+ 2
4
+ 3
5
+ 4
6
+ # last comment
data/spec/numbers.txt ADDED
@@ -0,0 +1,4 @@
1
+ 1
2
+ 2
3
+ 3
4
+ 4
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.1
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-10-26 00:00:00.000000000 Z
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