tidy_table 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History.txt +6 -0
- data/Rakefile +1 -1
- data/lib/tidy_table.rb +41 -12
- data/spec/tidy_table_spec.rb +23 -0
- metadata +2 -2
data/History.txt
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
== 0.0.4 / 2007-06-27
|
2
|
+
|
3
|
+
* Changed default table class name to 'tidy_table' for consistency.
|
4
|
+
* Added ability to return a Hash from the block passed to TidyTable#to_html.
|
5
|
+
This allows the :id or :class of the row to be set along with the :data.
|
6
|
+
|
1
7
|
== 0.0.3 / 2007-06-24
|
2
8
|
|
3
9
|
* Added automatic last_column_class to header row also
|
data/Rakefile
CHANGED
data/lib/tidy_table.rb
CHANGED
@@ -15,12 +15,12 @@
|
|
15
15
|
|
16
16
|
class TidyTable
|
17
17
|
|
18
|
-
VERSION = '0.0.
|
18
|
+
VERSION = '0.0.4'
|
19
19
|
|
20
20
|
##
|
21
21
|
# Make a new TidyTable with a data array and CSS options.
|
22
22
|
#
|
23
|
-
# * :table_class -- Defaults to '
|
23
|
+
# * :table_class -- Defaults to 'tidy_table'
|
24
24
|
# * :first_row_class -- Defaults to 'first_row'
|
25
25
|
# * :first_column_class -- Defaults to 'first_column'
|
26
26
|
# * :last_column_class -- Defaults to 'last_column'
|
@@ -30,7 +30,7 @@ class TidyTable
|
|
30
30
|
def initialize(array, options={})
|
31
31
|
@rows = array
|
32
32
|
@options = {
|
33
|
-
:table_class => "
|
33
|
+
:table_class => "tidy_table",
|
34
34
|
:first_row_class => "first_row",
|
35
35
|
:first_column_class => "first_column",
|
36
36
|
:last_column_class => "last_column"
|
@@ -38,17 +38,24 @@ class TidyTable
|
|
38
38
|
end
|
39
39
|
|
40
40
|
##
|
41
|
-
# First argument is an array of
|
41
|
+
# First argument is an array of column names.
|
42
|
+
# Will also be called as methods on each row object if no block is provided.
|
42
43
|
#
|
43
44
|
# If given, a block will be called for each row of the array. You should
|
44
|
-
# return an
|
45
|
+
# return an Array with the values formatted in the format that you want to see
|
45
46
|
# in the resulting cells.
|
47
|
+
#
|
48
|
+
# Or, return a Hash where
|
49
|
+
#
|
50
|
+
# :data => ['contents', 'for', 'cells'],
|
51
|
+
# :id => 'id_for_this_row',
|
52
|
+
# :class => 'extra_classes for_this row'
|
46
53
|
|
47
|
-
def to_html(fields)
|
54
|
+
def to_html(fields)
|
48
55
|
output = []
|
49
56
|
output << %(<table class="#{@options[:table_class]}">)
|
50
57
|
|
51
|
-
# First row
|
58
|
+
# First row (header)
|
52
59
|
output << %(<tr class="#{@options[:first_row_class]}">)
|
53
60
|
fields.each_with_index do |field, index|
|
54
61
|
output << %(<th class="#{@options[:first_row_class]} #{column_classes(fields, index)}">#{field}</th>)
|
@@ -56,13 +63,28 @@ class TidyTable
|
|
56
63
|
output << "</tr>"
|
57
64
|
|
58
65
|
@rows.each_with_index do |row, row_index|
|
59
|
-
output << %(<tr class="#{row_index % 2 == 0 ? 'even': 'odd'}">)
|
60
66
|
if block_given?
|
61
|
-
|
62
|
-
|
63
|
-
|
67
|
+
yield_output = yield(row)
|
68
|
+
|
69
|
+
data = []
|
70
|
+
row_options = {}
|
71
|
+
case yield_output
|
72
|
+
when Array
|
73
|
+
data = yield_output
|
74
|
+
when Hash
|
75
|
+
data = yield_output.delete(:data)
|
76
|
+
row_options = yield_output
|
77
|
+
else
|
78
|
+
raise ArgumentError, "TidyTable block expects an Array or Hash, but a #{yield_output.class} was returned."
|
79
|
+
end
|
80
|
+
|
81
|
+
row_classes = [row_index % 2 == 0 ? 'even': 'odd', row_options[:class]].select {|i| !i.nil?}.join(' ')
|
82
|
+
output << %(<tr class="#{row_classes}" #{"id=\"#{row_options[:id]}\"" if row_options.has_key?(:id)}>)
|
83
|
+
data.each_with_index do |item, index|
|
84
|
+
output << %(<td class="#{column_classes(data, index)}">#{item}</td>)
|
64
85
|
end
|
65
86
|
else
|
87
|
+
output << %(<tr class="#{row_index % 2 == 0 ? 'even': 'odd'}">)
|
66
88
|
fields.each_with_index do |field, index|
|
67
89
|
output << %(<td class="#{column_classes(fields, index)}">#{row.send(field.to_sym)}</td>)
|
68
90
|
end
|
@@ -77,10 +99,17 @@ class TidyTable
|
|
77
99
|
|
78
100
|
##
|
79
101
|
# Returns "first_column", "last_column", or both.
|
102
|
+
#
|
103
|
+
# additional_class_names is a string of other class names to append to the
|
104
|
+
# autogenerated classes.
|
80
105
|
|
81
|
-
def column_classes(fields_array, index)
|
106
|
+
def column_classes(fields_array, index, additional_class_names=nil)
|
82
107
|
classes = (index == 0 ? @options[:first_column_class] : '')
|
83
108
|
classes += (index == fields_array.length - 1 ? @options[:last_column_class] : '')
|
109
|
+
if additional_class_names
|
110
|
+
classes += " " + additional_class_names
|
111
|
+
end
|
112
|
+
classes
|
84
113
|
end
|
85
114
|
|
86
115
|
end
|
data/spec/tidy_table_spec.rb
CHANGED
@@ -35,4 +35,27 @@ describe TidyTable do
|
|
35
35
|
content.should match(/the_end_my_friend/)
|
36
36
|
end
|
37
37
|
|
38
|
+
it "should set row title" do
|
39
|
+
t = TidyTable.new(@records)
|
40
|
+
t.to_html([:title, :description]) do |record|
|
41
|
+
{:id => "cheese_1", :data => [record.title, record.description]}
|
42
|
+
end.should match(/tr class="even" id="cheese_1"/)
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should set row class" do
|
46
|
+
t = TidyTable.new(@records)
|
47
|
+
t.to_html([:title, :description]) do |record|
|
48
|
+
{:class => "cheese_class", :data => [record.title, record.description]}
|
49
|
+
end.should match(/tr class="even cheese_class"/)
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should raise if wrong argument passed from block" do
|
53
|
+
t = TidyTable.new(@records)
|
54
|
+
lambda {
|
55
|
+
t.to_html([:title, :description]) do |record|
|
56
|
+
Date.new
|
57
|
+
end
|
58
|
+
}.should raise_error(ArgumentError)
|
59
|
+
end
|
60
|
+
|
38
61
|
end
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
|
|
3
3
|
specification_version: 1
|
4
4
|
name: tidy_table
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.0.
|
7
|
-
date: 2007-06-
|
6
|
+
version: 0.0.4
|
7
|
+
date: 2007-06-27 00:00:00 -07:00
|
8
8
|
summary: Yet another library for converting a struct into an HTML table.
|
9
9
|
require_paths:
|
10
10
|
- lib
|