osheet 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- osheet (0.5.0)
4
+ osheet (0.6.0)
5
5
  enumeration (~> 1.1.0)
6
6
  xmlss (~> 0.2.0)
7
7
 
data/lib/osheet/cell.rb CHANGED
@@ -7,6 +7,7 @@ module Osheet
7
7
  include WorkbookElement
8
8
  include WorksheetElement
9
9
  include StyledElement
10
+ include MarkupElement
10
11
 
11
12
  def initialize(workbook=nil, worksheet=nil, *args, &block)
12
13
  set_ivar(:workbook, workbook)
@@ -16,6 +17,7 @@ module Osheet
16
17
  set_ivar(:rowspan, 1)
17
18
  set_ivar(:colspan, 1)
18
19
  set_ivar(:href, nil)
20
+ set_ivar(:index, nil)
19
21
  if block_given?
20
22
  set_binding_ivars(block.binding)
21
23
  instance_exec(*args, &block)
@@ -40,6 +42,7 @@ module Osheet
40
42
  def rowspan(value); set_ivar(:rowspan, value); end
41
43
  def colspan(value); set_ivar(:colspan, value); end
42
44
  def href(value); set_ivar(:href, value); end
45
+ def index(value); set_ivar(:index, value); end
43
46
 
44
47
  def attributes
45
48
  {
@@ -48,7 +51,8 @@ module Osheet
48
51
  :format => get_ivar(:format),
49
52
  :colspan => get_ivar(:colspan),
50
53
  :rowspan => get_ivar(:rowspan),
51
- :href => get_ivar(:href)
54
+ :href => get_ivar(:href),
55
+ :index => get_ivar(:index)
52
56
  }
53
57
  end
54
58
 
data/lib/osheet/column.rb CHANGED
@@ -5,6 +5,7 @@ module Osheet
5
5
  include WorksheetElement
6
6
  include StyledElement
7
7
  include MetaElement
8
+ include MarkupElement
8
9
 
9
10
  def initialize(workbook=nil, worksheet=nil, *args, &block)
10
11
  set_ivar(:workbook, workbook)
@@ -0,0 +1,22 @@
1
+ module Osheet
2
+ module MarkupElement
3
+
4
+ # markup elements can add partial markup to themselves
5
+ def add(partial_name, *args)
6
+ if self.kind_of?(Workbook)
7
+ # on: workbook
8
+ if (partial = self.partials.get(partial_name))
9
+ # add partial
10
+ instance_exec(*args, &partial)
11
+ end
12
+ else
13
+ # on: worksheet, column, row
14
+ if self.workbook && (partial = self.workbook.partials.get(partial_name))
15
+ # add partial
16
+ instance_exec(*args, &partial)
17
+ end
18
+ end
19
+ end
20
+
21
+ end
22
+ end
@@ -1,7 +1,7 @@
1
1
  module Osheet::MetaElement
2
2
 
3
3
  def meta(value=nil)
4
- value.nil? ? instance_variable_get("@meta") : instance_variable_set("@meta", value)
4
+ value.nil? ? get_ivar(:meta) : set_ivar(:meta, value)
5
5
  end
6
6
 
7
7
  end
@@ -0,0 +1,24 @@
1
+ module Osheet
2
+ class Partial < ::Proc
3
+
4
+ # this class is essentially a way to define a named definition
5
+ # block with arguments. If the partial is added to an element,
6
+ # any markup in it's definition block is applied to the element.
7
+ # ie. the definition block will be instance_eval'd on the element.
8
+
9
+ include Instance
10
+
11
+ def initialize(name)
12
+ unless name.kind_of?(::String) || name.kind_of?(::Symbol)
13
+ raise ArgumentError, "please use a string or symbol for the partial name."
14
+ end
15
+
16
+ set_ivar(:name, name.to_s)
17
+ super()
18
+ end
19
+
20
+ def name; get_ivar(:name); end
21
+ def name=(v); set_ivar(:name, v); end
22
+
23
+ end
24
+ end
@@ -0,0 +1,57 @@
1
+ require 'osheet/partial'
2
+
3
+ module Osheet
4
+ class PartialSet < ::Hash
5
+
6
+ # this class is a Hash that behaves kinda like a set. I want to
7
+ # push partials into the set using the '<<' operator, only allow
8
+ # Osheet::Partial objs to be pushed, and then be able to reference
9
+ # a particular partial using a key
10
+
11
+ def initialize
12
+ super
13
+ end
14
+
15
+ def <<(partial)
16
+ if (key = verify(partial))
17
+ push(key, partial)
18
+ end
19
+ end
20
+
21
+ # return the named partial
22
+ def get(name)
23
+ lookup(key(name.to_s))
24
+ end
25
+
26
+ private
27
+
28
+ def lookup(key)
29
+ self[key]
30
+ end
31
+
32
+ # push the partial onto the key
33
+ def push(key, partial)
34
+ self[key] = partial
35
+ end
36
+
37
+ # verify the partial, init and return the key
38
+ # otherwise ArgumentError it up
39
+ def verify(partial)
40
+ unless partial.kind_of?(Partial)
41
+ raise ArgumentError, 'you can only push Osheet::Partial objs to the partial set'
42
+ end
43
+ pkey = partial_key(partial)
44
+ self[pkey] ||= nil
45
+ pkey
46
+ end
47
+
48
+ def partial_key(partial)
49
+ key(partial.name)
50
+ end
51
+
52
+ def key(name)
53
+ name.to_s
54
+ end
55
+
56
+ end
57
+ end
data/lib/osheet/row.rb CHANGED
@@ -8,6 +8,7 @@ module Osheet
8
8
  include WorksheetElement
9
9
  include StyledElement
10
10
  include MetaElement
11
+ include MarkupElement
11
12
 
12
13
  hm :cells
13
14
 
@@ -1,8 +1,9 @@
1
+ require 'osheet/partial'
2
+
1
3
  module Osheet
2
- class Template < ::Proc
4
+ class Template < Partial
3
5
 
4
- # this class is essentially a way to define a named initializer
5
- # block and associate that with an osheet element
6
+ # this class is a partial that is associated with an osheet element
6
7
  # if an element is initialized from a template, the template
7
8
  # block will be instance_eval'd for the element being initialized
8
9
 
@@ -11,31 +12,16 @@ module Osheet
11
12
  ELEMENTS = ['worksheet', 'column', 'row', 'cell']
12
13
 
13
14
  def initialize(element, name)
14
- verify(element, name)
15
- set_ivar(:element, element.to_s)
16
- set_ivar(:name, name.to_s)
17
- super()
18
- end
19
-
20
- [:element, :name].each do |meth|
21
- define_method(meth) do
22
- get_ivar(meth)
23
- end
24
- define_method("#{meth}=") do |value|
25
- set_ivar(meth, value)
26
- end
27
- end
28
-
29
- private
30
-
31
- def verify(element, name)
32
15
  unless element.respond_to?(:to_s) && ELEMENTS.include?(element.to_s)
33
16
  raise ArgumentError, "you can only define a template for #{ELEMENTS.join(', ')} elements."
34
17
  end
35
- unless name.kind_of?(::String) || name.kind_of?(::Symbol)
36
- raise ArgumentError, "please use a string or symbol for the template name."
37
- end
18
+
19
+ set_ivar(:element, element.to_s)
20
+ super(name)
38
21
  end
39
22
 
23
+ def element; get_ivar(:element); end
24
+ def element=(v); set_ivar(:element, v); end
25
+
40
26
  end
41
27
  end
@@ -1,23 +1,17 @@
1
+ require 'osheet/partial_set'
1
2
  require 'osheet/template'
2
3
 
3
4
  module Osheet
4
- class TemplateSet < ::Hash
5
+ class TemplateSet < PartialSet
5
6
 
6
- # this class is a Hash that behaves kinda like a set. I want to
7
- # push templates into the set using the '<<' operator, only allow
8
- # Osheet::Template objs to be pushed, and then be able to reference
9
- # a particular set of templates using a key
7
+ # this class is a PartialSet that keys off of the template element
8
+ # and name. Only Osheet::Template objs can be pushed, and you reference
9
+ # a particular template using a key of
10
10
 
11
11
  def initialize
12
12
  super
13
13
  end
14
14
 
15
- def <<(template)
16
- if (key = verify(template))
17
- push(key, template)
18
- end
19
- end
20
-
21
15
  # return the template set for the named element
22
16
  def get(element, name)
23
17
  lookup(key(element.to_s, name.to_s))
@@ -51,7 +45,7 @@ module Osheet
51
45
  end
52
46
 
53
47
  def key(element, name)
54
- [element, name]
48
+ [element.to_s, name.to_s]
55
49
  end
56
50
  end
57
51
  end
@@ -1,3 +1,3 @@
1
1
  module Osheet
2
- VERSION = "0.5.0"
2
+ VERSION = "0.6.0"
3
3
  end
@@ -0,0 +1,2 @@
1
+ require 'osheet/view_handler/tilt' if defined?(Tilt)
2
+ require 'osheet/view_handler/rails' if defined?(Rails)
@@ -1,6 +1,9 @@
1
1
  require 'osheet/instance'
2
+ require 'osheet/associations'
3
+ require 'osheet/markup_element'
2
4
  require 'osheet/style_set'
3
5
  require 'osheet/template_set'
6
+ require 'osheet/partial_set'
4
7
  require 'osheet/worksheet'
5
8
  require 'osheet/xmlss_writer'
6
9
 
@@ -8,6 +11,7 @@ module Osheet
8
11
  class Workbook
9
12
  include Instance
10
13
  include Associations
14
+ include MarkupElement
11
15
 
12
16
  hm :worksheets
13
17
 
@@ -15,6 +19,7 @@ module Osheet
15
19
  set_ivar(:title, nil)
16
20
  set_ivar(:styles, StyleSet.new)
17
21
  set_ivar(:templates, TemplateSet.new)
22
+ set_ivar(:partials, PartialSet.new)
18
23
  if block_given?
19
24
  set_binding_ivars(block.binding)
20
25
  instance_eval(&block)
@@ -33,6 +38,10 @@ module Osheet
33
38
  def templates
34
39
  get_ivar(:templates)
35
40
  end
41
+ def partial(name, &block); push_ivar(:partials, Partial.new(name, &block)); end
42
+ def partials
43
+ get_ivar(:partials)
44
+ end
36
45
 
37
46
  def attributes
38
47
  { :title => get_ivar(:title) }
@@ -7,6 +7,7 @@ module Osheet
7
7
  include Associations
8
8
  include WorkbookElement
9
9
  include MetaElement
10
+ include MarkupElement
10
11
 
11
12
  hm :columns
12
13
  hm :rows
@@ -34,7 +35,10 @@ module Osheet
34
35
  if get_ivar(:workbook) && get_ivar(:workbook).worksheets.collect{|ws| ws.name}.include?(name_value)
35
36
  raise ArgumentError, "the sheet name '#{name_value}' is already in use. choose a sheet name that is not used by another sheet"
36
37
  end
37
- name_value
38
+ if name_value.to_s.length > 31
39
+ raise ArgumentError, "worksheet names must be less than 32 characters long"
40
+ end
41
+ name_value.to_s
38
42
  end
39
43
 
40
44
  end
@@ -53,6 +53,7 @@ module Osheet::XmlssWriter::Elements
53
53
  ::Xmlss::Cell.new({
54
54
  :style_id => style_id(ocell.attributes[:style_class], ocell.attributes[:format]),
55
55
  :href => ocell.attributes[:href],
56
+ :index => ocell.attributes[:index],
56
57
  :merge_across => cell_merge(ocell.attributes[:colspan]),
57
58
  :merge_down => cell_merge(ocell.attributes[:rowspan]),
58
59
  :data => data(ocell.attributes[:data])
data/lib/osheet.rb CHANGED
@@ -3,23 +3,26 @@ module Osheet
3
3
  MIME_TYPE = "application/vnd.ms-excel"
4
4
 
5
5
  class << self
6
- # used to register an appropriate template handler
6
+ # used to register an appropriate view handler
7
7
  # and register a mime type if necessary
8
8
  def register
9
9
  ::Mime::Type.register MIME_TYPE, :xls if defined? ::Mime::Type
10
- require 'osheet/template_handler'
10
+ require 'osheet/view_handler'
11
11
  end
12
12
  end
13
13
 
14
14
  end
15
15
 
16
+ require 'osheet/instance'
17
+ require 'osheet/partial'
16
18
  require 'osheet/associations'
17
- require 'osheet/workbook_element'
18
- require 'osheet/worksheet_element'
19
+ require 'osheet/mixin'
20
+
21
+ require 'osheet/markup_element'
19
22
  require 'osheet/styled_element'
20
23
  require 'osheet/meta_element'
21
- require 'osheet/instance'
22
- require 'osheet/mixin'
24
+ require 'osheet/workbook_element'
25
+ require 'osheet/worksheet_element'
23
26
 
24
27
  if defined? Rails::Railtie
25
28
  require 'osheet/railtie'
data/test/cell_test.rb CHANGED
@@ -2,8 +2,8 @@ require "test/helper"
2
2
  require 'osheet/cell'
3
3
 
4
4
  module Osheet
5
- class CellTest < Test::Unit::TestCase
6
5
 
6
+ class CellTest < Test::Unit::TestCase
7
7
  context "Osheet::Cell" do
8
8
  subject { Cell.new }
9
9
 
@@ -11,7 +11,7 @@ module Osheet
11
11
  should_be_a_worksheet_element(Cell)
12
12
  should_be_a_workbook_element(Cell)
13
13
 
14
- should_have_instance_methods :data, :format, :colspan, :rowspan, :href
14
+ should_have_instance_methods :data, :format, :colspan, :rowspan, :href, :index
15
15
 
16
16
  should "set it's defaults" do
17
17
  assert_equal nil, subject.send(:get_ivar, "data")
@@ -19,57 +19,75 @@ module Osheet
19
19
  assert_equal 1, subject.send(:get_ivar, "colspan")
20
20
  assert_equal 1, subject.send(:get_ivar, "rowspan")
21
21
  assert_equal nil, subject.send(:get_ivar, "href")
22
+ assert_equal nil, subject.send(:get_ivar, "index")
22
23
  end
23
24
 
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
25
+ end
26
+ end
35
27
 
36
- should "should set them correctly" do
37
- assert_equal "Poo", subject.send(:get_ivar, "data")
38
- assert_kind_of Format::Number, subject.send(:get_ivar, "format")
39
- assert_equal 4, subject.send(:get_ivar, "colspan")
40
- assert_equal 2, subject.send(:get_ivar, "rowspan")
41
- assert_equal "http://www.google.com", subject.send(:get_ivar, "href")
28
+ class CellAttributeTest < Test::Unit::TestCase
29
+ context "a celll that has attributes" do
30
+ subject do
31
+ Cell.new do
32
+ style_class 'more poo'
33
+ data "Poo"
34
+ format :number
35
+ colspan 4
36
+ rowspan 2
37
+ index 3
38
+ href "http://www.google.com"
42
39
  end
40
+ end
43
41
 
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]
42
+ should "should set them correctly" do
43
+ assert_equal "Poo", subject.send(:get_ivar, "data")
44
+ assert_kind_of Format::Number, subject.send(:get_ivar, "format")
45
+ assert_equal 4, subject.send(:get_ivar, "colspan")
46
+ assert_equal 2, subject.send(:get_ivar, "rowspan")
47
+ assert_equal 3, subject.send(:get_ivar, "index")
48
+ assert_equal "http://www.google.com", subject.send(:get_ivar, "href")
49
+ end
50
+
51
+ should "know it's attribute(s)" do
52
+ [:style_class, :data, :format, :rowspan, :colspan, :index, :href].each do |a|
53
+ assert subject.attributes.has_key?(a)
54
54
  end
55
+
56
+ assert_equal "more poo", subject.attributes[:style_class]
57
+ assert_equal "Poo", subject.attributes[:data]
58
+ assert_kind_of Format::Number, subject.attributes[:format]
59
+ assert_equal 4, subject.attributes[:colspan]
60
+ assert_equal 2, subject.attributes[:rowspan]
61
+ assert_equal 3, subject.attributes[:index]
62
+ assert_equal "http://www.google.com", subject.attributes[:href]
55
63
  end
56
64
 
65
+ end
66
+ end
67
+
68
+ class CellDataTest < Test::Unit::TestCase
69
+ context "A cell" do
70
+ subject { Cell.new }
71
+
57
72
  should "type cast data strings/symbols" do
58
73
  ['a string', :symbol].each do |thing|
59
74
  cell = Cell.new{data thing}
60
75
  assert_kind_of ::String, cell.send(:get_ivar, "data")
61
76
  end
62
77
  end
78
+
63
79
  should "type cast data dates" do
64
80
  cell = Cell.new{data Date.today}
65
81
  assert_kind_of ::Date, cell.send(:get_ivar, "data")
66
82
  end
83
+
67
84
  should "type cast data numerics" do
68
85
  [1, 1.0].each do |thing|
69
86
  cell = Cell.new{data thing}
70
87
  assert_kind_of ::Numeric, cell.send(:get_ivar, "data")
71
88
  end
72
89
  end
90
+
73
91
  should "type cast all other data to string" do
74
92
  [Osheet, [:a, 'Aye'], {:a => 'Aye'}].each do |thing|
75
93
  cell = Cell.new{data thing}
@@ -78,7 +96,29 @@ module Osheet
78
96
  end
79
97
 
80
98
  end
99
+ end
81
100
 
101
+ class CellPartialTest < Test::Unit::TestCase
102
+ context "A workbook that defines column partials" do
103
+ subject do
104
+ Workbook.new {
105
+ partial(:cell_stuff) {
106
+ style_class 'more poo'
107
+ data "Poo"
108
+ }
109
+
110
+ worksheet { row { cell {
111
+ add :cell_stuff
112
+ } } }
113
+ }
114
+ end
115
+
116
+ should "add it's partials to it's markup" do
117
+ assert_equal 'more poo', subject.worksheets.first.rows.first.cells.first.attributes[:style_class]
118
+ assert_equal 'Poo', subject.worksheets.first.rows.first.cells.first.attributes[:data]
119
+ end
120
+
121
+ end
82
122
  end
83
123
 
84
124
  class CellBindingTest < Test::Unit::TestCase
data/test/column_test.rb CHANGED
@@ -2,8 +2,8 @@ require "test/helper"
2
2
  require "osheet/column"
3
3
 
4
4
  module Osheet
5
- class ColumnTest < Test::Unit::TestCase
6
5
 
6
+ class ColumnTest < Test::Unit::TestCase
7
7
  context "Osheet::Column" do
8
8
  subject { Column.new }
9
9
 
@@ -26,47 +26,56 @@ module Osheet
26
26
  assert_equal nil, subject.meta
27
27
  end
28
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
29
+ end
30
+ end
41
31
 
42
- should "should set them correctly" do
43
- assert_equal 100, subject.send(:get_ivar, "width")
44
- assert_equal true, subject.send(:get_ivar, "autofit")
45
- assert subject.autofit?
46
- assert_equal true, subject.send(:get_ivar, "hidden")
47
- assert subject.hidden?
48
- assert_equal({}, subject.meta)
32
+ class ColumnAttributesTest < Test::Unit::TestCase
33
+ context "that has attributes" do
34
+ subject do
35
+ Column.new do
36
+ style_class "more poo"
37
+ width 100
38
+ autofit true
39
+ hidden true
40
+ meta(
41
+ {}
42
+ )
49
43
  end
44
+ end
50
45
 
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
46
+ should "should set them correctly" do
47
+ assert_equal 100, subject.send(:get_ivar, "width")
48
+ assert_equal true, subject.send(:get_ivar, "autofit")
49
+ assert subject.autofit?
50
+ assert_equal true, subject.send(:get_ivar, "hidden")
51
+ assert subject.hidden?
52
+ assert_equal({}, subject.meta)
53
+ end
59
54
 
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]
55
+ should "know it's attribute(s)" do
56
+ [:style_class, :width, :autofit, :hidden].each do |a|
57
+ assert subject.attributes.has_key?(a)
68
58
  end
