csv_generator 0.5.1 → 0.5.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9d9a9aab0ef93184107fb29217cd482102a8b8ab
4
- data.tar.gz: ec424aba1eb32cba378f3815bb529e18723f4693
3
+ metadata.gz: b7ca333b9d9a9cdd6246dd186c3665cd016859f1
4
+ data.tar.gz: eb87e512cd89d733dcd4af3137050a1973137afa
5
5
  SHA512:
6
- metadata.gz: a9e9c3ea8d1884d2778661b02d0464503304867f0dda0fe7671d3daba33d9bce50d300259b58d1b6f91dab6119fec99eae939da32bd3e277aa97f6327402fa79
7
- data.tar.gz: 387487571fc882d52747affd8178e3a4e9e9bdfdcb7201a32ef2fb09eab9bad7d6589e28f58ec5c664554d0189a8f6ccca7b8c3a7fdccbbd6934728c7dc80115
6
+ metadata.gz: bd81198e0248578d24a566d1861281bf8a2313094b4f7133bcf4e879c50687524fa85d26a53aebfa4e4a3eacb1418536003ffff3086c0a3138be4af838ebf13b
7
+ data.tar.gz: 9fa44baa803d84359f7aeb55a64f8b0f1e7d7a9335657d0c792d50706e336de8047bba8d9bd35634d4467b3b6c56ce01f9ef9f052eb17230fa753d0a22a5b311
data/lib/csv_generator.rb CHANGED
@@ -37,7 +37,7 @@ class CsvGenerator
37
37
  end
38
38
 
39
39
  class RowGenerator
40
- KNOWN_OPTIONS = %i(line_separator field_separator quote_charactor).freeze
40
+ KNOWN_OPTIONS = %i(line_separator field_separator quote_character).freeze
41
41
 
42
42
  attr_reader(*KNOWN_OPTIONS)
43
43
  attr_reader :escaped_quote
@@ -47,7 +47,7 @@ class CsvGenerator
47
47
  instance_variable_set :"@#{k}", v
48
48
  end
49
49
 
50
- @escaped_quote = quote_charactor * 2
50
+ @escaped_quote = quote_character * 2
51
51
  end
52
52
 
53
53
  private
@@ -60,7 +60,7 @@ class CsvGenerator
60
60
  {
61
61
  line_separator: "\r\n",
62
62
  field_separator: ',',
63
- quote_charactor: '"',
63
+ quote_character: '"',
64
64
  }
65
65
  end
66
66
 
@@ -82,14 +82,16 @@ class CsvGenerator
82
82
  ''
83
83
  when String
84
84
  quote value
85
+ when Fixnum, Float
86
+ value.to_s
85
87
  else
86
- value
88
+ stringify value.to_s
87
89
  end
88
90
  end
89
91
 
90
92
  def quote(str)
91
- escaped_string = str.gsub quote_charactor, escaped_quote
92
- %("#{escaped_string}")
93
+ escaped_string = str.gsub quote_character, escaped_quote
94
+ quote_character + escaped_string + quote_character
93
95
  end
94
96
  end
95
97
  end
@@ -1,3 +1,3 @@
1
1
  class CsvGenerator
2
- VERSION = '0.5.1'
2
+ VERSION = '0.5.2'
3
3
  end
@@ -83,6 +83,38 @@ describe CsvGenerator do
83
83
  end
84
84
  end
85
85
 
86
+ context 'given float values' do
87
+ specify do
88
+ csv << [1.23, 9.876]
89
+
90
+ expect(io.string).to eq %(1.23,9.876\r\n)
91
+ end
92
+ end
93
+
94
+ context 'given negative numbers' do
95
+ specify do
96
+ csv << [-123, -0.987]
97
+
98
+ expect(io.string).to eq %(-123,-0.987\r\n)
99
+ end
100
+ end
101
+
102
+ context 'given nil' do
103
+ specify do
104
+ csv << ['test', nil, 123]
105
+
106
+ expect(io.string).to eq %("test",,123\r\n)
107
+ end
108
+ end
109
+
110
+ context 'given dates' do
111
+ specify do
112
+ csv << [123, Date.new(2014, 12, 6)]
113
+
114
+ expect(io.string).to eq %(123,"2014-12-06"\r\n)
115
+ end
116
+ end
117
+
86
118
  context 'when called more than once' do
87
119
  specify do
88
120
  csv << ['test1', 123]
@@ -92,4 +124,39 @@ describe CsvGenerator do
92
124
  end
93
125
  end
94
126
  end
127
+
128
+ context 'with options' do
129
+ context 'given line_separator' do
130
+ let(:csv) { CsvGenerator.new io, line_separator: "\n" }
131
+ let(:io) { StringIO.new }
132
+
133
+ specify do
134
+ csv << ['test', 123]
135
+
136
+ expect(io.string).to eq %("test",123\n)
137
+ end
138
+ end
139
+
140
+ context 'given field_separator' do
141
+ let(:csv) { CsvGenerator.new io, field_separator: "\t" }
142
+ let(:io) { StringIO.new }
143
+
144
+ specify do
145
+ csv << ['test', 123]
146
+
147
+ expect(io.string).to eq %("test"\t123\r\n)
148
+ end
149
+ end
150
+
151
+ context 'given quote_character' do
152
+ let(:csv) { CsvGenerator.new io, quote_character: "'" }
153
+ let(:io) { StringIO.new }
154
+
155
+ specify do
156
+ csv << ['test', %q('single'), %q("double"), 123]
157
+
158
+ expect(io.string).to eq %('test','''single''','"double"',123\r\n)
159
+ end
160
+ end
161
+ end
95
162
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: csv_generator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - SAWADA Tadashi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-03 00:00:00.000000000 Z
11
+ date: 2014-12-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler