sequel_plus 0.1.0 → 0.1.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.
- data/lib/extensions/export.rb +26 -20
- data/test/test_export.rb +50 -29
- metadata +2 -2
data/lib/extensions/export.rb
CHANGED
@@ -1,13 +1,22 @@
|
|
1
|
-
#
|
2
|
-
#
|
3
|
-
|
1
|
+
#
|
2
|
+
# The export extension adds Sequel::Dataset#export
|
3
|
+
#
|
4
|
+
# Export with no options specified will export as tab-delimited w/o any quoting
|
5
|
+
#
|
6
|
+
# Date, Time, and DateTime are exported as ISO-8601
|
7
|
+
# http://en.wikipedia.org/wiki/ISO_8601
|
8
|
+
#
|
9
|
+
# Non-numerics are encased in given :quote_char (default is none)
|
10
|
+
# Columns are delimited by given :delimiter (default is tab character)
|
11
|
+
# Headers are emitted by default (suppress with :headers => false)
|
12
|
+
#
|
4
13
|
module Sequel
|
5
14
|
class Dataset
|
6
|
-
# outputs the records in the dataset as plain-text table.
|
7
15
|
def export(fd = $stdout, options = {})
|
8
|
-
opts[:delimiter]
|
9
|
-
opts[:quote_char] = options[:quote_char] || '
|
10
|
-
opts[:headers]
|
16
|
+
opts[:delimiter] = options[:delimiter] || "\t"
|
17
|
+
opts[:quote_char] = options[:quote_char] || ''
|
18
|
+
opts[:headers] = options[:headers] != false
|
19
|
+
|
11
20
|
Sequel::Export::Writer.new(fd, self, opts).output
|
12
21
|
end
|
13
22
|
end
|
@@ -21,15 +30,6 @@ module Sequel
|
|
21
30
|
@options = options
|
22
31
|
end
|
23
32
|
|
24
|
-
class ::Date; def to_export(q); ; end; end
|
25
|
-
class ::DateTime; def to_export(q); "#{q}#{iso8601}#{q}"; end; end
|
26
|
-
class ::Time; def to_export(q); "#{q}#{iso8601}#{q}"; end; end
|
27
|
-
class ::Float; def to_export(q); to_f.to_s; end; end
|
28
|
-
class ::BigDecimal; def to_export(q); to_f.to_s; end; end
|
29
|
-
class ::Bignum; def to_export(q); to_i.to_s; end; end
|
30
|
-
class ::Fixnum; def to_export(q); to_i.to_s; end; end
|
31
|
-
class ::Object; def to_export(q); "#{q}#{to_s}#{q}"; end; end
|
32
|
-
|
33
33
|
def output
|
34
34
|
quot = @options[:quote_char]
|
35
35
|
@columns ||= @dataset.first.keys.sort_by{|x|x.to_s}
|
@@ -41,10 +41,16 @@ module Sequel
|
|
41
41
|
@dataset.each do |row|
|
42
42
|
data = @columns.map do |col|
|
43
43
|
case row[col]
|
44
|
-
when Date
|
45
|
-
|
46
|
-
when
|
47
|
-
|
44
|
+
when Date then
|
45
|
+
"#{quot}#{row[col].strftime('%Y-%m-%d')}#{quot}"
|
46
|
+
when DateTime, Time then
|
47
|
+
"#{quot}#{row[col].localtime.strftime('%Y-%m-%dT%H:%M%Z')}#{quot}"
|
48
|
+
when Float, BigDecimal then
|
49
|
+
row[col].to_f
|
50
|
+
when BigDecimal, Bignum, Fixnum then
|
51
|
+
row[col].to_i
|
52
|
+
else
|
53
|
+
"#{quot}#{row[col].to_s}#{quot}"
|
48
54
|
end
|
49
55
|
end
|
50
56
|
@file.puts data.join(@options[:delimiter])
|
data/test/test_export.rb
CHANGED
@@ -50,11 +50,32 @@ module ExportTest
|
|
50
50
|
DB[:nodes].all.size.should == 12
|
51
51
|
end
|
52
52
|
|
53
|
-
it "should export everything" do
|
53
|
+
it "should export everything tab delimited w/o quotes" do
|
54
54
|
mem_stream = StringIO.new("", "w+")
|
55
55
|
DB[:nodes].export(mem_stream)
|
56
56
|
mem_stream.pos = 0
|
57
57
|
mem_stream.read.should == <<-TEXT
|
58
|
+
id name parent_id position
|
59
|
+
1 one 1
|
60
|
+
2 two 2
|
61
|
+
3 three 3
|
62
|
+
4 two.one 2 1
|
63
|
+
5 two.two 2 2
|
64
|
+
6 two.two.one 5 1
|
65
|
+
7 one.two 1 2
|
66
|
+
8 one.one 1 1
|
67
|
+
9 five 5
|
68
|
+
10 four 4
|
69
|
+
11 five.one 9 1
|
70
|
+
12 two.three 2 3
|
71
|
+
TEXT
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should export tab delimited with quotes" do
|
75
|
+
mem_stream = StringIO.new("", "w+")
|
76
|
+
DB[:nodes].export(mem_stream, :quote_char => '"')
|
77
|
+
mem_stream.pos = 0
|
78
|
+
mem_stream.read.should == <<-TEXT
|
58
79
|
"id" "name" "parent_id" "position"
|
59
80
|
1 "one" "" 1
|
60
81
|
2 "two" "" 2
|
@@ -73,7 +94,7 @@ module ExportTest
|
|
73
94
|
|
74
95
|
it "should export everything with comma delimiter" do
|
75
96
|
mem_stream = StringIO.new("", "w+")
|
76
|
-
DB[:nodes].export(mem_stream, :delimiter => ',')
|
97
|
+
DB[:nodes].export(mem_stream, :quote_char => '"', :delimiter => ',')
|
77
98
|
mem_stream.pos = 0
|
78
99
|
mem_stream.read.should == <<-TEXT
|
79
100
|
"id","name","parent_id","position"
|
@@ -94,7 +115,7 @@ module ExportTest
|
|
94
115
|
|
95
116
|
it "should export everything with comma delimiter and no quote characters" do
|
96
117
|
mem_stream = StringIO.new("", "w+")
|
97
|
-
DB[:nodes].export(mem_stream, :delimiter => ','
|
118
|
+
DB[:nodes].export(mem_stream, :delimiter => ',')
|
98
119
|
mem_stream.pos = 0
|
99
120
|
mem_stream.read.should == <<-TEXT
|
100
121
|
id,name,parent_id,position
|
@@ -117,7 +138,7 @@ id,name,parent_id,position
|
|
117
138
|
mem_stream = StringIO.new("", "w+")
|
118
139
|
DB[:nodes].filter(:id < 3).select(:id, :name).export(mem_stream)
|
119
140
|
mem_stream.pos = 0
|
120
|
-
mem_stream.read.should == "
|
141
|
+
mem_stream.read.should == "id\tname\n1\tone\n2\ttwo\n"
|
121
142
|
end
|
122
143
|
|
123
144
|
it "should not export headers" do
|
@@ -125,18 +146,18 @@ id,name,parent_id,position
|
|
125
146
|
DB[:nodes].export(mem_stream, :headers => false)
|
126
147
|
mem_stream.pos = 0
|
127
148
|
mem_stream.read.should == <<-TEXT
|
128
|
-
1
|
129
|
-
2
|
130
|
-
3
|
131
|
-
4
|
132
|
-
5
|
133
|
-
6
|
134
|
-
7
|
135
|
-
8
|
136
|
-
9
|
137
|
-
10
|
138
|
-
11
|
139
|
-
12
|
149
|
+
1 one 1
|
150
|
+
2 two 2
|
151
|
+
3 three 3
|
152
|
+
4 two.one 2 1
|
153
|
+
5 two.two 2 2
|
154
|
+
6 two.two.one 5 1
|
155
|
+
7 one.two 1 2
|
156
|
+
8 one.one 1 1
|
157
|
+
9 five 5
|
158
|
+
10 four 4
|
159
|
+
11 five.one 9 1
|
160
|
+
12 two.three 2 3
|
140
161
|
TEXT
|
141
162
|
end
|
142
163
|
|
@@ -145,19 +166,19 @@ id,name,parent_id,position
|
|
145
166
|
DB[:nodes].export(mem_stream, :headers => true)
|
146
167
|
mem_stream.pos = 0
|
147
168
|
mem_stream.read.should == <<-TEXT
|
148
|
-
|
149
|
-
1
|
150
|
-
2
|
151
|
-
3
|
152
|
-
4
|
153
|
-
5
|
154
|
-
6
|
155
|
-
7
|
156
|
-
8
|
157
|
-
9
|
158
|
-
10
|
159
|
-
11
|
160
|
-
12
|
169
|
+
id name parent_id position
|
170
|
+
1 one 1
|
171
|
+
2 two 2
|
172
|
+
3 three 3
|
173
|
+
4 two.one 2 1
|
174
|
+
5 two.two 2 2
|
175
|
+
6 two.two.one 5 1
|
176
|
+
7 one.two 1 2
|
177
|
+
8 one.one 1 1
|
178
|
+
9 five 5
|
179
|
+
10 four 4
|
180
|
+
11 five.one 9 1
|
181
|
+
12 two.three 2 3
|
161
182
|
TEXT
|
162
183
|
end
|
163
184
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sequel_plus
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Lang
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-02-
|
12
|
+
date: 2010-02-13 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|