csv_generator 0.5.1 → 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/csv_generator.rb +8 -6
- data/lib/csv_generator/version.rb +1 -1
- data/spec/csv_generator_spec.rb +67 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b7ca333b9d9a9cdd6246dd186c3665cd016859f1
|
4
|
+
data.tar.gz: eb87e512cd89d733dcd4af3137050a1973137afa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
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 =
|
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
|
-
|
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
|
92
|
-
|
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
|
data/spec/csv_generator_spec.rb
CHANGED
@@ -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.
|
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-
|
11
|
+
date: 2014-12-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|