osheet 0.10.0 → 1.0.0.rc.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. data/Gemfile +0 -1
  2. data/Gemfile.lock +9 -6
  3. data/Rakefile +35 -19
  4. data/bench/bench_runner.rb +91 -0
  5. data/bench/profiler_runner.rb +1 -0
  6. data/examples/basic.rb +1 -1
  7. data/examples/basic.xls +2 -1
  8. data/examples/basic_with_templates.rb +2 -2
  9. data/examples/basic_with_templates.xls +3 -3
  10. data/examples/formats.rb +2 -2
  11. data/examples/formats.xls +46 -46
  12. data/examples/formula.rb +2 -2
  13. data/examples/styles.rb +2 -2
  14. data/examples/styles.xls +5 -5
  15. data/examples/trivial.rb +2 -2
  16. data/lib/osheet/cell.rb +48 -46
  17. data/lib/osheet/column.rb +23 -29
  18. data/lib/osheet/format.rb +3 -3
  19. data/lib/osheet/meta_element.rb +2 -1
  20. data/lib/osheet/mixin.rb +21 -9
  21. data/lib/osheet/partial.rb +5 -9
  22. data/lib/osheet/row.rb +28 -32
  23. data/lib/osheet/style.rb +11 -25
  24. data/lib/osheet/styled_element.rb +9 -1
  25. data/lib/osheet/template.rb +3 -8
  26. data/lib/osheet/version.rb +1 -1
  27. data/lib/osheet/workbook.rb +135 -43
  28. data/lib/osheet/workbook_api.rb +208 -0
  29. data/lib/osheet/workbook_element.rb +225 -8
  30. data/lib/osheet/worksheet.rb +22 -28
  31. data/lib/osheet/xmlss_writer/style_cache.rb +64 -0
  32. data/lib/osheet/xmlss_writer/style_settings.rb +148 -0
  33. data/lib/osheet/xmlss_writer.rb +143 -1
  34. data/lib/osheet.rb +3 -29
  35. data/osheet.gemspec +4 -1
  36. data/test/cell_test.rb +33 -98
  37. data/test/column_test.rb +20 -88
  38. data/test/{mixins.rb → fixtures/mixins.rb} +6 -4
  39. data/test/fixtures/test_writer.rb +68 -0
  40. data/test/format_test.rb +2 -2
  41. data/test/helper.rb +34 -68
  42. data/test/mixin_test.rb +37 -43
  43. data/test/partial_test.rb +3 -26
  44. data/test/row_test.rb +32 -85
  45. data/test/style_test.rb +9 -26
  46. data/test/template_test.rb +5 -6
  47. data/test/workbook_element_test.rb +231 -0
  48. data/test/workbook_test.rb +225 -116
  49. data/test/worksheet_test.rb +51 -98
  50. data/test/xmlss_writer/api_test.rb +139 -0
  51. data/test/xmlss_writer/style_cache_test.rb +65 -0
  52. data/test/xmlss_writer/style_settings_test.rb +263 -0
  53. data/test/xmlss_writer/styles_test.rb +121 -153
  54. data/test/xmlss_writer_test.rb +91 -0
  55. metadata +75 -50
  56. data/lib/osheet/associations.rb +0 -58
  57. data/lib/osheet/instance.rb +0 -30
  58. data/lib/osheet/markup_element.rb +0 -22
  59. data/lib/osheet/partial_set.rb +0 -57
  60. data/lib/osheet/railtie.rb +0 -9
  61. data/lib/osheet/style_set.rb +0 -39
  62. data/lib/osheet/template_set.rb +0 -51
  63. data/lib/osheet/view_handler/rails.rb +0 -44
  64. data/lib/osheet/view_handler/tilt.rb +0 -42
  65. data/lib/osheet/view_handler.rb +0 -2
  66. data/lib/osheet/worksheet_element.rb +0 -17
  67. data/lib/osheet/xmlss_writer/base.rb +0 -49
  68. data/lib/osheet/xmlss_writer/elements.rb +0 -56
  69. data/lib/osheet/xmlss_writer/styles.rb +0 -216
  70. data/test/osheet_test.rb +0 -13
  71. data/test/partial_set_test.rb +0 -64
  72. data/test/style_set_test.rb +0 -47
  73. data/test/template_set_test.rb +0 -74
  74. data/test/xmlss_writer/base_test.rb +0 -103
  75. data/test/xmlss_writer/elements_test.rb +0 -172
