atv 1.0.0 → 1.0.1

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
  SHA256:
3
- metadata.gz: 01241ccb91e5d82a64f6ea850637960f09ae9a311cf2c246776df57e04373df5
4
- data.tar.gz: 5a04b4843e859ea00808415f876b966cd30ed4eb9330429bb2846b034e07ec1a
3
+ metadata.gz: c3ddffe7d3ab226d5c2b6e5a82e6b2a2f174c0313dba16e63c1df29548d3f5e4
4
+ data.tar.gz: e1026661b09ad6c27088cffbb0ed9ad6804498502b0e11f907df673c066df002
5
5
  SHA512:
6
- metadata.gz: 41f03eb6c3e36e1925c7dca47652ca96d3729ffbb20854219ccd28b8a55f2b1c5c9021978ac0f2290c4492dfa927a24a43414486dd2b0f0a8046831b7b0c7d19
7
- data.tar.gz: dadd30ee857e755648608d7240ab8fd03d1333a56cdfb2b3623eabd09591d89286f7d04e05be7e230ef1b0b33043ddfb45925a534443bf9d2e3e7d73c3caa3b0
6
+ metadata.gz: abeca3e13187543d8b97a3c671673113968239b0b093a61eb4a2a4649d1d481ccd1bcdc3ee061e8f5b1faf5937147ab77bd9b1d100f1f69d9740fa9a373320a8
7
+ data.tar.gz: fd4d95942343225917209524263a7fc4385cc786e217037072b08077f6e13f8dd3974e3d73f688fedca2fb2015b5bba87da61b3a3bfd08efeabad53801a15873
@@ -20,5 +20,5 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.7"
22
22
  spec.add_development_dependency "rake", "~> 10.0"
23
- spec.add_development_dependency "minitest", "~> 5.11"
23
+ spec.add_development_dependency "minitest", "~> 5"
24
24
  end
data/lib/atv.rb CHANGED
@@ -11,6 +11,9 @@ class ATV
11
11
  '' => nil
12
12
  }
13
13
 
14
+ SEPARATOR_LINE_RE = /^\s*\|\-/
15
+ COMMENT_LINE_RE = /^\s*#/
16
+
14
17
  attr_reader :headers
15
18
 
16
19
  def initialize(io)
@@ -25,9 +28,8 @@ class ATV
25
28
  def each
26
29
  line_data = []
27
30
  @io.each_line do |line|
28
- next if line =~ /^\s*#/
29
- line.chomp!
30
- if (!@has_separators && !line_data.empty?) || line =~ /^\|\-/
31
+ next if line =~ COMMENT_LINE_RE
32
+ if (!@has_separators && !line_data.empty?) || line =~ SEPARATOR_LINE_RE
31
33
  folded_items = line_data.transpose.map { |tokens| tokens.join(' ').rstrip }
32
34
  converted_folded_items = folded_items.map { |token| SUBSTITUTIONS.has_key?(token) ? SUBSTITUTIONS[token] : token }
33
35
  csv_row = CSV::Row.new(@headers, converted_folded_items)
@@ -46,6 +48,7 @@ class ATV
46
48
  protected
47
49
 
48
50
  def split_table_line(line)
51
+ line.strip!
49
52
  return [] if line.empty?
50
53
  line[1..-1].split('|').map(&:strip)
51
54
  end
@@ -56,7 +59,7 @@ class ATV
56
59
  @io.readline
57
60
  separator_count = 0
58
61
  @io.each_line do |line|
59
- if line =~ /^\|\-/
62
+ if line =~ SEPARATOR_LINE_RE
60
63
  separator_count += 1
61
64
  return true if separator_count > 1
62
65
  end
@@ -1,3 +1,3 @@
1
1
  module Atv
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
@@ -3,8 +3,9 @@ require_relative '../lib/atv'
3
3
 
4
4
  describe ATV do
5
5
  describe 'with separators' do
6
- before do
7
- @data_as_table = <<-TEXT
6
+ describe '#each' do
7
+ it 'with a block it yields rows of data as CSV rows and ignores commented lines' do
8
+ data_as_table = <<-TEXT
8
9
  |-----------+--------------------+--------------|
9
10
  | name | dob | predictable? |
10
11
  |-----------+--------------------+--------------|
@@ -22,59 +23,133 @@ describe ATV do
22
23
  | Derrial | null | true |
23
24
  | Book | | |
