tidy_table 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +3 -0
- data/Manifest.txt +7 -0
- data/README.txt +44 -0
- data/Rakefile +19 -0
- data/lib/tidy_table.rb +65 -0
- data/spec/spec_helper.rb +5 -0
- data/spec/tidy_table_spec.rb +26 -0
- metadata +63 -0
data/History.txt
ADDED
data/Manifest.txt
ADDED
data/README.txt
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
TidyTable
|
2
|
+
by Geoffrey Grosenbach
|
3
|
+
http://topfunky.com
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
Yet another library to convert an array of structs (such as an ActiveRecord object) into an HTML table. Simple, with a few options. Includes automatic tags for first_column, first_row, even, odd, etc.
|
8
|
+
|
9
|
+
== FEATURES/PROBLEMS:
|
10
|
+
|
11
|
+
* First release
|
12
|
+
|
13
|
+
== REQUIREMENTS:
|
14
|
+
|
15
|
+
* None
|
16
|
+
|
17
|
+
== INSTALL:
|
18
|
+
|
19
|
+
* sudo gem install tidy_table
|
20
|
+
|
21
|
+
== LICENSE:
|
22
|
+
|
23
|
+
(The MIT License)
|
24
|
+
|
25
|
+
Copyright (c) 2007 Geoffrey Grosenbach
|
26
|
+
|
27
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
28
|
+
a copy of this software and associated documentation files (the
|
29
|
+
'Software'), to deal in the Software without restriction, including
|
30
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
31
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
32
|
+
permit persons to whom the Software is furnished to do so, subject to
|
33
|
+
the following conditions:
|
34
|
+
|
35
|
+
The above copyright notice and this permission notice shall be
|
36
|
+
included in all copies or substantial portions of the Software.
|
37
|
+
|
38
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
39
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
40
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
41
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
42
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
43
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
44
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
|
2
|
+
require 'rubygems'
|
3
|
+
require 'hoe'
|
4
|
+
require './lib/tidy_table.rb'
|
5
|
+
|
6
|
+
Hoe.new('tidy_table', TidyTable::VERSION) do |p|
|
7
|
+
p.rubyforge_name = 'seattlerb'
|
8
|
+
p.author = 'Geoffrey Grosenbach'
|
9
|
+
p.email = 'boss AT topfunky.com'
|
10
|
+
p.summary = 'Yet another library for converting a struct into an HTML table.'
|
11
|
+
p.description = p.paragraphs_of('README.txt', 0..1).join("\n\n")
|
12
|
+
p.url = "http://rubyforge.org/projects/seattlerb"
|
13
|
+
p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
|
14
|
+
end
|
15
|
+
|
16
|
+
desc "Test task (actually runs specs)"
|
17
|
+
task "test" do
|
18
|
+
system "spec --format specdoc --color spec/*_spec.rb"
|
19
|
+
end
|
data/lib/tidy_table.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
##
|
2
|
+
# Yet another library to convert an ActiveRecord array (or any struct) and output a simple HTML table.
|
3
|
+
#
|
4
|
+
# Can also send the fields back to a block for special formatting.
|
5
|
+
#
|
6
|
+
# <%= TidyTable.new(@records, :table_class => "revenue_report").to_html(%w(resource visits min max)) do |row|
|
7
|
+
# [
|
8
|
+
# row.resource,
|
9
|
+
# number_with_delimiter(row.visit_count),
|
10
|
+
# row.min,
|
11
|
+
# row.max
|
12
|
+
# ]
|
13
|
+
# end %>
|
14
|
+
#
|
15
|
+
|
16
|
+
class TidyTable
|
17
|
+
|
18
|
+
VERSION = '0.0.1'
|
19
|
+
|
20
|
+
def initialize(array, options={})
|
21
|
+
@rows = array
|
22
|
+
@options = {
|
23
|
+
:table_class => "tidytable",
|
24
|
+
:first_row_class => "first_row",
|
25
|
+
:first_column_class => "first_column"
|
26
|
+
}.merge(options)
|
27
|
+
end
|
28
|
+
|
29
|
+
##
|
30
|
+
# First argument is an array of the names of the fields you want to display.
|
31
|
+
#
|
32
|
+
# If given, a block will be called for each row of the array. You should
|
33
|
+
# return an array with the values formatted in the format that you want to see
|
34
|
+
# in the resulting cells.
|
35
|
+
|
36
|
+
def to_html(fields)
|
37
|
+
output = []
|
38
|
+
output << %(<table class="#{@options[:table_class]}">)
|
39
|
+
|
40
|
+
# First row
|
41
|
+
output << %(<tr class="#{@options[:first_row_class]}">)
|
42
|
+
fields.each_with_index do |field, index|
|
43
|
+
output << %(<th class="#{@options[:first_row_class]} #{ index == 0 ? @options[:first_column_class] : ''}">#{field}</th>)
|
44
|
+
end
|
45
|
+
output << "</tr>"
|
46
|
+
|
47
|
+
@rows.each_with_index do |row, row_index|
|
48
|
+
output << %(<tr class="#{row_index % 2 == 0 ? 'even': 'odd'}">)
|
49
|
+
if block_given?
|
50
|
+
formatted_data = yield(row)
|
51
|
+
formatted_data.each_with_index do |data, index|
|
52
|
+
output << %(<td class="#{ index == 0 ? @options[:first_column_class] : ''}">#{data}</td>)
|
53
|
+
end
|
54
|
+
else
|
55
|
+
fields.each_with_index do |field, index|
|
56
|
+
output << %(<td class="#{ index == 0 ? @options[:first_column_class] : ''}">#{row.send(field.to_sym)}</td>)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
output << "</tr>"
|
60
|
+
end
|
61
|
+
output << "</table>"
|
62
|
+
output.join
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
class RecordStruct < Struct.new(:title, :description); end
|
4
|
+
|
5
|
+
describe TidyTable do
|
6
|
+
|
7
|
+
before do
|
8
|
+
@records = [RecordStruct.new("Cheese", "A sample of fine cheeses")]
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should initialize with defaults" do
|
12
|
+
t = TidyTable.new(@records)
|
13
|
+
t.to_html([:title, :description]) {|row| [row.title, row.description]}.should match(/Cheese/)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should run without block" do
|
17
|
+
t = TidyTable.new(@records)
|
18
|
+
t.to_html([:title, :description]).should match(/Cheese/)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should merge options" do
|
22
|
+
t = TidyTable.new(@records, :table_class => "cheese_table_class")
|
23
|
+
t.to_html([:title, :description]).should match(/cheese_table_class/)
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.9.0
|
3
|
+
specification_version: 1
|
4
|
+
name: tidy_table
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.0.1
|
7
|
+
date: 2007-06-20 00:00:00 -07:00
|
8
|
+
summary: Yet another library for converting a struct into an HTML table.
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: boss AT topfunky.com
|
12
|
+
homepage: http://rubyforge.org/projects/seattlerb
|
13
|
+
rubyforge_project: seattlerb
|
14
|
+
description: "TidyTable by Geoffrey Grosenbach http://topfunky.com == DESCRIPTION: Yet another library to convert an array of structs (such as an ActiveRecord object) into an HTML table. Simple, with a few options. Includes automatic tags for first_column, first_row, even, odd, etc."
|
15
|
+
autorequire:
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: true
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
post_install_message:
|
29
|
+
authors:
|
30
|
+
- Geoffrey Grosenbach
|
31
|
+
files:
|
32
|
+
- History.txt
|
33
|
+
- Manifest.txt
|
34
|
+
- README.txt
|
35
|
+
- Rakefile
|
36
|
+
- lib/tidy_table.rb
|
37
|
+
- spec/spec_helper.rb
|
38
|
+
- spec/tidy_table_spec.rb
|
39
|
+
test_files: []
|
40
|
+
|
41
|
+
rdoc_options:
|
42
|
+
- --main
|
43
|
+
- README.txt
|
44
|
+
extra_rdoc_files:
|
45
|
+
- History.txt
|
46
|
+
- Manifest.txt
|
47
|
+
- README.txt
|
48
|
+
executables: []
|
49
|
+
|
50
|
+
extensions: []
|
51
|
+
|
52
|
+
requirements: []
|
53
|
+
|
54
|
+
dependencies:
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: hoe
|
57
|
+
version_requirement:
|
58
|
+
version_requirements: !ruby/object:Gem::Version::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 1.2.1
|
63
|
+
version:
|