fastercsv 1.1.0 → 1.1.1

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/CHANGELOG CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  Below is a complete listing of changes for each revision of FasterCSV.
4
4
 
5
+ == 1.1.1
6
+
7
+ * Added a <tt>:force_quotes</tt> output option.
8
+
5
9
  == 1.1.0
6
10
 
7
11
  * Added empty?(), length(), and size() methods to FasterCSV::Row and
data/lib/faster_csv.rb CHANGED
@@ -75,7 +75,7 @@ require "stringio"
75
75
  #
76
76
  class FasterCSV
77
77
  # The version of the installed library.
78
- VERSION = "1.1.0".freeze
78
+ VERSION = "1.1.1".freeze
79
79
 
80
80
  #
81
81
  # A FasterCSV::Row is part Array and part Hash. It retains an order for the
@@ -774,6 +774,7 @@ class FasterCSV
774
774
  # <b><tt>:return_headers</tt></b>:: +false+
775
775
  # <b><tt>:header_converters</tt></b>:: +nil+
776
776
  # <b><tt>:skip_blanks</tt></b>:: +false+
777
+ # <b><tt>:force_quotes</tt></b>:: +false+
777
778
  #
778
779
  DEFAULT_OPTIONS = { :col_sep => ",",
779
780
  :row_sep => :auto,
@@ -782,7 +783,8 @@ class FasterCSV
782
783
  :headers => false,
783
784
  :return_headers => false,
784
785
  :header_converters => nil,
785
- :skip_blanks => false }.freeze
786
+ :skip_blanks => false,
787
+ :force_quotes => false }.freeze
786
788
 
787
789
  #
788
790
  # This method will build a drop-in replacement for many of the standard CSV
@@ -1292,6 +1294,8 @@ class FasterCSV
1292
1294
  # <b><tt>:skip_blanks</tt></b>:: When set to a +true+ value, FasterCSV
1293
1295
  # will skip over any rows with no
1294
1296
  # content.
1297
+ # <b><tt>:force_quotes</tt></b>:: When set to a +true+ value, FasterCSV
1298
+ # will quote all CSV fields it creates.
1295
1299
  #
1296
1300
  # See FasterCSV::DEFAULT_OPTIONS for the default settings.
1297
1301
  #
@@ -1354,19 +1358,7 @@ class FasterCSV
1354
1358
  # handle FasterCSV::Row objects
1355
1359
  row = row.fields if row.is_a? self.class::Row
1356
1360
 
1357
- @io << row.map do |field|
1358
- if field.nil? # represent +nil+ fields as empty unquoted fields
1359
- ""
1360
- else
1361
- field = String(field) # Stringify fields
1362
- # represent empty fields as empty quoted fields
1363
- if field.empty? or field.count(%Q{\r\n#{@col_sep}"}).nonzero?
1364
- %Q{"#{field.gsub('"', '""')}"} # escape quoted fields
1365
- else
1366
- field # unquoted field
1367
- end
1368
- end
1369
- end.join(@col_sep) + @row_sep # add separators
1361
+ @io << row.map(&@quote).join(@col_sep) + @row_sep # quote and separate
1370
1362
 
1371
1363
  self # for chaining
1372
1364
  end
@@ -1573,6 +1565,8 @@ class FasterCSV
1573
1565
  # +STDERR+ and any stream open for output only with a default
1574
1566
  # <tt>@row_sep</tt> of <tt>$INPUT_RECORD_SEPARATOR</tt> (<tt>$/</tt>).
1575
1567
  #
1568
+ # This method also establishes the quoting rules used for CSV output.
1569
+ #
1576
1570
  def init_separators(options)
1577
1571
  # store the selected separators
1578
1572
  @col_sep = options.delete(:col_sep)
@@ -1611,6 +1605,25 @@ class FasterCSV
1611
1605
  end
1612
1606
  end
1613
1607
  end
1608
+
1609
+ # establish quoting rules
1610
+ @quote = if options.delete(:force_quotes)
1611
+ lambda { |field| %Q{"#{String(field).gsub('"', '""')}"} }
1612
+ else
1613
+ lambda do |field|
1614
+ if field.nil? # represent +nil+ fields as empty unquoted fields
1615
+ ""
1616
+ else
1617
+ field = String(field) # Stringify fields
1618
+ # represent empty fields as empty quoted fields
1619
+ if field.empty? or field.count(%Q{\r\n#{@col_sep}"}).nonzero?
1620
+ %Q{"#{field.gsub('"', '""')}"} # escape quoted fields
1621
+ else
1622
+ field # unquoted field
1623
+ end
1624
+ end
1625
+ end
1626
+ end
1614
1627
  end
1615
1628
 
1616
1629
  # Pre-compiles parsers and stores them by name for access during reads.
@@ -87,4 +87,10 @@ class TestFasterCSVWriting < Test::Unit::TestCase
87
87
  assert_equal( "a,b,,c\r\n", FasterCSV.generate_line( ["a", "b", nil, "c"],
88
88
  :row_sep => "\r\n" ) )
89
89
  end
90
+
91
+ def test_force_quotes
92
+ assert_equal( %Q{"1","b","","already ""quoted"""\n},
93
+ FasterCSV.generate_line( [1, "b", nil, %Q{already "quoted"}],
94
+ :force_quotes => true ) )
95
+ end
90
96
  end
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.8.11
2
+ rubygems_version: 0.9.1
3
3
  specification_version: 1
4
4
  name: fastercsv
5
5
  version: !ruby/object:Gem::Version
6
- version: 1.1.0
7
- date: 2006-12-12 00:00:00 -06:00
6
+ version: 1.1.1
7
+ date: 2007-01-31 00:00:00 -06:00
8
8
  summary: FasterCSV is CSV, but faster, smaller, and cleaner.
9
9
  require_paths:
10
10
  - lib
@@ -25,6 +25,7 @@ required_ruby_version: !ruby/object:Gem::Version::Requirement
25
25
  platform: ruby
26
26
  signing_key:
27
27
  cert_chain:
28
+ post_install_message:
28
29
  authors:
29
30
  - James Edward Gray II
30
31
  files: