tree_decorator 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +22 -0
- data/README.md +31 -0
- data/lib/example.rb +30 -0
- data/lib/tree_decorator/hanger.rb +45 -0
- data/lib/tree_decorator/object_hanger.rb +42 -0
- data/lib/tree_decorator.rb +6 -0
- metadata +51 -0
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Rob Nichols
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
TreeDecorator
|
2
|
+
=============
|
3
|
+
|
4
|
+
TreeDecorator has been designed to make it easier to decorate trees or nested
|
5
|
+
sets of data. The tools provided, walk through a hash or nested object and
|
6
|
+
apply code to containers and elements based on user defined rules.
|
7
|
+
|
8
|
+
For example:
|
9
|
+
|
10
|
+
class Thing < ActiveRecord::Base
|
11
|
+
acts_as_nested_set
|
12
|
+
|
13
|
+
def name
|
14
|
+
@name
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
hanger = TreeDecorator::ObjectHanger.new(
|
19
|
+
Thing.roots.first,
|
20
|
+
:children_method => :children,
|
21
|
+
:content_method => :name
|
22
|
+
)
|
23
|
+
|
24
|
+
hanger.outer {|content| "<ul>#{content}</ul>"}
|
25
|
+
hanger.inner {|content| "<li>#{content}</li>"}
|
26
|
+
hanger.element {|content| "<span class=\"thing\">#{content}</span>"}
|
27
|
+
|
28
|
+
hanger.tree #----> outputs a nested HTML unordered list with each thing's name
|
29
|
+
# in a span of class 'thing'.
|
30
|
+
|
31
|
+
See lib/example and tests for examples of usage
|
data/lib/example.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require_relative('tree_decorator')
|
2
|
+
|
3
|
+
tree = {
|
4
|
+
1 => {
|
5
|
+
2 => {
|
6
|
+
4 => {},
|
7
|
+
5 => {},
|
8
|
+
},
|
9
|
+
6 => {}
|
10
|
+
},
|
11
|
+
7 => {}
|
12
|
+
}
|
13
|
+
|
14
|
+
hanger = TreeDecorator::Hanger.new(tree)
|
15
|
+
|
16
|
+
hanger.outer {|content| "<ul>#{content}</ul>"}
|
17
|
+
hanger.inner {|content| "<li>#{content}<li>"}
|
18
|
+
|
19
|
+
puts unordered_list = hanger.tree
|
20
|
+
|
21
|
+
hanger = TreeDecorator::Hanger.new(tree)
|
22
|
+
hanger.element {|content| "element:#{content}"}
|
23
|
+
hanger.outer {|content| "<container>#{content}</container>"}
|
24
|
+
hanger.join_with(',')
|
25
|
+
|
26
|
+
puts containers = hanger.tree
|
27
|
+
|
28
|
+
|
29
|
+
|
30
|
+
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module TreeDecorator
|
2
|
+
|
3
|
+
# Walks through a nested hash and decorates each element and container
|
4
|
+
class Hanger
|
5
|
+
def initialize(tree)
|
6
|
+
@tree = tree
|
7
|
+
end
|
8
|
+
|
9
|
+
def outer(&containment)
|
10
|
+
@outer = containment
|
11
|
+
end
|
12
|
+
|
13
|
+
def inner(&elementizer)
|
14
|
+
@inner = elementizer
|
15
|
+
end
|
16
|
+
|
17
|
+
def element(&element)
|
18
|
+
@element = element
|
19
|
+
end
|
20
|
+
|
21
|
+
def join_with(text)
|
22
|
+
@join_with = text
|
23
|
+
end
|
24
|
+
|
25
|
+
def tree
|
26
|
+
output = packager(@tree)
|
27
|
+
@outer ? @outer.call(output) : output
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
def packager(hash)
|
32
|
+
output = []
|
33
|
+
hash.each do |parent, child|
|
34
|
+
parent = @element.call(parent) if @element
|
35
|
+
if child and !child.empty?
|
36
|
+
child = packager(child)
|
37
|
+
child = @outer.call(child) if @outer
|
38
|
+
parent = parent.to_s + child
|
39
|
+
end
|
40
|
+
output << (@inner ? @inner.call(parent) : parent)
|
41
|
+
end
|
42
|
+
return output.join(@join_with)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require_relative 'hanger'
|
2
|
+
|
3
|
+
module TreeDecorator
|
4
|
+
|
5
|
+
# Walks through objects that have containers with sub-objects
|
6
|
+
# and decorates each object and sub-object.
|
7
|
+
class ObjectHanger < Hanger
|
8
|
+
attr_reader :children_method, :content_method
|
9
|
+
|
10
|
+
def initialize(root, args = {})
|
11
|
+
@root = root
|
12
|
+
@children_method = args[:children_method]
|
13
|
+
@content_method = args[:content_method]
|
14
|
+
@tree = hash
|
15
|
+
end
|
16
|
+
|
17
|
+
def hash
|
18
|
+
@tree ||= get_hash_from_objects
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
def get_hash_from_objects
|
23
|
+
content = Hash.new
|
24
|
+
content[@root.send(content_method)] = populate_with_children(@root)
|
25
|
+
return content
|
26
|
+
end
|
27
|
+
|
28
|
+
def populate_with_children(object)
|
29
|
+
content = Hash.new
|
30
|
+
children = object.send(children_method)
|
31
|
+
|
32
|
+
if children and !children.empty?
|
33
|
+
children.each do |child|
|
34
|
+
content[child.send(content_method)] = populate_with_children(child)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
return content
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
metadata
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tree_decorator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Rob Nichols
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-28 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description: A tool that walks through a hash or nested object and applies code to
|
15
|
+
containers and elements based on user defined rules.
|
16
|
+
email: rob@undervale.co.uk
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- README.md
|
22
|
+
- LICENSE
|
23
|
+
- lib/tree_decorator.rb
|
24
|
+
- lib/tree_decorator/object_hanger.rb
|
25
|
+
- lib/tree_decorator/hanger.rb
|
26
|
+
- lib/example.rb
|
27
|
+
homepage: https://github.com/reggieb/TreeDecorator
|
28
|
+
licenses: []
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
requirements: []
|
46
|
+
rubyforge_project:
|
47
|
+
rubygems_version: 1.8.10
|
48
|
+
signing_key:
|
49
|
+
specification_version: 3
|
50
|
+
summary: Tree Decorator decorates trees or nested sets of data
|
51
|
+
test_files: []
|