dt 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/dt.rb +85 -0
- data/test/tc_dt.rb +20 -0
- metadata +46 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c46079508d7cdf2fec20164a516c8ccd9149cc86
|
4
|
+
data.tar.gz: 7047fc72b237ed5d2fb79c6d6361657656d0cdad
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ec0268089da0958acad2ccc73380b2e59e65557c33b0e52682b8b4e708f6907dbce195e6d823c0dc9e003a2806d9276a1736ee773fdf3645019e1ec70a560d58
|
7
|
+
data.tar.gz: 37743632e3f6f14255a2fbc557f2af745aa14833265856583da07e57ca09f9b2456986b1aed3ba3cc5940cab38d9d4f04964052b4857637c24513ffe6236da6d
|
data/lib/dt.rb
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
module DT
|
2
|
+
# ++
|
3
|
+
# construct html table from array
|
4
|
+
# ++
|
5
|
+
def self.array_to_list(array)
|
6
|
+
list_string_start = "<ul>"
|
7
|
+
list_string_content = String.new
|
8
|
+
array.each do |item|
|
9
|
+
list_string_content += "<li>" + item + "</li>"
|
10
|
+
end
|
11
|
+
list_string_end = "</ul>"
|
12
|
+
list_string_start + list_string_content + list_string_end
|
13
|
+
end
|
14
|
+
|
15
|
+
# ++
|
16
|
+
# construct html table from a hash with all the value should be array
|
17
|
+
# like, hash => {"name" => ["John", "Jim"], "Year" => ["1990", "1991"]}
|
18
|
+
# ++
|
19
|
+
def self.hash_array_to_table(hash_array)
|
20
|
+
table_head = "<table border=\"1\"><thead><tr>"
|
21
|
+
table_head_cell = ""
|
22
|
+
|
23
|
+
len = 0
|
24
|
+
key_num = hash_array.keys.length
|
25
|
+
|
26
|
+
hash_array.each_key do |key|
|
27
|
+
table_head_cell += "<th>" + key + "</th>"
|
28
|
+
new_len = hash_array["#{key}"].length
|
29
|
+
if new_len > len
|
30
|
+
len = new_len
|
31
|
+
end
|
32
|
+
end
|
33
|
+
table_head_cell += "</tr></thead>"
|
34
|
+
|
35
|
+
table_body = String.new
|
36
|
+
i = 0
|
37
|
+
table_body = "<tbody>"
|
38
|
+
while (i < len)
|
39
|
+
sub_head = "<tr>"
|
40
|
+
sub_body = ""
|
41
|
+
j = 0
|
42
|
+
key_num.times do
|
43
|
+
value = hash_array["#{hash_array.keys[j]}"][i]
|
44
|
+
sub_body += "<td>#{value}</td>" if value
|
45
|
+
sub_body += "<td></td>" if !value
|
46
|
+
j += 1
|
47
|
+
end
|
48
|
+
i += 1
|
49
|
+
sub_foot = "</tr>"
|
50
|
+
|
51
|
+
sub = sub_head + sub_body + sub_foot
|
52
|
+
table_body += sub
|
53
|
+
end
|
54
|
+
|
55
|
+
table_head + table_head_cell + table_body + "</tbody>"
|
56
|
+
end
|
57
|
+
|
58
|
+
# ++
|
59
|
+
# construct html table from array, but all the elements are same
|
60
|
+
# shape hash, array = [{"name" => "John", "year" => "1990"},{"name" => "Jim", "year" => "1991"}]
|
61
|
+
# ++
|
62
|
+
def self.array_hash_to_table(array_hash)
|
63
|
+
# all the element of array are same structure hash
|
64
|
+
#
|
65
|
+
table_head = "<table border=\"1\"><thead><tr>"
|
66
|
+
table_head_cell = String.new
|
67
|
+
array_hash[0].each_key do |key|
|
68
|
+
table_head_cell += "<th>" + key + "</th>"
|
69
|
+
end
|
70
|
+
table_head += table_head_cell + "</tr></thead>"
|
71
|
+
|
72
|
+
table_body = ""
|
73
|
+
table_cell = ""
|
74
|
+
array_hash.each do |hash|
|
75
|
+
table_cell += "<tr>"
|
76
|
+
hash.each do |key, value|
|
77
|
+
table_cell += "<td>#{value}</td>"
|
78
|
+
end
|
79
|
+
table_cell += "</tr>"
|
80
|
+
end
|
81
|
+
table_body = "<tbody>" + table_cell + "</tbody>"
|
82
|
+
|
83
|
+
table_head + table_body
|
84
|
+
end
|
85
|
+
end
|
data/test/tc_dt.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require_relative "../lib/dt.rb"
|
2
|
+
require "minitest/autorun"
|
3
|
+
|
4
|
+
class TestDT < MiniTest::Unit::TestCase
|
5
|
+
def test_array
|
6
|
+
assert_equal("<ul><li>a</li><li>b</li></ul>", DT::array_to_list(["a", "b"]))
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_hash_array
|
10
|
+
hash = {"name" => ["John", "Jim"], "year" => ["1988", "1989"]}
|
11
|
+
expect = "<table border=\"1\"><thead><tr><th>name</th><th>year</th></tr></thead><tbody><tr><td>John</td><td>1988</td></tr><tr><td>Jim</td><td>1989</td></tr></tbody>"
|
12
|
+
assert_equal(expect, DT::hash_array_to_table(hash))
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_array_hash
|
16
|
+
array = [{"name" => "John"},{"name" => "Jim"}]
|
17
|
+
result = "<table border=\"1\"><thead><tr><th>name</th></tr></thead><tbody><tr><td>John</td></tr><tr><td>Jim</td></tr></tbody>"
|
18
|
+
assert_equal(result, DT::array_hash_to_table(array))
|
19
|
+
end
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dt
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Minghe Huang
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-07-11 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: a simle lib help you transfer data struct (array, hash array, array hash)
|
14
|
+
to html table
|
15
|
+
email: h.mignhe@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/dt.rb
|
21
|
+
- test/tc_dt.rb
|
22
|
+
homepage: http://minghe.me
|
23
|
+
licenses:
|
24
|
+
- MIT
|
25
|
+
metadata: {}
|
26
|
+
post_install_message:
|
27
|
+
rdoc_options: []
|
28
|
+
require_paths:
|
29
|
+
- lib
|
30
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
requirements: []
|
41
|
+
rubyforge_project:
|
42
|
+
rubygems_version: 2.0.14
|
43
|
+
signing_key:
|
44
|
+
specification_version: 4
|
45
|
+
summary: data to table
|
46
|
+
test_files: []
|