osheet 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.bundle/config +2 -0
- data/.gitignore +6 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +33 -0
- data/README.rdoc +103 -61
- data/Rakefile +5 -56
- data/examples/basic.rb +73 -0
- data/examples/formats.rb +366 -0
- data/examples/trivial.rb +21 -0
- data/lib/osheet/associations.rb +62 -0
- data/lib/osheet/cell.rb +44 -10
- data/lib/osheet/column.rb +34 -7
- data/lib/osheet/format/accounting.rb +21 -0
- data/lib/osheet/format/currency.rb +23 -0
- data/lib/osheet/format/custom.rb +13 -0
- data/lib/osheet/format/datetime.rb +13 -0
- data/lib/osheet/format/fraction.rb +33 -0
- data/lib/osheet/format/general.rb +9 -0
- data/lib/osheet/format/number.rb +13 -0
- data/lib/osheet/format/numeric.rb +113 -0
- data/lib/osheet/format/percentage.rb +25 -0
- data/lib/osheet/format/scientific.rb +25 -0
- data/lib/osheet/format/special.rb +28 -0
- data/lib/osheet/format/text.rb +11 -0
- data/lib/osheet/format.rb +30 -0
- data/lib/osheet/row.rb +32 -7
- data/lib/osheet/style.rb +65 -0
- data/lib/osheet/style_set.rb +39 -0
- data/lib/osheet/styled_element.rb +18 -0
- data/lib/osheet/template.rb +32 -0
- data/lib/osheet/template_set.rb +57 -0
- data/lib/osheet/version.rb +1 -11
- data/lib/osheet/workbook.rb +32 -16
- data/lib/osheet/workbook_element.rb +21 -0
- data/lib/osheet/worksheet.rb +18 -20
- data/lib/osheet/worksheet_element.rb +15 -0
- data/lib/osheet/xmlss_writer/base.rb +42 -0
- data/lib/osheet/xmlss_writer/elements.rb +68 -0
- data/lib/osheet/xmlss_writer/styles.rb +176 -0
- data/lib/osheet/xmlss_writer.rb +1 -0
- data/lib/osheet.rb +7 -27
- data/osheet.gemspec +26 -0
- data/test/cell_test.rb +83 -0
- data/test/column_test.rb +81 -0
- data/test/env.rb +9 -0
- data/test/format/accounting_test.rb +158 -0
- data/test/format/currency_test.rb +158 -0
- data/test/format/custom_test.rb +22 -0
- data/test/format/datetime_test.rb +22 -0
- data/test/format/fraction_test.rb +84 -0
- data/test/format/general_test.rb +21 -0
- data/test/format/number_test.rb +93 -0
- data/test/format/percentage_test.rb +116 -0
- data/test/format/scientific_test.rb +116 -0
- data/test/format/special_test.rb +51 -0
- data/test/format/text_test.rb +18 -0
- data/test/format_test.rb +35 -0
- data/test/helper.rb +101 -0
- data/test/osheet_test.rb +14 -0
- data/test/row_test.rb +78 -0
- data/test/style_set_test.rb +50 -0
- data/test/style_test.rb +92 -0
- data/test/template_set_test.rb +76 -0
- data/test/template_test.rb +63 -0
- data/test/workbook_test.rb +142 -0
- data/test/worksheet_test.rb +77 -0
- data/test/xmlss_writer/base_test.rb +92 -0
- data/test/xmlss_writer/elements_test.rb +168 -0
- data/test/xmlss_writer/styles_test.rb +253 -0
- metadata +141 -30
- data/lib/osheet/base.rb +0 -95
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
module Osheet::XmlssWriter; end
|
3
|
+
require 'xmlss'
|
4
|
+
require 'osheet/xmlss_writer/elements'
|
5
|
+
require 'osheet/xmlss_writer/styles'
|
6
|
+
|
7
|
+
module Osheet::XmlssWriter
|
8
|
+
class Base
|
9
|
+
include Elements
|
10
|
+
include Styles
|
11
|
+
|
12
|
+
attr_reader :workbook, :styles
|
13
|
+
|
14
|
+
def initialize(opts={})
|
15
|
+
@styles = []
|
16
|
+
@ostyles = ::Osheet::StyleSet.new
|
17
|
+
self.workbook = opts[:workbook] if opts.has_key?(:workbook)
|
18
|
+
end
|
19
|
+
|
20
|
+
def workbook=(oworkbook)
|
21
|
+
unless oworkbook.kind_of?(::Osheet::Workbook)
|
22
|
+
raise ArgumentError, "'#{oworkbook.inspect}' is not an Osheet::Workbook"
|
23
|
+
end
|
24
|
+
@ostyles = oworkbook.styles
|
25
|
+
@workbook = ::Xmlss::Workbook.new({
|
26
|
+
:worksheets => worksheets(oworkbook.worksheets)
|
27
|
+
})
|
28
|
+
end
|
29
|
+
|
30
|
+
def to_data
|
31
|
+
@workbook.styles = @styles
|
32
|
+
@workbook.to_xml
|
33
|
+
end
|
34
|
+
|
35
|
+
def to_file(path)
|
36
|
+
FileUtils.mkdir_p(File.dirname(path))
|
37
|
+
File.open(path, 'w') {|f| f.write self.to_data}
|
38
|
+
File.exists?(path) ? path : false
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
module Osheet::XmlssWriter::Elements
|
2
|
+
|
3
|
+
protected
|
4
|
+
|
5
|
+
def worksheets(oworksheets)
|
6
|
+
oworksheets.collect do |oworksheet|
|
7
|
+
worksheet(oworksheet)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
def worksheet(oworksheet)
|
11
|
+
::Xmlss::Worksheet.new(oworksheet.attributes[:name], {
|
12
|
+
:table => table(oworksheet)
|
13
|
+
})
|
14
|
+
end
|
15
|
+
def table(oworksheet)
|
16
|
+
::Xmlss::Table.new({
|
17
|
+
:columns => columns(oworksheet.columns),
|
18
|
+
:rows => rows(oworksheet.rows)
|
19
|
+
})
|
20
|
+
end
|
21
|
+
|
22
|
+
def columns(ocolumns)
|
23
|
+
ocolumns.collect do |ocolumn|
|
24
|
+
column(ocolumn)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
def column(ocolumn)
|
28
|
+
::Xmlss::Column.new({
|
29
|
+
:style_id => style_id(ocolumn.attributes[:style_class]),
|
30
|
+
:width => ocolumn.attributes[:width],
|
31
|
+
:auto_fit_width => ocolumn.attributes[:autofit],
|
32
|
+
:hidden => ocolumn.attributes[:hidden]
|
33
|
+
})
|
34
|
+
end
|
35
|
+
|
36
|
+
def rows(orows)
|
37
|
+
orows.collect{|orow| row(orow)}
|
38
|
+
end
|
39
|
+
def row(orow)
|
40
|
+
::Xmlss::Row.new({
|
41
|
+
:style_id => style_id(orow.attributes[:style_class]),
|
42
|
+
:height => orow.attributes[:height],
|
43
|
+
:auto_fit_height => orow.attributes[:autofit],
|
44
|
+
:hidden => orow.attributes[:hidden],
|
45
|
+
:cells => cells(orow.cells)
|
46
|
+
})
|
47
|
+
end
|
48
|
+
|
49
|
+
def cells(ocells)
|
50
|
+
ocells.collect{|ocell| cell(ocell)}
|
51
|
+
end
|
52
|
+
def cell(ocell)
|
53
|
+
::Xmlss::Cell.new({
|
54
|
+
:style_id => style_id(ocell.attributes[:style_class], ocell.attributes[:format]),
|
55
|
+
:href => ocell.attributes[:href],
|
56
|
+
:merge_across => cell_merge(ocell.attributes[:colspan]),
|
57
|
+
:merge_down => cell_merge(ocell.attributes[:rowspan]),
|
58
|
+
:data => data(ocell.attributes[:data])
|
59
|
+
})
|
60
|
+
end
|
61
|
+
def cell_merge(span)
|
62
|
+
span.kind_of?(::Fixnum) && span > 1 ? span-1 : 0
|
63
|
+
end
|
64
|
+
def data(ocell_data)
|
65
|
+
::Xmlss::Data.new(ocell_data)
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
@@ -0,0 +1,176 @@
|
|
1
|
+
require 'osheet/format'
|
2
|
+
module Osheet::XmlssWriter::Styles
|
3
|
+
|
4
|
+
protected
|
5
|
+
|
6
|
+
def style_id(style_class, oformat=nil)
|
7
|
+
oformat ||= Osheet::Format.new(:general)
|
8
|
+
(xmlss_style = style(style_class, oformat)).nil? ? '' : xmlss_style.id
|
9
|
+
end
|
10
|
+
|
11
|
+
def style(style_class, oformat=nil)
|
12
|
+
oformat ||= Osheet::Format.new(:general)
|
13
|
+
key = style_key(style_class, oformat.key)
|
14
|
+
xmlss_style = @styles.find{|style| style.id == key}
|
15
|
+
if !key.empty? && xmlss_style.nil?
|
16
|
+
settings = style_settings(key)
|
17
|
+
@styles << (xmlss_style = ::Xmlss::Style::Base.new(key) {
|
18
|
+
if settings.has_key?(:align) && !settings[:align].empty?
|
19
|
+
alignment(settings[:align])
|
20
|
+
end
|
21
|
+
if settings.has_key?(:font) && !settings[:font].empty?
|
22
|
+
font(settings[:font])
|
23
|
+
end
|
24
|
+
if settings.has_key?(:bg) && !settings[:bg].empty?
|
25
|
+
interior(settings[:bg])
|
26
|
+
end
|
27
|
+
::Osheet::Style::BORDERS.each do |bp|
|
28
|
+
if settings.has_key?(bp) && !settings[bp].empty?
|
29
|
+
border(settings[bp])
|
30
|
+
end
|
31
|
+
end
|
32
|
+
if oformat
|
33
|
+
number_format(:format => oformat.style)
|
34
|
+
end
|
35
|
+
})
|
36
|
+
end
|
37
|
+
xmlss_style
|
38
|
+
end
|
39
|
+
|
40
|
+
def style_key(style_class, format_key)
|
41
|
+
(style_class || '').strip.split(/\s+/).collect do |c|
|
42
|
+
".#{c}"
|
43
|
+
end.join('') + (format_key.nil? || format_key.empty? ? '' : "..#{format_key}")
|
44
|
+
end
|
45
|
+
|
46
|
+
def style_settings(key)
|
47
|
+
@ostyles.for(key).inject({}) do |style_settings, ostyle|
|
48
|
+
merged_settings(style_settings, ostyle_settings(ostyle))
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def merged_settings(current, add)
|
53
|
+
# concat values for keys in both sets
|
54
|
+
current.keys.each do |k|
|
55
|
+
current[k].merge!(add.delete(k))
|
56
|
+
end
|
57
|
+
# merge keys for anything not in the current
|
58
|
+
current.merge(add)
|
59
|
+
end
|
60
|
+
|
61
|
+
def ostyle_settings(ostyle)
|
62
|
+
ostyle_settings = {}
|
63
|
+
ostyle.attributes.each do |k,v|
|
64
|
+
unless v.empty?
|
65
|
+
ostyle_settings[k] = send("#{k}_settings", v)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
ostyle_settings
|
69
|
+
end
|
70
|
+
|
71
|
+
def align_settings(align_cmds)
|
72
|
+
align_cmds.inject({}) do |align_settings, align_cmd|
|
73
|
+
if (setting = case align_cmd
|
74
|
+
when :left, :center, :right
|
75
|
+
[:horizontal, align_cmd]
|
76
|
+
when :top, :bottom
|
77
|
+
[:vertical, align_cmd]
|
78
|
+
when :middle
|
79
|
+
[:vertical, :center]
|
80
|
+
when :wrap
|
81
|
+
[:wrap_text, true]
|
82
|
+
when ::Fixnum
|
83
|
+
[:rotate, align_cmd]
|
84
|
+
end
|
85
|
+
)
|
86
|
+
align_settings[setting.first] = setting.last
|
87
|
+
end
|
88
|
+
align_settings
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def font_settings(font_cmds)
|
93
|
+
font_cmds.inject({}) do |font_settings, font_cmd|
|
94
|
+
if (setting = case font_cmd
|
95
|
+
when :underline
|
96
|
+
[:underline, :single]
|
97
|
+
when :double_underline
|
98
|
+
[:underline, :double]
|
99
|
+
when :subscript, :superscript
|
100
|
+
[:alignment, font_cmd]
|
101
|
+
when :bold, :italic
|
102
|
+
[font_cmd, true]
|
103
|
+
when :strikethrough
|
104
|
+
[:strike_through, true]
|
105
|
+
when ::Fixnum
|
106
|
+
[:size, font_cmd]
|
107
|
+
when ::String
|
108
|
+
if font_cmd =~ /^#/
|
109
|
+
[:color, font_cmd]
|
110
|
+
end
|
111
|
+
end
|
112
|
+
)
|
113
|
+
font_settings[setting.first] = setting.last
|
114
|
+
end
|
115
|
+
font_settings
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
def bg_settings(bg_cmds)
|
120
|
+
bg_cmds.inject({}) do |bg_settings, bg_cmd|
|
121
|
+
if (settings = case bg_cmd
|
122
|
+
when ::String
|
123
|
+
if bg_cmd =~ /^#/
|
124
|
+
[[:color, bg_cmd]]
|
125
|
+
end
|
126
|
+
when ::Symbol
|
127
|
+
if ::Xmlss::Style::Interior.pattern_set.include?(bg_cmd)
|
128
|
+
[[:pattern, bg_cmd]]
|
129
|
+
end
|
130
|
+
when ::Hash
|
131
|
+
bg_cmd.inject([]) do |set, kv|
|
132
|
+
if ::Xmlss::Style::Interior.pattern_set.include?(kv.first) && kv.last =~ /^#/
|
133
|
+
set << [:pattern, kv.first]
|
134
|
+
set << [:pattern_color, kv.last]
|
135
|
+
end
|
136
|
+
set
|
137
|
+
end
|
138
|
+
end
|
139
|
+
)
|
140
|
+
settings.each do |setting|
|
141
|
+
bg_settings[setting.first] = setting.last
|
142
|
+
end
|
143
|
+
end
|
144
|
+
bg_settings
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
def border_settings(border_cmds)
|
149
|
+
border_cmds.inject({}) do |border_settings, border_cmd|
|
150
|
+
if (setting = case border_cmd
|
151
|
+
when ::String
|
152
|
+
if border_cmd =~ /^#/
|
153
|
+
[:color, border_cmd]
|
154
|
+
end
|
155
|
+
when ::Symbol
|
156
|
+
if ::Xmlss::Style::Border.position_set.include?(border_cmd)
|
157
|
+
[:position, border_cmd]
|
158
|
+
elsif ::Xmlss::Style::Border.weight_set.include?(border_cmd)
|
159
|
+
[:weight, border_cmd]
|
160
|
+
elsif ::Xmlss::Style::Border.line_style_set.include?(border_cmd)
|
161
|
+
[:line_style, border_cmd]
|
162
|
+
end
|
163
|
+
end
|
164
|
+
)
|
165
|
+
border_settings[setting.first] = setting.last
|
166
|
+
end
|
167
|
+
border_settings
|
168
|
+
end
|
169
|
+
end
|
170
|
+
::Osheet::Style::BORDER_POSITIONS.each do |p|
|
171
|
+
define_method("border_#{p}_settings") do |cmds|
|
172
|
+
border_settings(cmds+[p])
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'osheet/xmlss_writer/base'
|
data/lib/osheet.rb
CHANGED
@@ -1,32 +1,12 @@
|
|
1
|
-
require 'osheet/base'
|
2
|
-
require 'osheet/workbook'
|
3
|
-
require 'osheet/worksheet'
|
4
|
-
|
5
1
|
module Osheet
|
6
2
|
SPREADSHEET_TYPE = "Excel"
|
7
3
|
MIME_TYPE = "application/vnd.ms-excel"
|
8
|
-
=begin
|
9
|
-
@@config = Config.new
|
10
|
-
|
11
|
-
class << self
|
12
|
-
|
13
|
-
# Configuration accessors for Rack::Less
|
14
|
-
# (see config.rb for details)
|
15
|
-
def configure
|
16
|
-
yield @@config if block_given?
|
17
|
-
end
|
18
|
-
def config
|
19
|
-
@@config
|
20
|
-
end
|
21
|
-
def config=(value)
|
22
|
-
@@config = value
|
23
|
-
end
|
24
|
-
|
25
|
-
# Combinations config convenience method
|
26
|
-
def combinations(key=nil)
|
27
|
-
@@config.combinations(key)
|
28
|
-
end
|
29
4
|
|
30
|
-
end
|
31
|
-
=end
|
32
5
|
end
|
6
|
+
|
7
|
+
require 'osheet/associations'
|
8
|
+
require 'osheet/workbook_element'
|
9
|
+
require 'osheet/worksheet_element'
|
10
|
+
require 'osheet/styled_element'
|
11
|
+
|
12
|
+
require 'osheet/workbook'
|
data/osheet.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "osheet/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "osheet"
|
7
|
+
s.version = Osheet::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Kelly Redding"]
|
10
|
+
s.email = ["kelly@kelredd.com"]
|
11
|
+
s.homepage = "http://github.com/kelredd/osheet"
|
12
|
+
s.summary = %q{A DSL for generating spreadsheets that doesn't totally suck - pronounced 'Oh sheeeeeet!'}
|
13
|
+
s.description = %q{A DSL for specifying and generating rich spreasheetML. Specify your spreadsheet using the richness of Ruby and easily produce the corresponding spreadsheetML to render in MS Excel.}
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
s.add_development_dependency("bundler", ["~> 1.0"])
|
21
|
+
s.add_development_dependency("test-belt", ["= 0.2.1"]) # locked to a specific version for test stability
|
22
|
+
|
23
|
+
s.add_dependency("enumeration", ["~>1.1.0"])
|
24
|
+
s.add_dependency("xmlss", "~>0.0.2")
|
25
|
+
|
26
|
+
end
|
data/test/cell_test.rb
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
require "test/helper"
|
2
|
+
require 'osheet/cell'
|
3
|
+
|
4
|
+
module Osheet
|
5
|
+
class CellTest < Test::Unit::TestCase
|
6
|
+
|
7
|
+
context "Osheet::Cell" do
|
8
|
+
subject { Cell.new }
|
9
|
+
|
10
|
+
should_be_a_styled_element(Cell)
|
11
|
+
should_be_a_worksheet_element(Cell)
|
12
|
+
should_be_a_workbook_element(Cell)
|
13
|
+
|
14
|
+
should_have_instance_methods :data, :format, :colspan, :rowspan, :href
|
15
|
+
|
16
|
+
should "set it's defaults" do
|
17
|
+
assert_equal nil, subject.send(:instance_variable_get, "@data")
|
18
|
+
assert_kind_of Format::General, subject.send(:instance_variable_get, "@format")
|
19
|
+
assert_equal 1, subject.send(:instance_variable_get, "@colspan")
|
20
|
+
assert_equal 1, subject.send(:instance_variable_get, "@rowspan")
|
21
|
+
assert_equal nil, subject.send(:instance_variable_get, "@href")
|
22
|
+
end
|
23
|
+
|
24
|
+
context "that has attributes" do
|
25
|
+
subject do
|
26
|
+
Cell.new do
|
27
|
+
style_class 'more poo'
|
28
|
+
data "Poo"
|
29
|
+
format :number
|
30
|
+
colspan 4
|
31
|
+
rowspan 2
|
32
|
+
href "http://www.google.com"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
should "should set them correctly" do
|
37
|
+
assert_equal "Poo", subject.send(:instance_variable_get, "@data")
|
38
|
+
assert_kind_of Format::Number, subject.send(:instance_variable_get, "@format")
|
39
|
+
assert_equal 4, subject.send(:instance_variable_get, "@colspan")
|
40
|
+
assert_equal 2, subject.send(:instance_variable_get, "@rowspan")
|
41
|
+
assert_equal "http://www.google.com", subject.send(:instance_variable_get, "@href")
|
42
|
+
end
|
43
|
+
|
44
|
+
should "know it's attribute(s)" do
|
45
|
+
[:style_class, :data, :format, :rowspan, :colspan, :href].each do |a|
|
46
|
+
assert subject.attributes.has_key?(a)
|
47
|
+
end
|
48
|
+
assert_equal "more poo", subject.attributes[:style_class]
|
49
|
+
assert_equal "Poo", subject.attributes[:data]
|
50
|
+
assert_kind_of Format::Number, subject.attributes[:format]
|
51
|
+
assert_equal 4, subject.attributes[:colspan]
|
52
|
+
assert_equal 2, subject.attributes[:rowspan]
|
53
|
+
assert_equal "http://www.google.com", subject.attributes[:href]
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
should "type cast data strings/symbols" do
|
58
|
+
['a string', :symbol].each do |thing|
|
59
|
+
cell = Cell.new{data thing}
|
60
|
+
assert_kind_of ::String, cell.send(:instance_variable_get, "@data")
|
61
|
+
end
|
62
|
+
end
|
63
|
+
should "type cast data dates" do
|
64
|
+
cell = Cell.new{data Date.today}
|
65
|
+
assert_kind_of ::Date, cell.send(:instance_variable_get, "@data")
|
66
|
+
end
|
67
|
+
should "type cast data numerics" do
|
68
|
+
[1, 1.0].each do |thing|
|
69
|
+
cell = Cell.new{data thing}
|
70
|
+
assert_kind_of ::Numeric, cell.send(:instance_variable_get, "@data")
|
71
|
+
end
|
72
|
+
end
|
73
|
+
should "type cast all other data to string" do
|
74
|
+
[Osheet, [:a, 'Aye'], {:a => 'Aye'}].each do |thing|
|
75
|
+
cell = Cell.new{data thing}
|
76
|
+
assert_kind_of ::String, cell.send(:instance_variable_get, "@data")
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
end
|
data/test/column_test.rb
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
require "test/helper"
|
2
|
+
require "osheet/column"
|
3
|
+
|
4
|
+
module Osheet
|
5
|
+
class ColumnTest < Test::Unit::TestCase
|
6
|
+
|
7
|
+
context "Osheet::Column" do
|
8
|
+
subject { Column.new }
|
9
|
+
|
10
|
+
should_be_a_styled_element(Column)
|
11
|
+
should_be_a_worksheet_element(Column)
|
12
|
+
should_be_a_workbook_element(Column)
|
13
|
+
|
14
|
+
should_have_instance_method :width
|
15
|
+
should_have_instance_methods :autofit, :autofit?
|
16
|
+
should_have_instance_methods :hidden, :hidden?
|
17
|
+
should_have_instance_method :meta
|
18
|
+
|
19
|
+
should "set it's defaults" do
|
20
|
+
assert_equal nil, subject.send(:instance_variable_get, "@width")
|
21
|
+
assert_equal false, subject.send(:instance_variable_get, "@autofit")
|
22
|
+
assert !subject.autofit?
|
23
|
+
assert_equal false, subject.send(:instance_variable_get, "@hidden")
|
24
|
+
assert !subject.hidden?
|
25
|
+
|
26
|
+
assert_equal nil, subject.meta
|
27
|
+
end
|
28
|
+
|
29
|
+
context "that has attributes" do
|
30
|
+
subject do
|
31
|
+
Column.new do
|
32
|
+
style_class "more poo"
|
33
|
+
width 100
|
34
|
+
autofit true
|
35
|
+
hidden true
|
36
|
+
meta(
|
37
|
+
{}
|
38
|
+
)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
should "should set them correctly" do
|
43
|
+
assert_equal 100, subject.send(:instance_variable_get, "@width")
|
44
|
+
assert_equal true, subject.send(:instance_variable_get, "@autofit")
|
45
|
+
assert subject.autofit?
|
46
|
+
assert_equal true, subject.send(:instance_variable_get, "@hidden")
|
47
|
+
assert subject.hidden?
|
48
|
+
assert_equal({}, subject.meta)
|
49
|
+
end
|
50
|
+
|
51
|
+
should "know it's width" do
|
52
|
+
subject.width(false)
|
53
|
+
assert_equal false, subject.width
|
54
|
+
subject.width(180)
|
55
|
+
assert_equal 180, subject.width
|
56
|
+
subject.width(nil)
|
57
|
+
assert_equal 180, subject.width
|
58
|
+
end
|
59
|
+
|
60
|
+
should "know it's attribute(s)" do
|
61
|
+
[:style_class, :width, :autofit, :hidden].each do |a|
|
62
|
+
assert subject.attributes.has_key?(a)
|
63
|
+
end
|
64
|
+
assert_equal 'more poo', subject.attributes[:style_class]
|
65
|
+
assert_equal 100, subject.attributes[:width]
|
66
|
+
assert_equal true, subject.attributes[:autofit]
|
67
|
+
assert_equal true, subject.attributes[:hidden]
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
|
72
|
+
should "cast autofit and hidden to bool" do
|
73
|
+
col = Column.new { autofit :true; hidden 'false'}
|
74
|
+
assert_kind_of ::TrueClass, col.send(:instance_variable_get, "@autofit")
|
75
|
+
assert_kind_of ::TrueClass, col.send(:instance_variable_get, "@hidden")
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
end
|
data/test/env.rb
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
# Add test and lib paths to the $LOAD_PATH
|
2
|
+
[ File.dirname(__FILE__),
|
3
|
+
File.join(File.dirname(__FILE__), '..', 'lib')
|
4
|
+
].each do |path|
|
5
|
+
full_path = File.expand_path(path)
|
6
|
+
$LOAD_PATH.unshift(full_path) unless $LOAD_PATH.include?(full_path)
|
7
|
+
end
|
8
|
+
|
9
|
+
require 'osheet'
|
@@ -0,0 +1,158 @@
|
|
1
|
+
require "test/helper"
|
2
|
+
require 'osheet/format/accounting'
|
3
|
+
|
4
|
+
module Osheet::Format
|
5
|
+
|
6
|
+
class AccountingTest < Test::Unit::TestCase
|
7
|
+
context "Accounting format" do
|
8
|
+
subject { Accounting.new }
|
9
|
+
|
10
|
+
should_have_accessors :decimal_places, :symbol, :comma_separator, :negative_numbers
|
11
|
+
|
12
|
+
should "provide symbol options" do
|
13
|
+
assert_equal 3, Accounting.symbol_set.size
|
14
|
+
[:none, :dollar, :euro].each do |a|
|
15
|
+
assert Accounting.symbol_set.include?(a)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
should "provide negative numbers options" do
|
20
|
+
assert_equal 4, Accounting.negative_numbers_set.size
|
21
|
+
[:black, :black_parenth, :red, :red_parenth].each do |a|
|
22
|
+
assert Accounting.negative_numbers_set.include?(a)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
should "set default values" do
|
27
|
+
assert_equal 2, subject.decimal_places
|
28
|
+
assert_equal :dollar, subject.symbol
|
29
|
+
assert_equal true, subject.comma_separator
|
30
|
+
assert_equal :black, subject.negative_numbers
|
31
|
+
end
|
32
|
+
|
33
|
+
should "only allow Fixnum decimal places between 0 and 30" do
|
34
|
+
assert_raises ArgumentError do
|
35
|
+
Accounting.new({:decimal_places => -1})
|
36
|
+
end
|
37
|
+
assert_raises ArgumentError do
|
38
|
+
Accounting.new({:decimal_places => 31})
|
39
|
+
end
|
40
|
+
assert_raises ArgumentError do
|
41
|
+
Accounting.new({:decimal_places => 'poo'})
|
42
|
+
end
|
43
|
+
assert_nothing_raised do
|
44
|
+
Accounting.new({:decimal_places => 1})
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
should "generate decimal place style strings" do
|
49
|
+
assert_equal "0", Accounting.new({
|
50
|
+
:decimal_places => 0,
|
51
|
+
:comma_separator => false,
|
52
|
+
:symbol => :none
|
53
|
+
}).style
|
54
|
+
(1..5).each do |n|
|
55
|
+
assert_equal "0.#{'0'*n}", Accounting.new({
|
56
|
+
:decimal_places => n,
|
57
|
+
:comma_separator => false,
|
58
|
+
:symbol => :none
|
59
|
+
}).style
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
should "generate comma separator style strings" do
|
64
|
+
assert_equal "0", Accounting.new({
|
65
|
+
:comma_separator => false,
|
66
|
+
:decimal_places => 0,
|
67
|
+
:symbol => :none
|
68
|
+
}).style
|
69
|
+
assert_equal "#,##0", Accounting.new({
|
70
|
+
:comma_separator => true,
|
71
|
+
:decimal_places => 0,
|
72
|
+
:symbol => :none
|
73
|
+
}).style
|
74
|
+
end
|
75
|
+
|
76
|
+
should "generate parenth negative numbers style strings" do
|
77
|
+
assert_equal "0", Accounting.new({
|
78
|
+
:negative_numbers => :black,
|
79
|
+
:decimal_places => 0,
|
80
|
+
:comma_separator => false,
|
81
|
+
:symbol => :none
|
82
|
+
}).style
|
83
|
+
assert_equal "0_);\(0\)", Accounting.new({
|
84
|
+
:negative_numbers => :black_parenth,
|
85
|
+
:decimal_places => 0,
|
86
|
+
:comma_separator => false,
|
87
|
+
:symbol => :none
|
88
|
+
}).style
|
89
|
+
end
|
90
|
+
|
91
|
+
should "generate red negative numbers style strings" do
|
92
|
+
assert_equal "0;[Red]0", Accounting.new({
|
93
|
+
:negative_numbers => :red,
|
94
|
+
:decimal_places => 0,
|
95
|
+
:comma_separator => false,
|
96
|
+
:symbol => :none
|
97
|
+
}).style
|
98
|
+
assert_equal "0_);[Red]\(0\)", Accounting.new({
|
99
|
+
:negative_numbers => :red_parenth,
|
100
|
+
:decimal_places => 0,
|
101
|
+
:comma_separator => false,
|
102
|
+
:symbol => :none
|
103
|
+
}).style
|
104
|
+
end
|
105
|
+
|
106
|
+
should "generate symbol style strings" do
|
107
|
+
assert_equal "0", Accounting.new({
|
108
|
+
:decimal_places => 0,
|
109
|
+
:comma_separator => false,
|
110
|
+
:symbol => :none
|
111
|
+
}).style
|
112
|
+
assert_equal "\"$\"* 0", Accounting.new({
|
113
|
+
:decimal_places => 0,
|
114
|
+
:comma_separator => false,
|
115
|
+
:symbol => :dollar
|
116
|
+
}).style
|
117
|
+
assert_equal "[$€-2]\ * 0", Accounting.new({
|
118
|
+
:decimal_places => 0,
|
119
|
+
:comma_separator => false,
|
120
|
+
:symbol => :euro
|
121
|
+
}).style
|
122
|
+
end
|
123
|
+
|
124
|
+
should "generate complex style string" do
|
125
|
+
assert_equal("\"$\"* 0.00_);\(\"$\"* 0.00\)", Accounting.new({
|
126
|
+
:symbol => :dollar,
|
127
|
+
:decimal_places => 2,
|
128
|
+
:negative_numbers => :black_parenth,
|
129
|
+
:comma_separator => false
|
130
|
+
}).style)
|
131
|
+
assert_equal("[$€-2]\ * #,##0.0000_);[Red]\([$€-2]\ * #,##0.0000\)", Accounting.new({
|
132
|
+
:symbol => :euro,
|
133
|
+
:decimal_places => 4,
|
134
|
+
:negative_numbers => :red_parenth,
|
135
|
+
:comma_separator => true
|
136
|
+
}).style)
|
137
|
+
end
|
138
|
+
|
139
|
+
should "provide unique format keys" do
|
140
|
+
assert_equal("accounting_dollar_2_nocomma_blackparenth", Accounting.new({
|
141
|
+
:decimal_places => 2,
|
142
|
+
:negative_numbers => :black_parenth,
|
143
|
+
:comma_separator => false
|
144
|
+
}).key)
|
145
|
+
assert_equal("accounting_none_4_comma_redparenth", Accounting.new({
|
146
|
+
:symbol => :none,
|
147
|
+
:decimal_places => 4,
|
148
|
+
:negative_numbers => :red_parenth,
|
149
|
+
:comma_separator => true
|
150
|
+
}).key)
|
151
|
+
end
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
end
|