xmlss 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. data/.gitignore +6 -0
  2. data/Gemfile +4 -0
  3. data/Gemfile.lock +28 -0
  4. data/README.rdoc +154 -0
  5. data/Rakefile +7 -0
  6. data/examples/example_workbook.rb +19 -0
  7. data/examples/layout.rb +81 -0
  8. data/examples/simple.rb +30 -0
  9. data/examples/styles.rb +85 -0
  10. data/examples/text.rb +36 -0
  11. data/lib/xmlss/cell.rb +38 -0
  12. data/lib/xmlss/column.rb +28 -0
  13. data/lib/xmlss/data.rb +67 -0
  14. data/lib/xmlss/enum.rb +56 -0
  15. data/lib/xmlss/item_set.rb +17 -0
  16. data/lib/xmlss/row.rb +34 -0
  17. data/lib/xmlss/style/alignment.rb +47 -0
  18. data/lib/xmlss/style/base.rb +63 -0
  19. data/lib/xmlss/style/border.rb +43 -0
  20. data/lib/xmlss/style/font.rb +41 -0
  21. data/lib/xmlss/style/interior.rb +41 -0
  22. data/lib/xmlss/style/number_format.rb +20 -0
  23. data/lib/xmlss/style/protection.rb +18 -0
  24. data/lib/xmlss/table.rb +22 -0
  25. data/lib/xmlss/version.rb +3 -0
  26. data/lib/xmlss/workbook.rb +33 -0
  27. data/lib/xmlss/worksheet.rb +37 -0
  28. data/lib/xmlss/xml.rb +55 -0
  29. data/lib/xmlss.rb +31 -0
  30. data/test/cell_test.rb +86 -0
  31. data/test/column_test.rb +46 -0
  32. data/test/data_test.rb +75 -0
  33. data/test/enum_test.rb +63 -0
  34. data/test/env.rb +10 -0
  35. data/test/helper.rb +49 -0
  36. data/test/item_set_test.rb +26 -0
  37. data/test/row_test.rb +67 -0
  38. data/test/style/alignment_test.rb +107 -0
  39. data/test/style/base_test.rb +126 -0
  40. data/test/style/border_test.rb +114 -0
  41. data/test/style/font_test.rb +100 -0
  42. data/test/style/interior_test.rb +87 -0
  43. data/test/style/number_format_test.rb +50 -0
  44. data/test/style/protection_test.rb +45 -0
  45. data/test/table_test.rb +53 -0
  46. data/test/thing.rb +5 -0
  47. data/test/workbook_test.rb +59 -0
  48. data/test/worksheet_test.rb +62 -0
  49. data/test/xml_test.rb +61 -0
  50. data/test/xmlss_test.rb +29 -0
  51. data/xmlss.gemspec +23 -0
  52. metadata +184 -0
