entable 0.0.4 → 0.0.5
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/entable.rb +2 -2
- data/lib/entable/transformer.rb +3 -1
- data/lib/entable/version.rb +1 -1
- data/lib/entable/wrapper.rb +9 -3
- data/spec/entable/entable_spec.rb +42 -0
- data/spec/spec_helper.rb +42 -2
- metadata +2 -2
data/lib/entable.rb
CHANGED
@@ -5,8 +5,8 @@ module Entable
|
|
5
5
|
Entable::Transformer.add_transformer name, transformer, &block
|
6
6
|
end
|
7
7
|
|
8
|
-
def self.add_wrapper name, &block
|
9
|
-
Entable::Wrapper.add_wrapper name, &block
|
8
|
+
def self.add_wrapper name, klass=nil, &block
|
9
|
+
Entable::Wrapper.add_wrapper name, klass, &block
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
data/lib/entable/transformer.rb
CHANGED
@@ -5,6 +5,8 @@ module Entable::Transformer
|
|
5
5
|
end
|
6
6
|
|
7
7
|
def self.apply_transform collection, transform_name
|
8
|
-
@@transformers[transform_name.to_sym]
|
8
|
+
transformer = @@transformers[transform_name.to_sym]
|
9
|
+
raise "Unknown transformer name #{transform_name.inspect}" if transformer.nil?
|
10
|
+
transformer.call collection
|
9
11
|
end
|
10
12
|
end
|
data/lib/entable/version.rb
CHANGED
data/lib/entable/wrapper.rb
CHANGED
@@ -1,11 +1,17 @@
|
|
1
1
|
module Entable::Wrapper
|
2
|
-
def self.add_wrapper name, &block
|
2
|
+
def self.add_wrapper name, klass, &block
|
3
3
|
@@wrappers ||= { }
|
4
|
-
@@wrappers[name.to_sym] = block
|
4
|
+
@@wrappers[name.to_sym] = klass || block
|
5
5
|
end
|
6
6
|
|
7
7
|
def self.apply_wrapper name, items, *args
|
8
8
|
wrapper = @@wrappers[name.to_sym]
|
9
|
-
|
9
|
+
if wrapper.respond_to? :call
|
10
|
+
items.map { |item| wrapper.call(item, *args) }
|
11
|
+
elsif wrapper.is_a? Class
|
12
|
+
items.map { |item| wrapper.new(*item, *args) }
|
13
|
+
else
|
14
|
+
raise "Unknown wrapper name #{name.inspect}"
|
15
|
+
end
|
10
16
|
end
|
11
17
|
end
|
@@ -143,6 +143,48 @@ describe Entable do
|
|
143
143
|
</table></body></html>}
|
144
144
|
end
|
145
145
|
|
146
|
+
it "should generate a simple table, reversing names, capitalizing some, applying prefix and postfix" do
|
147
|
+
config = {
|
148
|
+
"preprocess" => {
|
149
|
+
"transform" => "alernating_boolean",
|
150
|
+
"wrap" => "reversi"
|
151
|
+
},
|
152
|
+
"columns" => [
|
153
|
+
{ "title" => "First", "content" => "%{firstname}" },
|
154
|
+
{ "title" => "Last", "content" => "%{lastname}" },
|
155
|
+
{ "title" => "Phone", "content" => "%{phone}" },
|
156
|
+
{ "title" => "Postcode", "content" => "%{postcode}" },
|
157
|
+
]
|
158
|
+
}
|
159
|
+
Exporter.new(config).to_xls(CONTACTS, "PREFIX", "POSTFIX").should == %{<html><head><meta content='application/vnd.ms-excel;charset=UTF-8' http-equiv='Content-Type'><meta content='UTF-8' http-equiv='Encoding'></head><body><table>
|
160
|
+
<tr><td>First</td> <td>Last</td> <td>Phone</td> <td>Postcode</td></tr>
|
161
|
+
<tr><td>Nanoc</td> <td>Notlad</td> <td>PREFIX01234567</td> <td>75020POSTFIX</td></tr>
|
162
|
+
<tr><td>dez</td> <td>arbmunez</td> <td>PREFIX999999</td> <td>99999POSTFIX</td></tr>
|
163
|
+
<tr><td>Maharba</td> <td>Kravdraa</td> <td>PREFIX0000000</td> <td>0POSTFIX</td></tr>
|
164
|
+
<tr><td>semaj</td> <td>ecyoj</td> <td>PREFIX3647583</td> <td>75001POSTFIX</td></tr>
|
165
|
+
</table></body></html>}
|
166
|
+
end
|
167
|
+
|
168
|
+
it "should generate an error for a nonexistent wrapper" do
|
169
|
+
config = {
|
170
|
+
"preprocess" => {
|
171
|
+
"wrap" => "not_configured"
|
172
|
+
},
|
173
|
+
"columns" => [{ "title" => "First", "content" => "%{firstname}" }]
|
174
|
+
}
|
175
|
+
Proc.new { Exporter.new(config).to_xls(CONTACTS) }.should raise_error(StandardError, "Unknown wrapper name :not_configured")
|
176
|
+
end
|
177
|
+
|
178
|
+
it "should generate an error for a nonexistent wrapper" do
|
179
|
+
config = {
|
180
|
+
"preprocess" => {
|
181
|
+
"transform" => "not_configured"
|
182
|
+
},
|
183
|
+
"columns" => [{ "title" => "First", "content" => "%{firstname}" }]
|
184
|
+
}
|
185
|
+
Proc.new { Exporter.new(config).to_xls(CONTACTS) }.should raise_error(StandardError, "Unknown transformer name :not_configured")
|
186
|
+
end
|
187
|
+
|
146
188
|
it "should escape double-quotes when necessary" do
|
147
189
|
self.class.send :include, Entable::XlsExport
|
148
190
|
quote_for_xls('2, "The Hammonds"').should == '="2, ""The Hammonds"""'
|
data/spec/spec_helper.rb
CHANGED
@@ -39,6 +39,32 @@ class ContactUpper
|
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
|
+
class ContactReverse
|
43
|
+
attr_accessor :contact, :upcasing, :prefix, :postfix
|
44
|
+
|
45
|
+
def initialize contact, upcasing, prefix, postfix
|
46
|
+
@contact, @upcasing, @prefix, @postfix = contact, upcasing, prefix, postfix
|
47
|
+
end
|
48
|
+
|
49
|
+
def firstname
|
50
|
+
f = contact.firstname.downcase.reverse
|
51
|
+
self.upcasing ? f.capitalize : f
|
52
|
+
end
|
53
|
+
|
54
|
+
def lastname
|
55
|
+
f = contact.lastname.downcase.reverse
|
56
|
+
self.upcasing ? f.capitalize : f
|
57
|
+
end
|
58
|
+
|
59
|
+
def phone
|
60
|
+
prefix + contact.phone
|
61
|
+
end
|
62
|
+
|
63
|
+
def postcode
|
64
|
+
contact.postcode + postfix
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
42
68
|
class TwiceEverything
|
43
69
|
def call collection
|
44
70
|
collection.inject([]) { |result, item|
|
@@ -49,16 +75,30 @@ class TwiceEverything
|
|
49
75
|
end
|
50
76
|
end
|
51
77
|
|
78
|
+
class AlternatingBoolean
|
79
|
+
def call collection
|
80
|
+
b = true
|
81
|
+
collection.inject([]) { |result, item|
|
82
|
+
result << [item, b]
|
83
|
+
b = !b
|
84
|
+
result
|
85
|
+
}
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
52
89
|
Entable.add_transformer :sort_by_last_name do |collection|
|
53
90
|
collection.sort_by &:lastname
|
54
91
|
end
|
55
92
|
|
56
93
|
Entable.add_transformer :double_each_item, TwiceEverything.new
|
94
|
+
Entable.add_transformer :alernating_boolean, AlternatingBoolean.new
|
57
95
|
|
58
96
|
Entable.add_wrapper :uppercase do |item|
|
59
97
|
ContactUpper.new item
|
60
98
|
end
|
61
99
|
|
100
|
+
Entable.add_wrapper :reversi, ContactReverse
|
101
|
+
|
62
102
|
class Exporter
|
63
103
|
include Entable::XlsExport
|
64
104
|
include Entable::HtmlBuilder
|
@@ -69,8 +109,8 @@ class Exporter
|
|
69
109
|
self.config = config
|
70
110
|
end
|
71
111
|
|
72
|
-
def to_xls items
|
73
|
-
build_interpreter(config).to_xls items
|
112
|
+
def to_xls items, *args
|
113
|
+
build_interpreter(config).to_xls items, *args
|
74
114
|
end
|
75
115
|
end
|
76
116
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: entable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-06-
|
12
|
+
date: 2013-06-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|