data-converter 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +23 -0
- data/data-converter.gemspec +17 -0
- data/lib/data-converter.rb +64 -0
- data/test/test.rb +59 -0
- metadata +69 -0
data/README.rdoc
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
=data-converter
|
2
|
+
|
3
|
+
==Install
|
4
|
+
gem install data-converter
|
5
|
+
|
6
|
+
==Description
|
7
|
+
Hash and Array convert to HTML Element (Array => ul, Hash => dl).
|
8
|
+
It's under the development.
|
9
|
+
|
10
|
+
==Code
|
11
|
+
require 'rubygems'
|
12
|
+
require 'data-converter'
|
13
|
+
|
14
|
+
puts DataConverter.parse({
|
15
|
+
:a => "b",
|
16
|
+
:c => { :d => [0, 1, 2] }
|
17
|
+
})
|
18
|
+
|
19
|
+
==License
|
20
|
+
the MIT License
|
21
|
+
|
22
|
+
==Author
|
23
|
+
namusyaka
|
@@ -0,0 +1,17 @@
|
|
1
|
+
Gem::Specification.new do | spec |
|
2
|
+
spec.name = "data-converter"
|
3
|
+
spec.version = "0.1"
|
4
|
+
spec.summary = "Ruby Data convert to HTML. It is under the devlopment."
|
5
|
+
spec.description = "Hash and Array convert to HTML Element (Array => ul, Hash => dl)."
|
6
|
+
spec.homepage = "https://github.com/namusyaka/data-converter"
|
7
|
+
spec.author = "namusyaka"
|
8
|
+
spec.email = "namusyaka@gmail.com"
|
9
|
+
spec.files = %w(
|
10
|
+
data-converter.gemspec
|
11
|
+
README.rdoc
|
12
|
+
lib
|
13
|
+
test
|
14
|
+
lib/data-converter.rb
|
15
|
+
test/test.rb
|
16
|
+
)
|
17
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
class DataConverter
|
2
|
+
attr_reader :data
|
3
|
+
|
4
|
+
ARRAY = "Array"
|
5
|
+
HASH = "Hash"
|
6
|
+
|
7
|
+
def initialize(data)
|
8
|
+
@data = data
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.parse(data)
|
12
|
+
constructor = self.new(data)
|
13
|
+
constructor.convert(constructor.data)
|
14
|
+
end
|
15
|
+
|
16
|
+
def convert(data = @data)
|
17
|
+
case data.class.to_s
|
18
|
+
when ARRAY
|
19
|
+
list = <<-UL
|
20
|
+
<ul>
|
21
|
+
#{ convert_to_li(data).join("\n") }
|
22
|
+
</ul>
|
23
|
+
UL
|
24
|
+
when HASH
|
25
|
+
list = <<-DL
|
26
|
+
<dl>
|
27
|
+
#{ convert_to_dl_childs(data).join("\n") }
|
28
|
+
</dl>
|
29
|
+
DL
|
30
|
+
else
|
31
|
+
return data
|
32
|
+
end
|
33
|
+
list
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def check_data?(data)
|
39
|
+
data.is_a?(Array) or data.is_a?(Hash)
|
40
|
+
end
|
41
|
+
|
42
|
+
def convert_to_li(data)
|
43
|
+
data.map! do | e |
|
44
|
+
if check_data?(e)
|
45
|
+
"<li>#{ convert(e) }</li>"
|
46
|
+
else
|
47
|
+
"<li>#{ e }</li>"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def convert_to_dl_childs(data)
|
53
|
+
childs = []
|
54
|
+
data.each_pair do | key, value |
|
55
|
+
childs << "<dt>#{ key }</dt>"
|
56
|
+
if check_data?(value)
|
57
|
+
childs << "<dd>#{ convert(value) }</dd>"
|
58
|
+
else
|
59
|
+
childs << "<dd>#{ value }</dd>"
|
60
|
+
end
|
61
|
+
end
|
62
|
+
childs
|
63
|
+
end
|
64
|
+
end
|
data/test/test.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require File.expand_path('../../lib/data-converter', __FILE__)
|
3
|
+
|
4
|
+
class Test_DataConverter < Test::Unit::TestCase
|
5
|
+
HASH_RESULT = <<-EOS
|
6
|
+
<dl>
|
7
|
+
<dt>a</dt>
|
8
|
+
<dd><dl>
|
9
|
+
<dt>b</dt>
|
10
|
+
<dd><dl>
|
11
|
+
<dt>c</dt>
|
12
|
+
<dd><ul>
|
13
|
+
<li>0</li>
|
14
|
+
<li>1</li>
|
15
|
+
<li>2</li>
|
16
|
+
<li>3</li>
|
17
|
+
</ul>
|
18
|
+
</dd>
|
19
|
+
</dl>
|
20
|
+
</dd>
|
21
|
+
</dl>
|
22
|
+
</dd>
|
23
|
+
</dl>
|
24
|
+
EOS
|
25
|
+
|
26
|
+
ARRAY_RESULT = <<-EOS
|
27
|
+
<ul>
|
28
|
+
<li><dl>
|
29
|
+
<dt>f</dt>
|
30
|
+
<dd><ul>
|
31
|
+
<li>4</li>
|
32
|
+
<li>5</li>
|
33
|
+
<li>6</li>
|
34
|
+
<li>7</li>
|
35
|
+
</ul>
|
36
|
+
</dd>
|
37
|
+
</dl>
|
38
|
+
</li>
|
39
|
+
</ul>
|
40
|
+
EOS
|
41
|
+
|
42
|
+
def setup
|
43
|
+
@hash_data = { :a => { :b => { :c => [0, 1, 2, 3] } } }
|
44
|
+
@array_data = [{ :f => [4, 5, 6, 7] }]
|
45
|
+
@class = DataConverter
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_array_parse
|
49
|
+
assert_equal(ARRAY_RESULT, @class.parse(@array_data))
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_hash_parse
|
53
|
+
assert_equal(HASH_RESULT, @class.parse(@hash_data))
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_other_approach
|
57
|
+
assert_equal(HASH_RESULT, DataConverter.new(@hash_data).convert)
|
58
|
+
end
|
59
|
+
end
|
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: data-converter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 9
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: "0.1"
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- namusyaka
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-11-12 00:00:00 +09:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Hash and Array convert to HTML Element (Array => ul, Hash => dl).
|
22
|
+
email: namusyaka@gmail.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files: []
|
28
|
+
|
29
|
+
files:
|
30
|
+
- data-converter.gemspec
|
31
|
+
- README.rdoc
|
32
|
+
- lib/data-converter.rb
|
33
|
+
- test/test.rb
|
34
|
+
has_rdoc: true
|
35
|
+
homepage: https://github.com/namusyaka/data-converter
|
36
|
+
licenses: []
|
37
|
+
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
hash: 3
|
49
|
+
segments:
|
50
|
+
- 0
|
51
|
+
version: "0"
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
hash: 3
|
58
|
+
segments:
|
59
|
+
- 0
|
60
|
+
version: "0"
|
61
|
+
requirements: []
|
62
|
+
|
63
|
+
rubyforge_project:
|
64
|
+
rubygems_version: 1.5.0
|
65
|
+
signing_key:
|
66
|
+
specification_version: 3
|
67
|
+
summary: Ruby Data convert to HTML. It is under the devlopment.
|
68
|
+
test_files: []
|
69
|
+
|