osheet 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,15 +1,17 @@
1
1
  = Osheet
2
- * Osheet is under development and should not be relied upon just yet. Thx. *
3
2
 
4
3
  == Description
5
4
 
6
- Pronounced 'oh-sheeeeeet!' - this gem is a DSL wrapper to the spreadsheet gem that hopefully doesn't totally suck.
5
+ A DSL for specifying and generating spreadsheets using Ruby.
7
6
 
8
7
  == Installation
9
8
 
10
9
  $ gem install osheet
11
10
 
12
11
  == Basic Example
12
+
13
+ require 'osheet'
14
+
13
15
  fields = ['Sex', 'Age', 'Height', 'Weight']
14
16
  data = {
15
17
  'Tom' => ['M', 52, "6'2\"", '220 lbs.'],
@@ -19,9 +21,50 @@ Pronounced 'oh-sheeeeeet!' - this gem is a DSL wrapper to the spreadsheet gem th
19
21
 
20
22
  # this will dump the above data to a single-sheet workbook w/ no styles
21
23
 
22
- require 'osheet'
24
+ Osheet::Workbook.new {
25
+ title "basic"
26
+
27
+ template(:column, :data) { |field, index|
28
+ width 80
29
+ meta(
30
+ :label => field.to_s,
31
+ :index => index
32
+ )
33
+ }
34
+
35
+ template(:row, :title) {
36
+ cell {
37
+ colspan columns.count
38
+ data worksheet.name
39
+ }
40
+ }
41
+
42
+ template(:row, :empty) {
43
+ cell {
44
+ colspan columns.count
45
+ data ''
46
+ }
47
+ }
48
+
49
+ template(:row, :header) {
50
+ columns.each do |column|
51
+ cell {
52
+ data column.meta[:label]
53
+ }
54
+ end
55
+ }
56
+
57
+ template(:row, :data) { |name, stats|
58
+ cell {
59
+ data name
60
+ }
61
+ stats.each do |stat|
62
+ cell {
63
+ data stat
64
+ }
65
+ end
66
+ }
23
67
 
24
- wb = Osheet::Workbook.new {
25
68
  worksheet {
26
69
  name "Stats: #{fields.join(', ')}"
27
70
 
@@ -32,103 +75,34 @@ Pronounced 'oh-sheeeeeet!' - this gem is a DSL wrapper to the spreadsheet gem th
32
75
  )
33
76
  }
34
77
  fields.each_with_index do |f, i|
35
- column {
36
- width 80
37
- meta(
38
- :label => f.to_s,
39
- :index => i
40
- )
41
- }
78
+ column :data, f, i
42
79
  end
43
80
 
44
- row { # title row
45
- cell {
46
- colspan columns.count
47
- data worksheet.name
48
- }
49
- }
50
- row { # empty row
51
- cell {
52
- colspan columns.count
53
- data ''
54
- }
55
- }
56
- row { # header row
57
- columns.each do |column|
58
- cell {
59
- data column.meta[:label]
60
- }
61
- end
62
- }
81
+ row :title
82
+ row :empty
83
+ row :header
63
84
 
64
85
  data.each do |name, stats|
65
- row { # data row
66
- cell {
67
- data name
68
- }
69
- stats.each do |stat|
70
- cell {
71
- data stat
72
- }
73
- end
74
- }
86
+ row :data, name, stats
75
87
  end
76
- } # worksheet
77
- } # workbook
88
+ }
89
+ }.to_file('stats.xls')
78
90
 
79
- file = wb.to_file('stats') # => <spreadsheet written to ./stats.xls>
80
-
81
- == Examples
91
+ == API
82
92
 
83
- I've add a few examples to ./examples. Please refer to these for examples on basic usage, using templates, formatting data, and styling data.
93
+ Check out the wiki: https://github.com/kelredd/osheet/wiki. It covers the full Osheet API.
84
94
 
85
- == API
95
+ == Examples
86
96
 
87
- These classes define how a spreadsheet is constructed.
88
- === Osheet::Workbook
89
- * *style(selector, &block)*: define a style for the workbook
90
- * *template(for, named, &block)*: define a named template for the workbook
91
- * *worksheet(&block)*: define a worksheet for the workbook
92
- # TODO: *use(module)*
93
- # TODO: *use(file)*
94
-
95
- === Osheet::Worksheet
96
- * *name(value)*: set the name for this worksheet
97
- * *column(&block)*: define a column for the worksheet
98
- * *column(:template, *args): define a templated column for the worksheet
99
- * *row(&block)*: define a row for the worksheet
100
- * *row(:template, *args): define a templated row for the worksheet
101
-
102
- === Xmlss::Column
103
- * *style_class(value)*: (string) the styles selectors should match against
104
- * *width(value)*: (numeric) set the explicit width for the column
105
- * *auto_fit_width(value)*: (bool) set whether the column should auto fit it's width, default: false
106
- * *hidden(value)*: (bool) set whether the column is hidden, default: false
107
- * *meta(data)*: (anything) a generic way to associate meta-data with the column
108
-
109
- === Xmlss::Row
110
- * *style_class(value)*: (string) the styles selectors should match against
111
- * *height(value)*: (numeric) set the explicit width for the column
112
- * *auto_fit_height(value)*: (bool) set whether the row should auto fit it's height, default: false
113
- * *hidden(value)*: (bool) set whether the row is hidden, default: false
114
- * *cell(&block)*: define a cell for the row
115
- * *cell(:template, *args): define a templated cell for the row
116
-
117
- === Xmlss::Cell
118
- * *style_class(value)*: (string) the styles selectors should match against
119
- * *data(value)*: (anything), data the cell should contain. if not a string, numeric, or date, will cast data to sting using 'inspect'.
120
- * format(value)*: (anything), optional, custom formatting string for the data (see driver documentation for options)
121
- * *colspan(value)*: (int) the number of columns (l to r) the cell should occupy, default: 1
122
- * *rowspan(value)*: (int) the number of rows (t to b) the cell should occupy, default: 1
123
- * *href(value)*: (string) set the href the data should link to
97
+ I've add a few examples to ./examples. Please refer first to the API then to these for examples on basic usage, using templates, formatting data, and styling data.
124
98
 
125
99
  == Links
126
100
 
127
- * *GitHub*
101
+ * *Osheet*
128
102
  - http://github.com/kelredd/osheet
129
103
 
130
- * *Spreadsheet*
131
- - http://spreadsheet.rubyforge.org/
104
+ * *Wiki*
105
+ - https://github.com/kelredd/osheet/wiki
132
106
 
133
107
  == License
134
108
 
data/lib/osheet.rb CHANGED
@@ -8,5 +8,6 @@ require 'osheet/associations'
8
8
  require 'osheet/workbook_element'
9
9
  require 'osheet/worksheet_element'
10
10
  require 'osheet/styled_element'
11
+ require 'osheet/meta_element'
11
12
 
12
13
  require 'osheet/workbook'
data/lib/osheet/column.rb CHANGED
@@ -3,6 +3,7 @@ module Osheet
3
3
  include WorkbookElement
4
4
  include WorksheetElement
5
5
  include StyledElement
6
+ include MetaElement
6
7
 
7
8
  def initialize(workbook=nil, worksheet=nil, *args, &block)
8
9
  @workbook = workbook
@@ -10,7 +11,6 @@ module Osheet
10
11
  @width = nil
11
12
  @autofit = false
12
13
  @hidden = false
13
- @meta = nil
14
14
  instance_exec(*args, &block) if block_given?
15
15
  end
16
16
 
@@ -31,8 +31,5 @@ module Osheet
31
31
  }
32
32
  end
33
33
 
34
- def meta(value=nil)
35
- value.nil? ? @meta : (@meta = value)
36
- end
37
34
  end
38
35
  end
@@ -0,0 +1,7 @@
1
+ module Osheet::MetaElement
2
+
3
+ def meta(value=nil)
4
+ value.nil? ? instance_variable_get("@meta") : instance_variable_set("@meta", value)
5
+ end
6
+
7
+ end
@@ -0,0 +1,30 @@
1
+ require 'osheet/style'
2
+ require 'osheet/template'
3
+
4
+ module Osheet::Mixin
5
+
6
+ def self.included(receiver)
7
+ receiver.send(:extend, ClassMethods)
8
+ end
9
+
10
+ module ClassMethods
11
+ def style(*selectors, &block)
12
+ instance_variable_set("@s",
13
+ (instance_variable_get("@s") || []) << ::Osheet::Style.new(*selectors, &block)
14
+ )
15
+ end
16
+ def styles
17
+ instance_variable_get("@s") || []
18
+ end
19
+
20
+ def template(element, name, &block)
21
+ instance_variable_set("@t",
22
+ (instance_variable_get("@t") || []) << ::Osheet::Template.new(element, name, &block)
23
+ )
24
+ end
25
+ def templates
26
+ instance_variable_get("@t") || []
27
+ end
28
+ end
29
+
30
+ end
data/lib/osheet/row.rb CHANGED
@@ -6,6 +6,7 @@ module Osheet
6
6
  include WorkbookElement
7
7
  include WorksheetElement
8
8
  include StyledElement
9
+ include MetaElement
9
10
 
10
11
  hm :cells
11
12
 
@@ -1,3 +1,3 @@
1
1
  module Osheet
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -27,6 +27,11 @@ module Osheet
27
27
  { :title => @title }
28
28
  end
29
29
 
30
+ def use(mixin)
31
+ (mixin.styles || []).each{ |s| @styles << s }
32
+ (mixin.templates || []).each{ |t| @templates << t }
33
+ end
34
+
30
35
  def writer
31
36
  XmlssWriter::Base.new(:workbook => self)
32
37
  end
@@ -5,6 +5,7 @@ module Osheet
5
5
  class Worksheet
6
6
  include Associations
7
7
  include WorkbookElement
8
+ include MetaElement
8
9
 
9
10
  hm :columns
10
11
  hm :rows
@@ -5,11 +5,14 @@ module Osheet::WorksheetElement
5
5
  end
6
6
  end
7
7
 
8
- def columns
9
- if worksheet && worksheet.respond_to?(:columns)
10
- worksheet.columns
11
- else
12
- nil
8
+ [:columns, :rows].each do |meth|
9
+ define_method(meth) do
10
+ if worksheet && worksheet.respond_to?(meth)
11
+ worksheet.send(meth)
12
+ else
13
+ nil
14
+ end
13
15
  end
14
16
  end
17
+
15
18
  end
data/osheet.gemspec CHANGED
@@ -9,8 +9,8 @@ Gem::Specification.new do |s|
9
9
  s.authors = ["Kelly Redding"]
10
10
  s.email = ["kelly@kelredd.com"]
11
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.}
12
+ s.summary = %q{A DSL for specifying and generating spreadsheets using Ruby}
13
+ s.description = %q{A DSL for specifying and generating spreadsheets using Ruby}
14
14
 
15
15
  s.files = `git ls-files`.split("\n")
16
16
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
@@ -0,0 +1,47 @@
1
+ require "test/helper"
2
+ require 'osheet/mixin'
3
+ require 'test/mixins'
4
+
5
+ module Osheet
6
+
7
+ class MixinBaseTest < Test::Unit::TestCase
8
+ context "Osheet::Mixin thing" do
9
+ subject { DefaultMixin }
10
+
11
+ should_have_readers :styles, :templates
12
+ should_have_instance_methods :style, :template
13
+
14
+ should "set it's defaults" do
15
+ assert_equal [], subject.styles
16
+ assert_equal [], subject.templates
17
+ end
18
+ end
19
+ end
20
+
21
+ class MixinStyleTest < Test::Unit::TestCase
22
+ context "that defines styles" do
23
+ subject { StyledMixin }
24
+
25
+ should "have it's styles defined" do
26
+ assert_equal 2, subject.styles.size
27
+ assert_equal 1, subject.styles.first.selectors.size
28
+ assert_equal '.test', subject.styles.first.selectors.first
29
+ assert_equal 1, subject.styles.last.selectors.size
30
+ assert_equal '.test.awesome', subject.styles.last.selectors.first
31
+ end
32
+ end
33
+ end
34
+
35
+ class MixinTemplateTest < Test::Unit::TestCase
36
+ context "that defines templates" do
37
+ subject { TemplatedMixin }
38
+
39
+ should "have it's templates defined" do
40
+ assert subject.templates
41
+ assert_equal 3, subject.templates.size
42
+ assert_kind_of Template, subject.templates.first
43
+ end
44
+ end
45
+ end
46
+
47
+ end
data/test/mixins.rb ADDED
@@ -0,0 +1,31 @@
1
+ require 'osheet/mixin'
2
+
3
+ class DefaultMixin
4
+ include Osheet::Mixin
5
+
6
+ end
7
+
8
+ class StyledMixin
9
+ include Osheet::Mixin
10
+
11
+ style('.test')
12
+ style('.test.awesome')
13
+
14
+ end
15
+
16
+ class TemplatedMixin
17
+ include Osheet::Mixin
18
+
19
+ template(:column, :yo) { |color|
20
+ width 200
21
+ meta(:color => color)
22
+ }
23
+ template(:row, :yo_yo) {
24
+ height 500
25
+ }
26
+ template(:worksheet, :go) {
27
+ column(:yo, 'blue')
28
+ row(:yo_yo)
29
+ }
30
+
31
+ end
data/test/row_test.rb CHANGED
@@ -14,6 +14,7 @@ module Osheet
14
14
  should_have_instance_method :height
15
15
  should_have_instance_methods :autofit, :autofit?
16
16
  should_have_instance_methods :hidden, :hidden?
17
+ should_have_instance_method :meta
17
18
 
18
19
  should "set it's defaults" do
19
20
  assert_equal nil, subject.send(:instance_variable_get, "@height")
@@ -23,6 +24,7 @@ module Osheet
23
24
  assert !subject.hidden?
24
25
 
25
26
  assert_equal [], subject.cells
27
+ assert_equal nil, subject.meta
26
28
  end
27
29
 
28
30
  context "that has attributes" do
@@ -32,6 +34,9 @@ module Osheet
32
34
  height 100
33
35
  autofit true
34
36
  hidden true
37
+ meta(
38
+ {}
39
+ )
35
40
  end
36
41
  end
37
42
 
@@ -41,6 +46,7 @@ module Osheet
41
46
  assert subject.autofit?
42
47
  assert_equal true, subject.send(:instance_variable_get, "@hidden")
43
48
  assert subject.hidden?
49
+ assert_equal({}, subject.meta)
44
50
  end
45
51
 
46
52
  should "know it's height" do
@@ -1,5 +1,6 @@
1
1
  require "test/helper"
2
2
  require 'osheet/workbook'
3
+ require 'test/mixins'
3
4
 
4
5
  module Osheet
5
6
  class WorkbookTest < Test::Unit::TestCase
@@ -8,7 +9,7 @@ module Osheet
8
9
  subject { Workbook.new }
9
10
 
10
11
  should_have_readers :styles, :templates
11
- should_have_instance_methods :title, :style, :template, :attributes
12
+ should_have_instance_methods :title, :style, :template, :attributes, :use
12
13
 
13
14
  should_hm(Workbook, :worksheets, Worksheet)
14
15
 
@@ -138,6 +139,40 @@ module Osheet
138
139
 
139
140
  end
140
141
 
142
+ class WorkbookMixins < Test::Unit::TestCase
143
+ context "a workbook w/ mixins" do
144
+ subject do
145
+ Workbook.new {
146
+ use StyledMixin
147
+ use TemplatedMixin
148
+ }
149
+ end
150
+
151
+ should "add the mixin styles to it's styles" do
152
+ assert_equal 2, subject.styles.size
153
+ assert_equal 1, subject.styles.first.selectors.size
154
+ assert_equal '.test', subject.styles.first.selectors.first
155
+ assert_equal 1, subject.styles.last.selectors.size
156
+ assert_equal '.test.awesome', subject.styles.last.selectors.first
157
+ end
158
+
159
+ should "add the mixin templates to it's templates" do
160
+ assert subject.templates
161
+ assert_kind_of TemplateSet, subject.templates
162
+ assert_equal 3, subject.templates.keys.size
163
+ assert_kind_of Template, subject.templates.get('column', 'yo')
164
+ assert_kind_of Template, subject.templates.get('row', 'yo_yo')
165
+ assert_kind_of Template, subject.templates.get('worksheet', 'go')
166
+
167
+ subject.worksheet(:go)
168
+ assert_equal 1, subject.worksheets.size
169
+ assert_equal 'blue', subject.worksheets.first.columns.first.meta[:color]
170
+ assert_equal 500, subject.worksheets.first.rows.first.attributes[:height]
171
+ end
172
+
173
+ end
174
+ end
175
+
141
176
  class WorkbookWriter < Test::Unit::TestCase