24
25
  |-----------+--------------------+--------------|
25
- TEXT
26
- @expected = [
26
+ TEXT
27
+
28
+ expected = [
27
29
  ['Malcolm Reynolds', 'September 20, 2468', false],
28
30
  ['Zoe Washburne', 'February 15, 2484', nil],
29
31
  ['Jane', nil, true],
30
32
  ['Derrial Book', nil, true]
31
- ]
32
- end
33
-
34
- describe '#each' do
35
- before do
36
- @atv = ATV.new(StringIO.new(@data_as_table))
37
- end
33
+ ]
38
34
 
39
- it 'with a block it yields rows of data as CSV rows and ignores commented lines' do
40
35
  i = 0
41
36
 
42
- @atv.each do |row|
43
- row.fields.must_equal(@expected[i])
44
- row[0].must_equal(@expected[i][0])
45
- row['name'].must_equal(@expected[i][0])
46
- row[1].must_equal(@expected[i][1])
47
- row['dob'].must_equal(@expected[i][1])
48
- row[2].must_equal(@expected[i][2])
49
- row['predictable?'].must_equal(@expected[i][2])
37
+ ATV.new(StringIO.new(data_as_table)).each do |row|
38
+ row.fields.must_equal(expected[i])
39
+ row[0].must_equal(expected[i][0])
40
+ row['name'].must_equal(expected[i][0])
41
+ row[1].must_equal(expected[i][1])
42
+ row['dob'].must_equal(expected[i][1])
43
+ row[2].must_equal(expected[i][2])
44
+ row['predictable?'].must_equal(expected[i][2])
50
45
  i += 1
51
46
  end
52
47
  i.must_equal(4)
53
48
  end
54
49
 
55
50
  it 'without a block it returns an enumerator of data as CSV rows' do
56
- enum = @atv.enum_for
57
- enum.map(&:fields).must_equal(@expected)
51
+ data_as_table = <<-TEXT
52
+ |-----------+--------------------+--------------|
53
+ | name | dob | predictable? |
54
+ |-----------+--------------------+--------------|
55
+ | Malcolm | September 20, 2468 | false |
56
+ | Reynolds | | |
57
+ |-----------+--------------------+--------------|
58
+ | Zoe | February 15, 2484 | |
59
+ | Washburne | | |
60
+ |-----------+--------------------+--------------|
61
+ TEXT
62
+
63
+ expected = [
64
+ ['Malcolm Reynolds', 'September 20, 2468', false],
65
+ ['Zoe Washburne', 'February 15, 2484', nil],
66
+ ]
67
+
68
+ atv = ATV.new(StringIO.new(data_as_table))
69
+ enum = atv.enum_for
70
+ enum.map(&:fields).must_equal(expected)
58
71
  end
59
72
  end
60
73
 
61
74
  describe "#headers" do
62
75
  it 'returns all the headers' do
63
- ATV.new(StringIO.new(@data_as_table)).headers.must_equal(%w|name dob predictable?|)
76
+ data_as_table = <<-TEXT
77
+ |-----------+--------------------+--------------|
78
+ | name | dob | predictable? |
79
+ |-----------+--------------------+--------------|
80
+ | Malcolm | September 20, 2468 | false |
81
+ | Reynolds | | |
82
+ |-----------+--------------------+--------------|
83
+ | Zoe | February 15, 2484 | |
84
+ | Washburne | | |
85
+ |-----------+--------------------+--------------|
86
+ TEXT
87
+ ATV.new(StringIO.new(data_as_table)).headers.must_equal(%w|name dob predictable?|)
64
88
  end
65
89
  end
66
90
 
67
91
  describe ".from_string(string)" do
68
92
  it 'initializes ATV with an IO object' do
93
+ data_as_table = <<-TEXT
94
+ |-----------+--------------------+--------------|
95
+ | name | dob | predictable? |
96
+ |-----------+--------------------+--------------|
97
+ | Malcolm | September 20, 2468 | false |
98
+ | Reynolds | | |
99
+ |-----------+--------------------+--------------|
100
+ | Zoe | February 15, 2484 | |
101
+ | Washburne | | |
102
+ |-----------+--------------------+--------------|
103
+ TEXT
104
+
69
105
  fussy = -> (io) { io.must_be_kind_of StringIO }
70
106
 
71
107
  ATV.stub :new, fussy do