@@ -0,0 +1,100 @@
1
+ require "test/helper"
2
+ require 'xmlss/style/font'
3
+
4
+ class Xmlss::Style::FontTest < Test::Unit::TestCase
5
+
6
+ context "Xmlss::Style::Font" do
7
+ subject { Xmlss::Style::Font.new }
8
+
9
+ should_have_class_method :underline
10
+ {
11
+ :none => 0,
12
+ :single => 1,
13
+ :double => 2
14
+ }.each do |underline, value|
15
+ should "provide the value for the '#{underline}' underline" do
16
+ assert_equal value, Xmlss::Style::Font.underline(underline)
17
+ end
18
+ end
19
+
20
+ should_have_class_method :alignment
21
+ {
22
+ :none => 0,
23
+ :subscript => 1,
24
+ :superscript => 2
25
+ }.each do |alignment, value|
26
+ should "provide the value for the '#{alignment}' alignment" do
27
+ assert_equal value, Xmlss::Style::Font.alignment(alignment)
28
+ end
29
+ end
30
+
31
+ should_have_accessors :bold, :color, :italic, :size, :strike_through
32
+ should_have_accessors :underline, :alignment
33
+ should_have_instance_methods :bold?, :italic?, :strike_through?
34
+
35
+ should "set it's defaults" do
36
+ assert_equal false, subject.bold
37
+ assert_equal nil, subject.color
38
+ assert_equal false, subject.italic
39
+ assert_equal nil, subject.size
40
+ assert_equal false, subject.strike_through
41
+ assert_equal Xmlss::Style::Font.underline(:default), subject.underline
42
+ assert_equal Xmlss::Style::Font.alignment(:default), subject.alignment
43
+ end
44
+
45
+ context "that sets attributes at init" do
46
+ before do
47
+ @attrs = {
48
+ :bold => true,
49
+ :color => '#FF0000',
50
+ :italic => true,
51
+ :size => 10,
52
+ :strike_through => true,
53
+ :underline => :single,
54
+ :alignment => :superscript
55
+ }
56
+ @font = Xmlss::Style::Font.new(@attrs)
57
+ end
58
+ subject{ @font }
59
+
60
+ should "should set them correctly" do
61
+ @attrs.reject{|a, v| [:underline, :alignment].include?(a)}.each do |a,v|
62
+ assert_equal v, subject.send(a)
63
+ end
64
+ assert_equal Xmlss::Style::Font.underline(:single), subject.underline
65
+ assert_equal Xmlss::Style::Font.alignment(:superscript), subject.alignment
66
+ end
67
+ end
68
+
69
+ context "that sets underline and alignment by key" do
70
+ before do
71
+ subject.underline = :double
72
+ subject.alignment = :subscript
73
+ end
74
+
75
+ should "should returm it by value" do
76
+ assert_equal Xmlss::Style::Font.underline(:double), subject.underline
77
+ assert_equal Xmlss::Style::Font.alignment(:subscript), subject.alignment
78
+ end
79
+ end
80
+
81
+ context "that sets underline and alignment by value" do
82
+ before do
83
+ subject.underline = Xmlss::Style::Font.underline(:double)
84
+ subject.alignment = Xmlss::Style::Font.alignment(:subscript)
85
+ end
86
+
87
+ should "should returm it by value" do
88
+ assert_equal Xmlss::Style::Font.underline(:double), subject.underline
89
+ assert_equal Xmlss::Style::Font.alignment(:subscript), subject.alignment
90
+ end
91
+ end
92
+
93
+ context "for generating XML" do
94
+ should_have_reader :xml
95
+ should_build_node
96
+ should_build_no_attributes_by_default(Xmlss::Style::Font)
97
+ end
98
+
99
+ end
100
+ end
@@ -0,0 +1,87 @@
1
+ require "test/helper"
2
+ require 'xmlss/style/interior'
3
+
4
+ class Xmlss::Style::InteriorTest < Test::Unit::TestCase
5
+
6
+ context "Xmlss::Style::Interior" do
7
+ subject { Xmlss::Style::Interior.new }
8
+
9
+ should_have_class_method :pattern
10
+ {
11
+ :none => "None",
12
+ :solid => "Solid",
13
+ :gray75 => "Gray75",
14
+ :gray50 => "Gray50",
15
+ :gray25 => "Gray25",
16
+ :gray125 => "Gray125",
17
+ :gray0625 => "Gray0625",
18
+ :horz_stripe => "HorzStripe",
19
+ :vert_stripe => "VertStripe",
20
+ :reverse_diag_stripe => "ReverseDiagStripe",
21
+ :diag_stripe => "DiagStripe",
22
+ :diag_cross => "DiagCross",
23
+ :thick_diag_cross => "ThickDiagCross",
24
+ :thin_horz_stripe => "ThinHorzStripe",
25
+ :thin_vert_stripe => "ThinVertStripe",
26
+ :thin_reverse_diag_stripe => "ThinReverseDiagStripe",
27
+ :thin_diag_stripe => "ThineDiagStripe",
28
+ :thin_horz_cross => "ThinHorzCross",
29
+ :thin_diag_cross => "ThinDiagCross"
30
+ }.each do |pattern, value|
31
+ should "provide the value for the '#{pattern}' pattern" do
32
+ assert_equal value, Xmlss::Style::Interior.pattern(pattern)
33
+ end
34
+ end
35
+
36
+ should_have_accessor :color, :pattern, :pattern_color
37
+
38
+ should "set it's defaults" do
39
+ assert_equal nil, subject.color
40
+ assert_equal nil, subject.pattern
41
+ assert_equal nil, subject.pattern_color
42
+ end
43
+
44
+ context "that sets attributes at init" do
45
+ subject do
46
+ Xmlss::Style::Interior.new({
47
+ :color => "#000000",
48
+ :pattern => :solid,
49
+ :pattern_color => "#FF0000"
50
+ })
51
+ end
52
+
53
+ should "should set them correctly" do
54
+ assert_equal "#000000", subject.color
55
+ assert_equal "Solid", subject.pattern
56
+ assert_equal "#FF0000", subject.pattern_color
57
+ end
58
+ end
59
+
60
+ context "that sets pattern by key" do
61
+ before do
62
+ subject.pattern = :solid
63
+ end
64
+
65
+ should "should returm them by value" do
66
+ assert_equal "Solid", subject.pattern
67
+ end
68
+ end
69
+
70
+ context "that sets attributes by value" do
71
+ before do
72
+ subject.pattern = "Solid"
73
+ end
74
+
75
+ should "should returm them by value" do
76
+ assert_equal "Solid", subject.pattern
77
+ end
78
+ end
79
+
80
+ context "for generating XML" do
81
+ should_have_reader :xml
82
+ should_build_node
83
+ should_build_no_attributes_by_default(Xmlss::Style::Alignment)
84
+ end
85
+
86
+ end
87
+ end
@@ -0,0 +1,50 @@
1
+ require "test/helper"
2
+ require 'xmlss/style/number_format'
3
+
4
+ class Xmlss::Style::NumberFormatTest < Test::Unit::TestCase
5
+
6
+ context "Xmlss::Style::NumberFormat" do
7
+ subject { Xmlss::Style::NumberFormat.new }
8
+
9
+ should_have_accessor :format
10
+
11
+ context "that sets attributes at init" do
12
+ subject do
13
+ Xmlss::Style::NumberFormat.new({
14
+ :format => "General"
15
+ })
16
+ end
17
+
18
+ should "should set them correctly" do
19
+ assert_equal "General", subject.format
20
+ end
21
+ end
22
+
23
+ context "that sets format by key" do
24
+ before do
25
+ subject.format = "@"
26
+ end
27
+
28
+ should "should returm it by value" do
29
+ assert_equal "@", subject.format
30
+ end
31
+ end
32
+
33
+ context "that sets format by value" do
34
+ before do
35
+ subject.format = "True/False"
36
+ end
37
+
38
+ should "should returm it by value" do
39
+ assert_equal "True/False", subject.format
40
+ end
41
+ end
42
+
43
+ context "for generating XML" do
44
+ should_have_reader :xml
45
+ should_build_node
46
+ should_build_no_attributes_by_default(Xmlss::Style::Alignment)
47
+ end
48
+
49
+ end
50
+ end
@@ -0,0 +1,45 @@
1
+ require "test/helper"
2
+ require 'xmlss/style/protection'
3
+
4
+ class Xmlss::Style::ProtectionTest < Test::Unit::TestCase
5
+
6
+ context "Xmlss::Style::Protection" do
7
+ subject { Xmlss::Style::Protection.new }
8
+
9
+ should_have_instance_methods :protected?
10
+ should_have_accessor :protect
11
+
12
+ should "set it's defaults" do
13
+ assert_equal false, subject.protected?
14
+ end
15
+
16
+ context "that sets attributes at init" do
17
+ subject do
18
+ Xmlss::Style::Protection.new({
19
+ :protect => true
20
+ })
21
+ end
22
+
23
+ should "should set them correctly" do
24
+ assert subject.protected?
25
+ end
26
+ end
27
+
28
+ context "that sets attributes after init" do
29
+ before do
30
+ subject.protect = true
31
+ end
32
+
33
+ should "should set them correctly" do
34
+ assert subject.protected?
35
+ end
36
+ end
37
+
38
+ context "for generating XML" do
39
+ should_have_reader :xml
40
+ should_build_node
41
+ should_build_no_attributes_by_default(Xmlss::Style::Alignment)
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1,53 @@
1
+ require "test/helper"
2
+ require 'xmlss/table'
3
+
4
+ module Xmlss
5
+ class TableTest < Test::Unit::TestCase
6
+
7
+ context "Xmlss::Table" do
8
+ subject { Table.new }
9
+
10
+ should_have_accessor :columns, :rows
11
+
12
+ should "set it's defaults" do
13
+ assert_equal [], subject.columns
14
+ assert_equal [], subject.rows
15
+ end
16
+
17
+ should "allow defining a rows/columns at init" do
18
+ tbl = Table.new({
19
+ :columns => [Column.new],
20
+ :rows => [Row.new]
21
+ })
22
+
23
+ assert_equal 1, tbl.columns.size
24
+ assert_kind_of Column, tbl.columns.first
25
+ assert_equal 1, tbl.rows.size
26
+ assert_kind_of Row, tbl.rows.first
27
+ end
28
+
29
+ context "for generating XML" do
30
+ should_have_reader :xml
31
+ should_build_node
32
+ end
33
+
34
+ context "when using rows/columns" do
35
+ before do
36
+ subject.columns << Column.new
37
+ r = Row.new
38
+ r.cells << Cell.new
39
+ subject.rows << r
40
+ end
41
+
42
+ should "should build a table object" do
43
+ assert_equal 1, subject.columns.size
44
+ assert_kind_of Column, subject.columns.first
45
+ assert_equal 1, subject.rows.size
46
+ assert_kind_of Row, subject.rows.first
47
+ end
48
+ end
49
+
50
+ end
51
+
52
+ end
53
+ end
data/test/thing.rb ADDED
@@ -0,0 +1,5 @@
1
+ class Thing
2
+ attr_accessor :one, :two, :three, :xml
3
+
4
+ include Xmlss::Xml
5
+ end
@@ -0,0 +1,59 @@
1
+ require "test/helper"
2
+ require 'xmlss/workbook'
3
+
4
+ module Xmlss
5
+ class WorkbookTest < Test::Unit::TestCase
6
+
7
+ context "Xmlss::Workbook" do
8
+ subject { Workbook.new }
9
+
10
+ should_have_accessor :styles, :worksheets
11
+ should_have_instance_method :to_xml
12
+
13
+ should "set it's defaults" do
14
+ assert_equal [], subject.styles
15
+ assert_equal [], subject.worksheets
16
+ end
17
+
18
+ context "when initializing with attrs" do
19
+ subject do
20
+ styles = [
21
+ Xmlss::Style::Base.new('title') do
22
+ alignment({:horizontal => :left})
23
+ font({:size => 14, :bold => true})
24
+ end,
25
+
26
+ Xmlss::Style::Base.new('header') do
27
+ alignment({:horizontal => :left})
28
+ font({:bold => true})
29
+ [:top, :right, :bottom, :left].each do |p|
30
+ border({:position => :p})
31
+ end
32
+ end
33
+ ]
34
+ Workbook.new({
35
+ :worksheets => [Worksheet.new('sheet1')],
36
+ :styles => styles
37
+ })
38
+ end
39
+
40
+ should "build the attrs appropriately" do
41
+ [:worksheets, :styles].each do |thing|
42
+ assert_kind_of ItemSet, subject.send(thing)
43
+ end
44
+ assert_kind_of Worksheet, subject.worksheets.first
45
+ assert_equal 1, subject.worksheets.size
46
+
47
+ assert_kind_of Style::Base, subject.styles.first
48
+ assert_equal 2, subject.styles.size
49
+ end
50
+ end
51
+
52
+ context "for generating XML" do
53
+ should_have_reader :xml
54
+ should_build_node
55
+ end
56
+ end
57
+
58
+ end
59
+ end
@@ -0,0 +1,62 @@
1
+ require "test/helper"
2
+ require 'xmlss/worksheet'
3
+
4
+ module Xmlss
5
+ class WorksheetTest < Test::Unit::TestCase
6
+
7
+ context "Xmlss::Worksheet" do
8
+ subject { Worksheet.new('sheet') }
9
+
10
+ should_have_accessor :name, :table
11
+
12
+ should "set it's defaults" do
13
+ assert_equal 'sheet', subject.name
14
+ assert_kind_of Table, subject.table
15
+ assert_equal [], subject.table.columns
16
+ assert_equal [], subject.table.rows
17
+ end
18
+
19
+ should "filter name chars" do
20
+ # worksheet name cannot contain: /, \, ?, *, [, ]
21
+ assert_equal "test test", Worksheet.new("test/ test").name
22
+ assert_equal "test test", Worksheet.new("tes\\t test").name
23
+ assert_equal "test test", Worksheet.new("te?st test?").name
24
+ assert_equal "test test", Worksheet.new("t*est test").name
25
+ assert_equal "test test", Worksheet.new("[te]st test").name
26
+ end
27
+
28
+ should "allow defining a table at init" do
29
+ wksht = Worksheet.new('table', {
30
+ :table => Table.new({
31
+ :columns => [Column.new]
32
+ })
33
+ })
34
+
35
+ assert_equal 1, wksht.table.columns.size
36
+ assert_kind_of Column, wksht.table.columns.first
37
+ end
38
+
39
+ should "bark when no name is given" do
40
+ assert_raises ArgumentError do
41
+ Worksheet.new(nil)
42
+ end
43
+ assert_raises ArgumentError do
44
+ Worksheet.new("")
45
+ end
46
+ end
47
+
48
+ should "bark when setting a table to something other" do
49
+ assert_raises ArgumentError do
50
+ subject.table = "not a table"
51
+ end
52
+ end
53
+
54
+ context "for generating XML" do
55
+ should_have_reader :xml
56
+ should_build_node
57
+ end
58
+
59
+ end
60
+
61
+ end
62
+ end
data/test/xml_test.rb ADDED
@@ -0,0 +1,61 @@
1
+ require "test/helper"
2
+ require 'test/thing'
3
+
4
+ module Xmlss
5
+ class XmlTest < Test::Unit::TestCase
6
+
7
+ context "Xmlss::Xml" do
8
+ subject { Thing.new }
9
+
10
+ should "should provide proper namespace constants" do
11
+ assert_equal "xmlns", Xmlss::XML_NS
12
+ assert_equal "ss", Xmlss::SHEET_NS
13
+ assert_equal "urn:schemas-microsoft-com:office:spreadsheet", Xmlss::NS_URI
14
+ end
15
+
16
+ should "bark if you use xml generation without configuring" do
17
+ assert_raises ArgumentError do
18
+ ::Nokogiri::XML::Builder.new do |builder|
19
+ subject.build_node(builder)
20
+ end
21
+ end
22
+ end
23
+
24
+ context "with config" do
25
+ before do
26
+ subject.xml = {
27
+ :node => :thing,
28
+ :attributes => [:one, :two, :three]
29
+ }
30
+ subject.one = true
31
+ subject.two = "two"
32
+ subject.three = ""
33
+ end
34
+
35
+ should "build it's node" do
36
+ assert_nothing_raised do
37
+ ::Nokogiri::XML::Builder.new do |builder|
38
+ subject.build_node(builder)
39
+ end
40
+ end
41
+ end
42
+
43
+ should "generate build attributes based on it's own attributes" do
44
+ assert_nothing_raised do
45
+ subject.send :build_attributes
46
+ end
47
+ attrs = subject.send :build_attributes
48
+
49
+ assert_kind_of ::Hash, attrs
50
+ assert attrs.has_key?("#{Xmlss::SHEET_NS}:One")
51
+ assert_equal 1, attrs["#{Xmlss::SHEET_NS}:One"]
52
+ assert attrs.has_key?("#{Xmlss::SHEET_NS}:Two")
53
+ assert_equal 'two', attrs["#{Xmlss::SHEET_NS}:Two"]
54
+ assert !attrs.has_key?("#{Xmlss::SHEET_NS}:Three")
55
+ end
56
+ end
57
+
58
+ end
59
+
60
+ end
61
+ end
@@ -0,0 +1,29 @@
1
+ require "test/helper"
2
+
3
+ class XmlssTest < Test::Unit::TestCase
4
+
5
+ context "Xmlss helpers" do
6
+ should "be provided" do
7
+ assert_respond_to Xmlss, :classify
8
+ assert_respond_to Xmlss, :xmlify
9
+ end
10
+
11
+ should "classify underscored strings" do
12
+ assert_nothing_raised do
13
+ subject.send(:classify, 'poo')
14
+ end
15
+ assert_equal "Poo", subject.send(:classify, 'poo')
16
+ assert_equal "MorePoo", subject.send(:classify, 'more_poo')
17
+ end
18
+
19
+ should "filter values for use in XML" do
20
+ assert_nothing_raised do
21
+ subject.send(:xmlify, 'poo')
22
+ end
23
+ assert_equal 1, subject.send(:xmlify, true)
24
+ assert_equal nil, subject.send(:xmlify, false)
25
+ assert_equal nil, subject.send(:xmlify, "")
26
+ assert_equal "poo", subject.send(:xmlify, 'poo')
27
+ end
28
+ end
29
+ end
data/xmlss.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "xmlss/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "xmlss"
7
+ s.version = Xmlss::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/xmlss"
12
+ s.summary = %q{Generate spreadsheet docs for MS Excel using XML Spreedsheet}
13
+ s.description = %q{This gem provides an api for constructing spreadsheet data and then uses that data to generate xml that can be interpreted by MS Excel. The xml conforms to the XML Spreadsheet spec (http://msdn.microsoft.com/en-us/library/aa140066(office.10).aspx).}
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
+ s.add_dependency("nokogiri", ["~> 1.4.0"])
23
+ end