142
177
  context "a workbook" do
143
178
  subject do
@@ -10,11 +10,14 @@ module Osheet
10
10
  should_be_a_workbook_element(Worksheet)
11
11
 
12
12
  should_have_instance_methods :name, :attributes
13
+ should_have_instance_method :meta
13
14
 
14
15
  should "set it's defaults" do
15
16
  assert_equal nil, subject.send(:instance_variable_get, "@name")
16
17
  assert_equal [], subject.columns
17
18
  assert_equal [], subject.rows
19
+
20
+ assert_equal nil, subject.meta
18
21
  end
19
22
 
20
23
  should_hm(Worksheet, :columns, Column)
@@ -24,6 +27,9 @@ module Osheet
24
27
  subject do
25
28
  Worksheet.new do
26
29
  name "Poo!"
30
+ meta(
31
+ {}
32
+ )
27
33
 
28
34
  column
29
35
 
@@ -36,8 +42,9 @@ module Osheet
36
42
  end
37
43
  end
38
44
 
39
- should "set it's name" do
45
+ should "set it's name and meta" do
40
46
  assert_equal "Poo!", subject.send(:instance_variable_get, "@name")
47
+ assert_equal({}, subject.meta)
41
48
  end
42
49
 
43
50
  should "know it's name" do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: osheet
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 3
8
+ - 4
9
9
  - 0
10
- version: 0.3.0
10
+ version: 0.4.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Kelly Redding
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-04-11 00:00:00 -05:00
18
+ date: 2011-04-14 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -81,7 +81,7 @@ dependencies:
81
81
  version: 0.1.0
82
82
  type: :runtime
83
83
  version_requirements: *id004
84
- description: 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.
84
+ description: A DSL for specifying and generating spreadsheets using Ruby
85
85
  email:
86
86
  - kelly@kelredd.com
87
87
  executables: []
@@ -119,6 +119,8 @@ files:
119
119
  - lib/osheet/format/scientific.rb
120
120
  - lib/osheet/format/special.rb
121
121
  - lib/osheet/format/text.rb
122
+ - lib/osheet/meta_element.rb
123
+ - lib/osheet/mixin.rb
122
124
  - lib/osheet/row.rb
123
125
  - lib/osheet/style.rb
124
126
  - lib/osheet/style_set.rb
@@ -151,6 +153,8 @@ files:
151
153
  - test/format/text_test.rb
152
154
  - test/format_test.rb
153
155
  - test/helper.rb
156
+ - test/mixin_test.rb
157
+ - test/mixins.rb
154
158
  - test/osheet_test.rb
155
159
  - test/row_test.rb
156
160
  - test/style_set_test.rb
@@ -195,7 +199,7 @@ rubyforge_project:
195
199
  rubygems_version: 1.3.7
196
200
  signing_key:
197
201
  specification_version: 3
198
- summary: A DSL for generating spreadsheets that doesn't totally suck - pronounced 'Oh sheeeeeet!'
202
+ summary: A DSL for specifying and generating spreadsheets using Ruby
199
203
  test_files:
200
204
  - test/cell_test.rb
201
205
  - test/column_test.rb
@@ -213,6 +217,8 @@ test_files:
213
217
  - test/format/text_test.rb
214
218
  - test/format_test.rb
215
219
  - test/helper.rb
220
+ - test/mixin_test.rb
221
+ - test/mixins.rb
216
222
  - test/osheet_test.rb
217
223
  - test/row_test.rb
218
224
  - test/style_set_test.rb