@@ -1,56 +0,0 @@
1
- module Osheet::XmlssWriter::Elements
2
-
3
- protected
4
-
5
- def worksheet(xworkbook, oworksheet)
6
- xworkbook.worksheet(oworksheet.attributes[:name]) do
7
- oworksheet.columns.each { |ocolumn| column(xworkbook, ocolumn) }
8
- oworksheet.rows.each { |orow| row(xworkbook, orow) }
9
- end
10
- end
11
-
12
- def column(xworkbook, ocolumn)
13
- xworkbook.column({
14
- :style_id => style_id(xworkbook, ocolumn.attributes[:style_class]),
15
- :width => ocolumn.attributes[:width],
16
- :auto_fit_width => ocolumn.attributes[:autofit],
17
- :hidden => ocolumn.attributes[:hidden]
18
- })
19
- end
20
-
21
- def row(xworkbook, orow)
22
- xworkbook.row({
23
- :style_id => style_id(xworkbook, orow.attributes[:style_class]),
24
- :height => orow.attributes[:height],
25
- :auto_fit_height => orow.attributes[:autofit],
26
- :hidden => orow.attributes[:hidden]
27
- }) do
28
- orow.cells.each { |ocell| cell(xworkbook, ocell) }
29
- end
30
- end
31
-
32
- def cell(xworkbook, ocell)
33
- xworkbook.cell({
34
- :style_id => style_id(xworkbook, ocell.attributes[:style_class], ocell.attributes[:format]),
35
- :href => ocell.attributes[:href],
36
- :index => ocell.attributes[:index],
37
- :merge_across => cell_merge(ocell.attributes[:colspan]),
38
- :merge_down => cell_merge(ocell.attributes[:rowspan]),
39
- :formula => ocell.attributes[:formula]
40
- }) do
41
- data(xworkbook, ocell.attributes[:data])
42
- end
43
- end
44
-
45
- def data(xworkbook, odata)
46
- xworkbook.data(odata)
47
- end
48
-
49
- private
50
-
51
- # convert osheet col/row span value to xmlss merge value
52
- def cell_merge(span)
53
- span.kind_of?(::Fixnum) && span > 1 ? span-1 : 0
54
- end
55
-
56
- end
@@ -1,216 +0,0 @@
1
- require 'osheet/format'
2
- module Osheet::XmlssWriter::Styles
3
-
4
- protected
5
-
6
- def style_id(xworkbook, style_class, oformat=nil)
7
- xstyle = style(xworkbook, style_class, oformat)
8
- xstyle.nil? ? '' : xstyle.id
9
- end
10
-
11
- def style(xworkbook, style_class, oformat=nil)
12
- oformat ||= Osheet::Format.new(:general)
13
-
14
- # generate the style key for the given class/format
15
- key = style_key(style_class, oformat.key)
16
-
17
- # see if we have already created/used an xmlss style for the given key
18
- xstyle = @used_xstyles.find{ |xs| xs.id == key }
19
-
20
- # if not, create an xmlss style for the key and add it to the used cache
21
- if !key.empty? && xstyle.nil?
22
- xstyle = xmlss_style(xworkbook, key, oformat)
23
- @used_xstyles << xstyle
24
- end
25
-
26
- xstyle
27
- end
28
-
29
- private
30
-
31
- def style_key(style_class, format_key)
32
- (style_class || '').strip.split(/\s+/).collect do |c|
33
- ".#{c}"
34
- end.join('') + (format_key.nil? || format_key.empty? ? '' : "..#{format_key}")
35
- end
36
-
37
- def xmlss_style(xworkbook, key, oformat)
38
- # take all the matching osheet styles for the given key,
39
- # and build up xmlss style settings for them
40
- settings = style_settings(key)
41
-
42
- xworkbook.style(key) do
43
-
44
- if settings.has_key?(:align) && !settings[:align].empty?
45
- xworkbook.alignment(settings[:align])
46
- end
47
-
48
- if settings.has_key?(:font) && !settings[:font].empty?
49
- xworkbook.font(settings[:font])
50
- end
51
-
52
- if settings.has_key?(:bg) && !settings[:bg].empty?
53
- xworkbook.interior(settings[:bg])
54
- end
55
-
56
- border_set = ::Osheet::Style::BORDERS.inject([]) do |set, bp|
57
- if settings.has_key?(bp) && !settings[bp].empty?
58
- set << settings[bp]
59
- end
60
- set
61
- end
62
- if !border_set.empty?
63
- xworkbook.borders {
64
- border_set.each { |border_setting| xworkbook.border(border_setting) }
65
- }
66
- end
67
-
68
- if oformat
69
- xworkbook.number_format(oformat.style)
70
- end
71
-
72
- end
73
- end
74
-
75
- # TODO: would be nice to have a class handle all of the osheet-xmlss style translations...
76
-
77
- def style_settings(key)
78
- @oworkbook.styles.for(key).inject({}) do |style_settings, ostyle|
79
- merged_settings(style_settings, ostyle_settings(ostyle))
80
- end
81
- end
82
-
83
- def merged_settings(current, add)
84
- # concat values for keys in both sets
85
- current.keys.each do |k|
86
- current[k].merge!(add.delete(k) || {})
87
- end
88
- # merge keys for anything not in the current
89
- current.merge(add)
90
- end
91
-
92
- def ostyle_settings(ostyle)
93
- ostyle_settings = {}
94
- ostyle.attributes.each do |k,v|
95
- unless v.empty?
96
- ostyle_settings[k] = send("#{k}_settings", v)
97
- end
98
- end
99
- ostyle_settings
100
- end
101
-
102
- def align_settings(align_cmds)
103
- align_cmds.inject({}) do |align_settings, align_cmd|
104
- if (setting = case align_cmd
105
- when :left, :center, :right
106
- [:horizontal, align_cmd]
107
- when :top, :bottom
108
- [:vertical, align_cmd]
109
- when :middle
110
- [:vertical, :center]
111
- when :wrap
112
- [:wrap_text, true]
113
- when ::Fixnum
114
- [:rotate, align_cmd]
115
- end
116
- )
117
- align_settings[setting.first] = setting.last
118
- end
119
- align_settings
120
- end
121
- end
122
-
123
- def font_settings(font_cmds)
124
- font_cmds.inject({}) do |font_settings, font_cmd|
125
- if (setting = case font_cmd
126
- when ::Fixnum
127
- [:size, font_cmd]
128
- when ::String
129
- if font_cmd =~ /^#/
130
- [:color, font_cmd]
131
- else
132
- [:name, font_cmd]
133
- end
134
- when :bold, :italic, :shadow
135
- [font_cmd, true]
136
- when :subscript, :superscript
137
- [:alignment, font_cmd]
138
- when :strikethrough
139
- [:strike_through, true]
140
- when :underline
141
- [:underline, :single]
142
- when :double_underline
143
- [:underline, :double]
144
- when :accounting_underline
145
- [:underline, :single_accounting]
146
- when :double_accounting_underline
147
- [:underline, :double_accounting]
148
- end
149
- )
150
- font_settings[setting.first] = setting.last
151
- end
152
- font_settings
153
- end
154
- end
155
-
156
- def bg_settings(bg_cmds)
157
- bg_cmds.inject({}) do |bg_settings, bg_cmd|
158
- if (settings = case bg_cmd
159
- when ::String
160
- if bg_cmd =~ /^#/
161
- [[:color, bg_cmd]]
162
- end
163
- when ::Symbol
164
- if ::Xmlss::Style::Interior.pattern_set.include?(bg_cmd)
165
- [[:pattern, bg_cmd]]
166
- end
167
- when ::Hash
168
- bg_cmd.inject([]) do |set, kv|
169
- if ::Xmlss::Style::Interior.pattern_set.include?(kv.first) && kv.last =~ /^#/
170
- set << [:pattern, kv.first]
171
- set << [:pattern_color, kv.last]
172
- end
173
- set
174
- end
175
- end
176
- )
177
- settings.each do |setting|
178
- bg_settings[setting.first] = setting.last
179
- end
180
- end
181
- if !bg_settings[:color].nil? && bg_settings[:pattern].nil?
182
- bg_settings[:pattern] = :solid
183
- end
184
- bg_settings
185
- end
186
- end
187
-
188
- def border_settings(border_cmds)
189
- border_cmds.inject({}) do |border_settings, border_cmd|
190
- if (setting = case border_cmd
191
- when ::String
192
- if border_cmd =~ /^#/
193
- [:color, border_cmd]
194
- end
195
- when ::Symbol
196
- if ::Xmlss::Style::Border.position_set.include?(border_cmd)
197
- [:position, border_cmd]
198
- elsif ::Xmlss::Style::Border.weight_set.include?(border_cmd)
199
- [:weight, border_cmd]
200
- elsif ::Xmlss::Style::Border.line_style_set.include?(border_cmd)
201
- [:line_style, border_cmd]
202
- end
203
- end
204
- )
205
- border_settings[setting.first] = setting.last
206
- end
207
- border_settings
208
- end
209
- end
210
- ::Osheet::Style::BORDER_POSITIONS.each do |p|
211
- define_method("border_#{p}_settings") do |cmds|
212
- border_settings(cmds+[p])
213
- end
214
- end
215
-
216
- end
data/test/osheet_test.rb DELETED
@@ -1,13 +0,0 @@
1
- require "assert"
2
-
3
- class OsheetTest < Assert::Context
4
- desc "Osheet"
5
- subject {::Osheet}
6
-
7
- should have_instance_method :register
8
-
9
- should "use provide a default mime type" do
10
- assert_equal "application/vnd.ms-excel", Osheet::MIME_TYPE
11
- end
12
-
13
- end
@@ -1,64 +0,0 @@
1
- require "assert"
2
- require "osheet/partial_set"
3
-
4
- module Osheet
5
-
6
- class PartialSetTest < Assert::Context
7
- desc "Osheet::PartialSet"
8
- before { @ps = PartialSet.new }
9
- subject { @ps }
10
-
11
- should "be a Hash" do
12
- assert_kind_of ::Hash, subject
13
- end
14
-
15
- should have_instance_method :<<
16
- should have_reader :get
17
-
18
- should "verify set objs are partials" do
19
- assert_raises ArgumentError do
20
- subject.send(:verify, {})
21
- end
22
- assert_nothing_raised do
23
- subject.send(:verify, Partial.new(:poo) {})
24
- end
25
- end
26
-
27
- should "key using name values" do
28
- assert_equal 'poo', subject.send(:key, :poo)
29
- end
30
-
31
- should "key on partial objs" do
32
- assert_equal 'poo', subject.send(:partial_key, Partial.new(:poo) {})
33
- end
34
-
35
- should "init the key in the set when verifying" do
36
- key = subject.send(:verify, Partial.new(:thing) {})
37
- assert_equal 'thing', key
38
- assert_equal({'thing' => nil}, subject)
39
- end
40
-
41
- should "push partials onto the set" do
42
- assert_nothing_raised do
43
- subject << Partial.new(:poo) {}
44
- subject << Partial.new(:not_poo) {}
45
- subject << Partial.new(:awesome) {}
46
- subject << Partial.new(:poo) {}
47
- end
48
-
49
- assert_equal 3, subject.keys.size
50
- assert subject["poo"]
51
- assert_kind_of Partial, subject["poo"]
52
- end
53
-
54
- should "lookup a partial by name" do
55
- p = Partial.new(:poo) {}
56
- subject << p
57
- assert_equal p, subject.get(:poo)
58
- assert_equal p, subject.get('poo')
59
- assert_equal nil, subject.get(:ugh)
60
- end
61
-
62
- end
63
-
64
- end
@@ -1,47 +0,0 @@
1
- require "assert"
2
- require "osheet/style_set"
3
-
4
- module Osheet
5
- class StyleSetTest < Assert::Context
6
- desc "Osheet::StyleSet"
7
- before { @set = StyleSet.new }
8
- subject { @set }
9
-
10
- should "be an Array" do
11
- assert_kind_of ::Array, subject
12
- end
13
-
14
- should have_reader :for
15
-
16
- should "verify Template objs" do
17
- assert_raises ArgumentError do
18
- subject.send(:verify, {})
19
- end
20
- assert_nothing_raised do
21
- subject.send(:verify, Style.new('.awesome') {})
22
- end
23
- end
24
-
25
- should "be able to lookup styles by class" do
26
- subject << (a = Style.new('.awesome') {})
27
- subject << (at = Style.new('.awesome.thing') {})
28
- subject << (b = Style.new('.boring') {})
29
- subject << (bt = Style.new('.boring.thing') {})
30
- subject << (a_b = Style.new('.awesome', '.boring') {})
31
- subject << (t = Style.new('.thing') {})
32
- subject << (s = Style.new('.stupid') {})
33
-
34
- { 'awesome' => [a, a_b],
35
- 'boring' => [b, a_b],
36
- 'thing' => [t],
37
- 'awesome thing' => [a, at, a_b, t],
38
- 'thing awesome' => [a, at, a_b, t],
39
- 'other' => []
40
- }.each do |style_class, styles_set|
41
- assert_equal styles_set, subject.for(style_class)
42
- end
43
- end
44
-
45
- end
46
-
47
- end
@@ -1,74 +0,0 @@
1
- require "assert"
2
- require "osheet/template_set"
3
-
4
- module Osheet
5
- class TemplateSetTest < Assert::Context
6
- desc "Osheet::TemplateSet"
7
- before { @set = TemplateSet.new }
8
- subject { @set }
9
-
10
- should "be a PartialSet" do
11
- assert_kind_of PartialSet, subject
12
- end
13
-
14
- should "verify set objs are templates" do
15
- assert_raises ArgumentError do
16
- subject.send(:verify, {})
17
- end
18
- assert_nothing_raised do
19
- subject.send(:verify, Template.new(:row, :poo) {})
20
- end
21
- end
22
-
23
- should "key templates using an array of their element and name" do
24
- assert_equal ['row','poo'], subject.send(:key, :row, :poo)
25
- end
26
-
27
- should "key on templates objs" do
28
- assert_equal ['row', 'poo'], subject.send(:template_key, Template.new(:row, :poo) {})
29
- end
30
-
31
- should "init the key in the set when verifying" do
32
- key = subject.send(:verify, Template.new(:row, :poo) {})
33
- assert_equal ['row', 'poo'], key
34
- assert_equal({
35
- key.first => { key.last => nil }
36
- }, subject)
37
- end
38
-
39
- should "push templates onto the set" do
40
- assert_nothing_raised do
41
- subject << Template.new(:row, :poo) {}
42
- subject << Template.new(:row, :not_poo) {}
43
- subject << Template.new(:column, :awesome) {}
44
- subject << Template.new(:column, :not_awesome) {}
45
- end
46
-
47
- assert_equal 2, subject.keys.size
48
- assert subject["row"]
49
- assert_equal 2, subject["row"].keys.size
50
- assert subject["row"]["poo"]
51
- assert_kind_of Template, subject["row"]["poo"]
52
- assert subject["row"]["not_poo"]
53
- assert_kind_of Template, subject["row"]["not_poo"]
54
- assert subject["column"]
55
- assert_equal 2, subject["column"].keys.size
56
- assert subject["column"]["awesome"]
57
- assert_kind_of Template, subject["column"]["awesome"]
58
- assert subject["column"]["not_awesome"]
59
- assert_kind_of Template, subject["column"]["not_awesome"]
60
- end
61
-
62
- should "lookup a template by element, name" do
63
- t = Template.new(:row, :poo) {}
64
- subject << t
65
- assert_equal t, subject.get(:row, :poo)
66
- assert_equal t, subject.get('row', 'poo')
67
-
68
- assert_equal nil, subject.get(:row, :ugh)
69
- assert_equal nil, subject.get(:col, :ugh)
70
- end
71
-
72
- end
73
-
74
- end
@@ -1,103 +0,0 @@
1
- require "assert"
2
- require 'osheet/xmlss_writer'
3
-
4
- module Osheet
5
-
6
- class XmlssWriter::BaseTest < Assert::Context
7
- desc "XmlssWriter::Base"
8
- before { @writer = XmlssWriter::Base.new }
9
- subject { @writer }
10
-
11
- should have_readers :used_xstyles
12
- should have_writer :oworkbook
13
- should have_instance_methods :xworkbook
14
-
15
- end
16
-
17
- class XmlssWriter::WorkbookTests < XmlssWriter::BaseTest
18
- before do
19
- @workbook = Workbook.new {
20
- title "xmlss"
21
- worksheet { name "testsheet1" }
22
- }
23
- end
24
-
25
- should "only allow writing an Osheet::Workbook" do
26
- assert_nothing_raised do
27
- subject.oworkbook = @workbook
28
- end
29
- assert_raises ArgumentError do
30
- subject.oworkbook = "poo"
31
- end
32
- end
33
-
34
- should "not allow writing a workbook that has multiple worksheets with the same name" do
35
- assert_raises ArgumentError do
36
- subject.workbook = Workbook.new {
37
- title "invalid"
38
- worksheet { name "testsheet1" }
39
- worksheet { name "testsheet1" }
40
- }
41
- end
42
- end
43
-
44
- should "create an Xmlss workbook" do
45
- subject.oworkbook = @workbook
46
- assert_kind_of ::Xmlss::Workbook, subject.xworkbook
47
-
48
- assert_equal(
49
- "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"><Styles></Styles><Worksheet ss:Name=\"testsheet1\"><Table></Table></Worksheet></Workbook>",
50
- subject.xworkbook.to_s
51
- )
52
- end
53
-
54
- end
55
-
56
- class XmlssWriter::ToFileTests < Assert::Context
57
- desc "XmlssWriter::Base"
58
- before do
59
- @writer = XmlssWriter::Base.new({
60
- :workbook => Workbook.new {
61
- title "written"
62
- worksheet {
63
- name "Poo!"
64
- column
65
- row {
66
- cell {
67
- data 1
68
- format :number
69
- }
70
- }
71
- }
72
- }
73
- })
74
- end
75
- after do
76
- # remove any test files this creates
77
- end
78
- subject { @writer }
79
-
80
- should have_instance_methods :to_data, :to_file
81
-
82
- should "return string xml data" do
83
- xml_data = nil
84
- assert_nothing_raised do
85
- xml_data = subject.to_data
86
- end
87
- assert_kind_of ::String, xml_data
88
- assert_match /^<\?xml/, xml_data
89
- end
90
-
91
- should "write xml data to a file path" do
92
- path = nil
93
- assert_nothing_raised do
94
- path = subject.to_file("./tmp/base_test.xls")
95
- end
96
- assert_kind_of ::String, path
97
- assert_equal './tmp/base_test.xls', path
98
- assert File.exists?(path)
99
- end
100
-
101
- end
102
-
103
- end