72
- ATV.from_string(@data_as_table)
108
+ ATV.from_string(data_as_table)
73
109
  end
74
110
  end
75
111
  end
76
112
  end
77
113
 
114
+ describe 'with separators, indented' do
115
+ describe '#each' do
116
+ it 'with a block it yields rows of data as CSV rows' do
117
+ data_as_table = <<TEXT
118
+ |-----------+--------------------+--------------|
119
+ | name | dob | predictable? |
120
+ |-----------+--------------------+--------------|
121
+ | Malcolm | September 20, 2468 | false |
122
+ | Reynolds | | |
123
+ |-----------+--------------------+--------------|
124
+ | Zoe | February 15, 2484 | |
125
+ | Washburne | | |
126
+ |-----------+--------------------+--------------|
127
+ TEXT
128
+
129
+ expected = [
130
+ ['Malcolm Reynolds', 'September 20, 2468', false],
131
+ ['Zoe Washburne', 'February 15, 2484', nil],
132
+ ]
133
+
134
+ atv = ATV.new(StringIO.new(data_as_table))
135
+
136
+ i = 0
137
+
138
+ atv.each do |row|
139
+ row.fields.must_equal(expected[i])
140
+ row[0].must_equal(expected[i][0])
141
+ row['name'].must_equal(expected[i][0])
142
+ row[1].must_equal(expected[i][1])
143
+ row['dob'].must_equal(expected[i][1])
144
+ row[2].must_equal(expected[i][2])
145
+ row['predictable?'].must_equal(expected[i][2])
146
+ i += 1
147
+ end
148
+ i.must_equal(2)
149
+ end
150
+ end
151
+ end
152
+
78
153
  describe 'without separators' do
79
154
  describe '#each' do
80
155
  it 'with a block it yields rows of data as CSV rows' do
@@ -89,15 +164,53 @@ describe ATV do
89
164
  TEXT
90
165
 
91
166
  expected = [
92
- ['Malcolm', 'September 20, 2468', false],
93
- ['Zoe', 'February 15, 2484', nil],
94
- ['Derrial', nil, true]
167
+ ['Malcolm', 'September 20, 2468', false],
168
+ ['Zoe', 'February 15, 2484', nil],
169
+ ['Derrial', nil, true]
170
+ ]
171
+
172
+ atv = ATV.new(StringIO.new(data_as_table))
173
+
174
+ i = 0
175
+
176
+ atv.each do |row|
177
+ row.fields.must_equal(expected[i])
178
+ row[0].must_equal(expected[i][0])
179
+ row['name'].must_equal(expected[i][0])
180
+ row[1].must_equal(expected[i][1])
181
+ row['dob'].must_equal(expected[i][1])
182
+ row[2].must_equal(expected[i][2])
183
+ row['predictable?'].must_equal(expected[i][2])
184
+ i += 1
185
+ end
186
+ i.must_equal(3)
187
+ end
188
+ end
189
+ end
190
+
191
+ describe 'without separators, indented' do
192
+ describe '#each' do
193
+ it 'with a block it yields rows of data as CSV rows' do
194
+ data_as_table = <<TEXT
195
+ |-----------+--------------------+--------------|
196
+ | name | dob | predictable? |
197
+ |-----------+--------------------+--------------|
198
+ | Malcolm | September 20, 2468 | false |
199
+ | Zoe | February 15, 2484 | |
200
+ | Derrial | null | true |
201
+ |-----------+--------------------+--------------|
202
+ TEXT
203
+
204
+ expected = [
205
+ ['Malcolm', 'September 20, 2468', false],
206
+ ['Zoe', 'February 15, 2484', nil],
207
+ ['Derrial', nil, true]
95
208
  ]
96
- @atv = ATV.new(StringIO.new(data_as_table))
209
+ atv = ATV.new(StringIO.new(data_as_table))
97
210
 
98
211
  i = 0
99
212
 
100
- @atv.each do |row|
213
+ atv.each do |row|
101
214
  row.fields.must_equal(expected[i])
102
215
  row[0].must_equal(expected[i][0])
103
216
  row['name'].must_equal(expected[i][0])
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: atv
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kelly Felkins
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-08 00:00:00.000000000 Z
11
+ date: 2018-05-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '5.11'
47
+ version: '5'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '5.11'
54
+ version: '5'
55
55
  description: Read data from an ascii table.
56
56
  email:
57
57
  - kelly@restlater.com