fasterer-csv 0.0.12 → 0.0.14

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/fasterer_csv.rb +16 -13
  2. metadata +3 -3
data/lib/fasterer_csv.rb CHANGED
@@ -1,5 +1,3 @@
1
- # Say hi to Rex for me!
2
-
3
1
  require 'rubygems'
4
2
  require 'stringio'
5
3
 
@@ -24,7 +22,7 @@ module FastererCSV
24
22
 
25
23
  def <<(row)
26
24
  @lines += 1
27
- if row.class != Row
25
+ if !row.is_a?(Row)
28
26
  row = Row.new(self, row, @lines)
29
27
  end
30
28
  if @headers.length != row.length
@@ -133,7 +131,7 @@ module FastererCSV
133
131
 
134
132
  def [](*is)
135
133
  is.each do |i|
136
- val = if i.class == Fixnum
134
+ val = if i.is_a? Fixnum
137
135
  super
138
136
  else
139
137
  found = headers.index(Row::to_key(i))
@@ -145,7 +143,7 @@ module FastererCSV
145
143
  end
146
144
 
147
145
  def []=(key, val)
148
- if key.class == Fixnum
146
+ if key.is_a? Fixnum
149
147
  super
150
148
  else
151
149
  key = Row::to_key(key)
@@ -162,7 +160,7 @@ module FastererCSV
162
160
  end
163
161
 
164
162
  def merge(row)
165
- if row.class == Row
163
+ if row.is_a? Row
166
164
  row.headers.each do |header|
167
165
  self[header] = row[header]
168
166
  end
@@ -335,27 +333,33 @@ module FastererCSV
335
333
  end
336
334
 
337
335
  def quot_row(row, q = '~', s = ',')
338
- needs_quot = /(?:[#{q}#{s}\n]|^\d+$)/
336
+ num_quot = /(?:[#{q}#{s}\n]|^\d+$)/
337
+ need_quot = /[#{q}#{s}\n]/
339
338
  row.map do |val|
340
339
  if val.nil?
341
340
  ""
342
- elsif val.class == Numeric
341
+ elsif val.is_a? Numeric
343
342
  val.to_s
344
343
  else
344
+ quot = val.is_a?(Symbol) ? need_quot : num_quot
345
345
  val = String(val)
346
346
  if val.length == 0
347
- q * 2
347
+ q * 2
348
348
  else
349
- val[needs_quot] ? q + val.gsub(q, q * 2) + q : val
349
+ val[quot] ? q + val.gsub(q, q * 2) + q : val
350
350
  end
351
351
  end
352
352
  end.join(s) + "\n"
353
353
  end
354
354
 
355
355
  class IOWriter
356
- def initialize(file, quot = '~', sep = ',') @io = file; @quot = quot; @sep = sep end
356
+ def initialize(file, quot = '~', sep = ',') @first = true; @io = file; @quot = quot; @sep = sep end
357
357
  def <<(row)
358
- raise "can only write arrays! #{row.class} #{row.inspect}" unless row.class == Array || row.class == Row
358
+ raise "can only write arrays! #{row.class} #{row.inspect}" unless row.is_a? Array
359
+ if @first && row.is_a?(Row)
360
+ @first = false
361
+ self.<<(row.headers)
362
+ end
359
363
  @io.syswrite FastererCSV::quot_row(row, @quot, @sep)
360
364
  row
361
365
  end
@@ -378,4 +382,3 @@ module FastererCSV
378
382
  end
379
383
  end
380
384
  end
381
-
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: 7
4
+ hash: 3
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 12
10
- version: 0.0.12
9
+ - 14
10
+ version: 0.0.14
11
11
  platform: ruby
12
12
  authors:
13
13
  - Mason