fasterer-csv 0.0.7 → 0.0.8
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.
- data/lib/fasterer_csv.rb +62 -12
- metadata +3 -3
data/lib/fasterer_csv.rb
CHANGED
@@ -182,22 +182,72 @@ module FastererCSV
|
|
182
182
|
end
|
183
183
|
end
|
184
184
|
|
185
|
+
class NumConverter < Array
|
186
|
+
|
187
|
+
def initialize
|
188
|
+
@int = true
|
189
|
+
@float = true
|
190
|
+
@dot = false
|
191
|
+
end
|
192
|
+
|
193
|
+
def clear
|
194
|
+
@int = true
|
195
|
+
@float = true
|
196
|
+
@dot = false
|
197
|
+
super
|
198
|
+
end
|
199
|
+
|
200
|
+
def <<(ch)
|
201
|
+
if (ch > ?9 || ch < ?0) && ch != ?.
|
202
|
+
@int = false
|
203
|
+
@float = false
|
204
|
+
elsif ch == ?. && @dot
|
205
|
+
@int = false
|
206
|
+
@float = false
|
207
|
+
elsif ch == ?.
|
208
|
+
@int = false
|
209
|
+
@dot = true
|
210
|
+
end
|
211
|
+
|
212
|
+
super(ch.chr)
|
213
|
+
end
|
214
|
+
|
215
|
+
def join
|
216
|
+
if @int
|
217
|
+
super.to_i
|
218
|
+
elsif @float
|
219
|
+
super.to_f
|
220
|
+
else
|
221
|
+
super
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
225
|
+
end
|
226
|
+
|
227
|
+
class NoConverter < Array
|
228
|
+
|
229
|
+
def <<(ch)
|
230
|
+
super(ch.chr)
|
231
|
+
end
|
232
|
+
|
233
|
+
end
|
234
|
+
|
185
235
|
class << self
|
186
236
|
|
187
|
-
def headers(file, quot = '~', sep = ',', fail_on_malformed = true, &block)
|
188
|
-
parse_headers(File.open(file, 'r') {|io| io.gets }, quot, sep, fail_on_malformed, &block)
|
237
|
+
def headers(file, quot = '~', sep = ',', fail_on_malformed = true, column = NumConverter.new, &block)
|
238
|
+
parse_headers(File.open(file, 'r') {|io| io.gets }, quot, sep, fail_on_malformed, column, &block)
|
189
239
|
end
|
190
240
|
|
191
|
-
def read(file, quot = '~', sep = ',', fail_on_malformed = true, &block)
|
192
|
-
parse(File.open(file, 'r') { |io| io.sysread(File.size(file)) }, quot, sep, fail_on_malformed, &block)
|
241
|
+
def read(file, quot = '~', sep = ',', fail_on_malformed = true, column = NumConverter.new, &block)
|
242
|
+
parse(File.open(file, 'r') { |io| io.sysread(File.size(file)) }, quot, sep, fail_on_malformed, column, &block)
|
193
243
|
end
|
194
244
|
|
195
|
-
def parse_headers(data, quot = '~', sep = ',', fail_on_malformed = true, &block)
|
196
|
-
parse(data, quot, sep, fail_on_malformed, &block).headers
|
245
|
+
def parse_headers(data, quot = '~', sep = ',', fail_on_malformed = true, column = NumConverter.new, &block)
|
246
|
+
parse(data, quot, sep, fail_on_malformed, column, &block).headers
|
197
247
|
end
|
198
248
|
|
199
|
-
def parse(data, quot = '~', sep = ',', fail_on_malformed = true)
|
200
|
-
q, s, row,
|
249
|
+
def parse(data, quot = '~', sep = ',', fail_on_malformed = true, column = NumConverter.new)
|
250
|
+
q, s, row, inquot, clean, maybe, table, field, endline = quot[0], sep[0], [], false, true, false, nil, true, false
|
201
251
|
|
202
252
|
data.each_byte do |c|
|
203
253
|
next if c == ?\r
|
@@ -219,12 +269,12 @@ module FastererCSV
|
|
219
269
|
elsif clean && c == q
|
220
270
|
inquot, clean, endline = true, false, false
|
221
271
|
elsif maybe && c == q
|
222
|
-
column << c
|
272
|
+
column << c
|
223
273
|
clean, maybe, endline = false, false, false
|
224
274
|
elsif c == q
|
225
275
|
maybe, endline = true, false
|
226
276
|
elsif inquot
|
227
|
-
column << c
|
277
|
+
column << c
|
228
278
|
clean, endline = false, false
|
229
279
|
elsif c == s
|
230
280
|
row << (column.empty? ? nil : column.join)
|
@@ -253,7 +303,7 @@ module FastererCSV
|
|
253
303
|
table << row unless row.empty?
|
254
304
|
row, clean, inquot, field, endline = [], true, false, false, true
|
255
305
|
else
|
256
|
-
column << c
|
306
|
+
column << c
|
257
307
|
clean, endline = false, false
|
258
308
|
end
|
259
309
|
end
|
@@ -299,7 +349,7 @@ module FastererCSV
|
|
299
349
|
def initialize(file, quot = '~', sep = ',') @io = file; @quot = quot; @sep = sep end
|
300
350
|
def <<(row)
|
301
351
|
raise "can only write arrays! #{row.class} #{row.inspect}" unless row.class == Array || row.class == Row
|
302
|
-
@io.syswrite FastererCSV::quot_row(row, @quot, @sep)
|
352
|
+
@io.syswrite FastererCSV::quot_row(row, @quot, @sep)
|
303
353
|
row
|
304
354
|
end
|
305
355
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fasterer-csv
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 15
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 8
|
10
|
+
version: 0.0.8
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Mason
|