59
+ assert_equal 'more poo', subject.attributes[:style_class]
60
+ assert_equal 100, subject.attributes[:width]
61
+ assert_equal true, subject.attributes[:autofit]
62
+ assert_equal true, subject.attributes[:hidden]
63
+ end
64
+
65
+ end
66
+ end
69
67
 
68
+ class ColumnDataTest < Test::Unit::TestCase
69
+ context "A column" do
70
+ subject { Column.new }
71
+
72
+ should "set it's width" do
73
+ subject.width(false)
74
+ assert_equal false, subject.width
75
+ subject.width(180)
76
+ assert_equal 180, subject.width
77
+ subject.width(nil)
78
+ assert_equal 180, subject.width
70
79
  end
71
80
 
72
81
  should "cast autofit and hidden to bool" do
@@ -76,7 +85,29 @@ module Osheet
76
85
  end
77
86
 
78
87
  end
88
+ end
79
89
 
90
+ class ColumnPartialTest < Test::Unit::TestCase
91
+ context "A workbook that defines column partials" do
92
+ subject do
93
+ Workbook.new {
94
+ partial(:column_stuff) {
95
+ width 200
96
+ meta(:label => 'awesome')
97
+ }
98
+
99
+ worksheet { column {
100
+ add :column_stuff
101
+ } }
102
+ }
103
+ end
104
+
105
+ should "add it's partials to it's markup" do
106
+ assert_equal 200, subject.worksheets.first.columns.first.width
107
+ assert_equal({:label => 'awesome'}, subject.worksheets.first.columns.first.meta)
108
+ end
109
+
110
+ end
80
111
  end
81
112
 
82
113
  class ColumnBindingTest < Test::Unit::TestCase
data/test/env.rb CHANGED
@@ -6,4 +6,5 @@
6
6
  $LOAD_PATH.unshift(full_path) unless $LOAD_PATH.include?(full_path)
7
7
  end
8
8
 
9
+ puts "osheet require"
9
10
  require 'osheet'