rubytree 0.9.5pre6 → 0.9.5pre7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/API-CHANGES.rdoc +11 -11
- data/Gemfile.lock +36 -24
- data/History.rdoc +46 -28
- data/LICENSE.md +1 -1
- data/README.md +31 -21
- data/Rakefile +11 -11
- data/TODO.org +2 -0
- data/gem_graph.png +0 -0
- data/lib/rubytree.rb +13 -13
- data/lib/tree.rb +127 -75
- data/lib/tree/binarytree.rb +48 -31
- data/lib/tree/tree_deps.rb +15 -14
- data/lib/tree/utils/camel_case_method_handler.rb +16 -15
- data/lib/tree/utils/hash_converter.rb +35 -21
- data/lib/tree/utils/json_converter.rb +18 -16
- data/lib/tree/utils/metrics_methods.rb +33 -24
- data/lib/tree/utils/path_methods.rb +92 -0
- data/lib/tree/utils/tree_merge_handler.rb +34 -23
- data/lib/tree/utils/utils.rb +14 -14
- data/lib/tree/version.rb +13 -13
- data/rubytree.gemspec +88 -0
- data/setup.rb +1585 -0
- data/test/test_subclassed_node.rb +5 -0
- data/test/test_tree.rb +73 -0
- metadata +8 -4
data/lib/tree/binarytree.rb
CHANGED
@@ -8,19 +8,19 @@
|
|
8
8
|
# Author:: Anupam Sengupta (anupamsg@gmail.com)
|
9
9
|
#
|
10
10
|
|
11
|
-
# Copyright (c) 2007, 2008, 2009, 2010, 2012, 2013, 2014 Anupam Sengupta
|
11
|
+
# Copyright (c) 2007, 2008, 2009, 2010, 2012, 2013, 2014, 2015 Anupam Sengupta
|
12
12
|
#
|
13
13
|
# All rights reserved.
|
14
14
|
#
|
15
|
-
# Redistribution and use in source and binary forms, with or without
|
16
|
-
# are permitted provided that the following conditions are met:
|
15
|
+
# Redistribution and use in source and binary forms, with or without
|
16
|
+
# modification, are permitted provided that the following conditions are met:
|
17
17
|
#
|
18
18
|
# - Redistributions of source code must retain the above copyright notice, this
|
19
19
|
# list of conditions and the following disclaimer.
|
20
20
|
#
|
21
|
-
# - Redistributions in binary form must reproduce the above copyright notice,
|
22
|
-
# list of conditions and the following disclaimer in the documentation
|
23
|
-
# other materials provided with the distribution.
|
21
|
+
# - Redistributions in binary form must reproduce the above copyright notice,
|
22
|
+
# this list of conditions and the following disclaimer in the documentation
|
23
|
+
# and/or other materials provided with the distribution.
|
24
24
|
#
|
25
25
|
# - Neither the name of the organization nor the names of its contributors may
|
26
26
|
# be used to endorse or promote products derived from this software without
|
@@ -29,21 +29,22 @@
|
|
29
29
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
30
30
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
31
31
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
32
|
-
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
|
33
|
-
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
34
|
-
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
35
|
-
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
36
|
-
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
37
|
-
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
38
|
-
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
32
|
+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
|
33
|
+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
34
|
+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
35
|
+
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
36
|
+
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
37
|
+
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
38
|
+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
39
39
|
#
|
40
40
|
|
41
41
|
require 'tree'
|
42
42
|
|
43
43
|
module Tree
|
44
44
|
|
45
|
-
# Provides a Binary tree implementation. This node allows only two child nodes
|
46
|
-
# provides direct access to the left or right
|
45
|
+
# Provides a Binary tree implementation. This node allows only two child nodes
|
46
|
+
# (left and right child). It also provides direct access to the left or right
|
47
|
+
# child, including assignment to the same.
|
47
48
|
#
|
48
49
|
# This inherits from the {Tree::TreeNode} class.
|
49
50
|
#
|
@@ -64,11 +65,13 @@ module Tree
|
|
64
65
|
end
|
65
66
|
|
66
67
|
# @!attribute [rw] right_child
|
67
|
-
# Right child of the receiver node. Note that right child == last child
|
68
|
+
# Right child of the receiver node. Note that right child == last child
|
69
|
+
# unless there is only one child.
|
68
70
|
#
|
69
71
|
# Returns +nil+ if the right child does not exist.
|
70
72
|
#
|
71
|
-
# @return [Tree::BinaryTreeNode] The right child, or +nil+ if the right side
|
73
|
+
# @return [Tree::BinaryTreeNode] The right child, or +nil+ if the right side
|
74
|
+
# child does not exist.
|
72
75
|
#
|
73
76
|
# @see #left_child
|
74
77
|
def right_child
|
@@ -97,16 +100,19 @@ module Tree
|
|
97
100
|
|
98
101
|
# @!group Structure Modification
|
99
102
|
|
100
|
-
# Adds the specified child node to the receiver node.
|
103
|
+
# Adds the specified child node to the receiver node. The child node's
|
104
|
+
# parent is set to be the receiver.
|
101
105
|
#
|
102
|
-
# The child nodes are added in the order of addition, i.e., the first child
|
103
|
-
# second child added will be the second
|
106
|
+
# The child nodes are added in the order of addition, i.e., the first child
|
107
|
+
# added becomes the left node, and the second child added will be the second
|
108
|
+
# node.
|
104
109
|
#
|
105
110
|
# If only one child is present, then this will be the left child.
|
106
111
|
#
|
107
112
|
# @param [Tree::BinaryTreeNode] child The child to add.
|
108
113
|
#
|
109
|
-
# @raise [ArgumentError] This exception is raised if two children are
|
114
|
+
# @raise [ArgumentError] This exception is raised if two children are
|
115
|
+
# already present.
|
110
116
|
def add(child)
|
111
117
|
raise ArgumentError, "Already has two child nodes" if @children.size == 2
|
112
118
|
|
@@ -137,11 +143,15 @@ module Tree
|
|
137
143
|
# root.add_from_hash({:B => {:D => {}}, [:C, "C content!"] => {}})
|
138
144
|
#
|
139
145
|
# @param [Hash] hashed_subtree The hash of child subtrees.
|
140
|
-
#
|
146
|
+
#
|
147
|
+
# @raise [ArgumentError] This exception is raised if hash contains too
|
148
|
+
# many children.
|
149
|
+
# =>
|
141
150
|
# @raise [ArgumentError] This exception is raised if a non-hash is passed.
|
142
151
|
# @return [Array] Array of child nodes added
|
143
152
|
def add_from_hash(hashed_subtree)
|
144
|
-
raise ArgumentError, "Too many children"
|
153
|
+
raise ArgumentError, "Too many children"\
|
154
|
+
if hashed_subtree.size + @children.size > 2
|
145
155
|
super(hashed_subtree)
|
146
156
|
end
|
147
157
|
|
@@ -179,17 +189,20 @@ module Tree
|
|
179
189
|
|
180
190
|
end
|
181
191
|
|
182
|
-
# A protected method to allow insertion of child nodes at the specified
|
183
|
-
# Note that this method allows insertion of +nil+ nodes.
|
192
|
+
# A protected method to allow insertion of child nodes at the specified
|
193
|
+
# location. Note that this method allows insertion of +nil+ nodes.
|
194
|
+
#
|
195
|
+
# @param [Tree::BinaryTreeNode] child The child to add at the specified
|
196
|
+
# location.
|
184
197
|
#
|
185
|
-
# @param [Tree::BinaryTreeNode] child The child to add at the specified location.
|
186
198
|
# @param [Integer] at_index The location to add the entry at (0 or 1).
|
187
199
|
#
|
188
200
|
# @return [Tree::BinaryTreeNode] The added child.
|
189
201
|
#
|
190
202
|
# @raise [ArgumentError] If the index is out of limits.
|
191
203
|
def set_child_at(child, at_index)
|
192
|
-
raise ArgumentError "A binary tree cannot have more than two children."
|
204
|
+
raise ArgumentError "A binary tree cannot have more than two children."\
|
205
|
+
unless (0..1).include? at_index
|
193
206
|
|
194
207
|
@children[at_index] = child
|
195
208
|
@children_hash[child.name] = child if child # Assign the name mapping
|
@@ -199,9 +212,11 @@ module Tree
|
|
199
212
|
|
200
213
|
protected :set_child_at
|
201
214
|
|
202
|
-
# Sets the left child of the receiver node. If a previous child existed, it
|
215
|
+
# Sets the left child of the receiver node. If a previous child existed, it
|
216
|
+
# is replaced.
|
203
217
|
#
|
204
|
-
# @param [Tree::BinaryTreeNode] child The child to add as the left-side
|
218
|
+
# @param [Tree::BinaryTreeNode] child The child to add as the left-side
|
219
|
+
# node.
|
205
220
|
#
|
206
221
|
# @return [Tree::BinaryTreeNode] The assigned child node.
|
207
222
|
#
|
@@ -211,9 +226,11 @@ module Tree
|
|
211
226
|
set_child_at child, 0
|
212
227
|
end
|
213
228
|
|
214
|
-
# Sets the right child of the receiver node. If a previous child existed, it
|
229
|
+
# Sets the right child of the receiver node. If a previous child existed, it
|
230
|
+
# is replaced.
|
215
231
|
#
|
216
|
-
# @param [Tree::BinaryTreeNode] child The child to add as the right-side
|
232
|
+
# @param [Tree::BinaryTreeNode] child The child to add as the right-side
|
233
|
+
# node.
|
217
234
|
#
|
218
235
|
# @return [Tree::BinaryTreeNode] The assigned child node.
|
219
236
|
#
|
data/lib/tree/tree_deps.rb
CHANGED
@@ -7,19 +7,19 @@
|
|
7
7
|
# Author:: Anupam Sengupta (anupamsg@gmail.com)
|
8
8
|
#
|
9
9
|
|
10
|
-
# Copyright (c) 2006
|
10
|
+
# Copyright (c) 2006-2015 Anupam Sengupta
|
11
11
|
#
|
12
12
|
# All rights reserved.
|
13
13
|
#
|
14
|
-
# Redistribution and use in source and binary forms, with or without
|
15
|
-
# are permitted provided that the following conditions are met:
|
14
|
+
# Redistribution and use in source and binary forms, with or without
|
15
|
+
# modification, are permitted provided that the following conditions are met:
|
16
16
|
#
|
17
17
|
# - Redistributions of source code must retain the above copyright notice, this
|
18
18
|
# list of conditions and the following disclaimer.
|
19
19
|
#
|
20
|
-
# - Redistributions in binary form must reproduce the above copyright notice,
|
21
|
-
# list of conditions and the following disclaimer in the documentation
|
22
|
-
# other materials provided with the distribution.
|
20
|
+
# - Redistributions in binary form must reproduce the above copyright notice,
|
21
|
+
# this list of conditions and the following disclaimer in the documentation
|
22
|
+
# and/or other materials provided with the distribution.
|
23
23
|
#
|
24
24
|
# - Neither the name of the organization nor the names of its contributors may
|
25
25
|
# be used to endorse or promote products derived from this software without
|
@@ -28,13 +28,13 @@
|
|
28
28
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
29
29
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
30
30
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
31
|
-
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
|
32
|
-
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
33
|
-
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
34
|
-
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
35
|
-
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
36
|
-
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
37
|
-
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
31
|
+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
|
32
|
+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
33
|
+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
34
|
+
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
35
|
+
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
36
|
+
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
37
|
+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
38
38
|
#
|
39
39
|
|
40
40
|
require 'structured_warnings'
|
@@ -42,7 +42,8 @@ require 'json'
|
|
42
42
|
|
43
43
|
require 'tree/version'
|
44
44
|
require 'tree/utils/metrics_methods'
|
45
|
+
require 'tree/utils/path_methods'
|
45
46
|
require 'tree/utils/camel_case_method_handler'
|
46
47
|
require 'tree/utils/json_converter'
|
47
48
|
require 'tree/utils/tree_merge_handler'
|
48
|
-
require 'tree/utils/hash_converter'
|
49
|
+
require 'tree/utils/hash_converter'
|
@@ -4,21 +4,21 @@
|
|
4
4
|
#
|
5
5
|
# Author:: Anupam Sengupta (anupamsg@gmail.com)
|
6
6
|
#
|
7
|
-
# Time-stamp: <
|
7
|
+
# Time-stamp: <2015-05-30 14:14:09 anupam>
|
8
8
|
#
|
9
|
-
# Copyright (C) 2012, 2013 Anupam Sengupta <anupamsg@gmail.com>
|
9
|
+
# Copyright (C) 2012, 2013, 2015 Anupam Sengupta <anupamsg@gmail.com>
|
10
10
|
#
|
11
11
|
# All rights reserved.
|
12
12
|
#
|
13
|
-
# Redistribution and use in source and binary forms, with or without
|
14
|
-
# are permitted provided that the following conditions are met:
|
13
|
+
# Redistribution and use in source and binary forms, with or without
|
14
|
+
# modification, are permitted provided that the following conditions are met:
|
15
15
|
#
|
16
16
|
# - Redistributions of source code must retain the above copyright notice, this
|
17
17
|
# list of conditions and the following disclaimer.
|
18
18
|
#
|
19
|
-
# - Redistributions in binary form must reproduce the above copyright notice,
|
20
|
-
# list of conditions and the following disclaimer in the documentation
|
21
|
-
# other materials provided with the distribution.
|
19
|
+
# - Redistributions in binary form must reproduce the above copyright notice,
|
20
|
+
# this list of conditions and the following disclaimer in the documentation
|
21
|
+
# and/or other materials provided with the distribution.
|
22
22
|
#
|
23
23
|
# - Neither the name of the organization nor the names of its contributors may
|
24
24
|
# be used to endorse or promote products derived from this software without
|
@@ -27,13 +27,13 @@
|
|
27
27
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
28
28
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
29
29
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
30
|
-
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
|
31
|
-
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
32
|
-
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
33
|
-
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
34
|
-
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
35
|
-
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
36
|
-
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
30
|
+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
|
31
|
+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
32
|
+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
33
|
+
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
34
|
+
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
35
|
+
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
36
|
+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
37
37
|
#
|
38
38
|
|
39
39
|
require 'structured_warnings'
|
@@ -49,7 +49,8 @@ module Tree::Utils
|
|
49
49
|
def method_missing(meth, *args, &blk)
|
50
50
|
if self.respond_to?(new_method_name = to_snake_case(meth))
|
51
51
|
warn DeprecatedMethodWarning,
|
52
|
-
|
52
|
+
"The camelCased methods are deprecated. "\
|
53
|
+
"Please use #{new_method_name} instead of #{meth}"
|
53
54
|
return send(new_method_name, *args, &blk)
|
54
55
|
else
|
55
56
|
super
|
@@ -5,22 +5,22 @@
|
|
5
5
|
#
|
6
6
|
# Author:: Jen Hamon (http://www.github.com/jhamon)
|
7
7
|
#
|
8
|
-
# Time-stamp: <2015-
|
8
|
+
# Time-stamp: <2015-05-30 14:19:16 anupam>
|
9
9
|
#
|
10
10
|
# Copyright (C) 2014, 2015 Jen Hamon (http://www.github.com/jhamon) and
|
11
11
|
# Anupam Sengupta <anupamsg@gmail.com>
|
12
12
|
#
|
13
13
|
# All rights reserved.
|
14
14
|
#
|
15
|
-
# Redistribution and use in source and binary forms, with or without
|
16
|
-
# are permitted provided that the following conditions are met:
|
15
|
+
# Redistribution and use in source and binary forms, with or without
|
16
|
+
# modification, are permitted provided that the following conditions are met:
|
17
17
|
#
|
18
18
|
# - Redistributions of source code must retain the above copyright notice, this
|
19
19
|
# list of conditions and the following disclaimer.
|
20
20
|
#
|
21
|
-
# - Redistributions in binary form must reproduce the above copyright notice,
|
22
|
-
# list of conditions and the following disclaimer in the documentation
|
23
|
-
# other materials provided with the distribution.
|
21
|
+
# - Redistributions in binary form must reproduce the above copyright notice,
|
22
|
+
# this list of conditions and the following disclaimer in the documentation
|
23
|
+
# and/or other materials provided with the distribution.
|
24
24
|
#
|
25
25
|
# - Neither the name of the organization nor the names of its contributors may
|
26
26
|
# be used to endorse or promote products derived from this software without
|
@@ -29,13 +29,13 @@
|
|
29
29
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
30
30
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
31
31
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
32
|
-
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
|
33
|
-
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
34
|
-
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
35
|
-
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
36
|
-
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
37
|
-
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
38
|
-
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
32
|
+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
|
33
|
+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
34
|
+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
35
|
+
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
36
|
+
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
37
|
+
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
38
|
+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
39
39
|
|
40
40
|
module Tree::Utils::HashConverter
|
41
41
|
|
@@ -73,19 +73,30 @@ module Tree::Utils::HashConverter
|
|
73
73
|
#
|
74
74
|
# # A tree with equivalent structure but with content present for
|
75
75
|
# # nodes A and D could be built from a hash like this:
|
76
|
-
# {[:A, "A content"] => {:B => {},
|
76
|
+
# {[:A, "A content"] => {:B => {},
|
77
|
+
# :C => { [:D, "D content"] => {},
|
78
|
+
# :E => {} }}}
|
77
79
|
#
|
78
80
|
# @author Jen Hamon (http://www.github.com/jhamon)
|
79
81
|
# @param [Hash] hash Hash to build tree from.
|
80
|
-
#
|
82
|
+
#
|
83
|
+
# @return [Tree::TreeNode] The {Tree::TreeNode} instance representing the
|
84
|
+
# root of your tree.
|
81
85
|
#
|
82
86
|
# @raise [ArgumentError] This exception is raised if a non-Hash is passed.
|
83
|
-
#
|
84
|
-
# @raise [ArgumentError] This exception is raised if the hash
|
87
|
+
#
|
88
|
+
# @raise [ArgumentError] This exception is raised if the hash has multiple
|
89
|
+
# top-level elements.
|
90
|
+
#
|
91
|
+
# @raise [ArgumentError] This exception is raised if the hash contains
|
92
|
+
# values that are not hashes or nils.
|
85
93
|
|
86
94
|
def from_hash(hash)
|
87
|
-
raise ArgumentError, "Argument must be a type of hash"
|
88
|
-
|
95
|
+
raise ArgumentError, "Argument must be a type of hash"\
|
96
|
+
unless hash.is_a?(Hash)
|
97
|
+
|
98
|
+
raise ArgumentError, "Hash must have one top-level element"\
|
99
|
+
if hash.size != 1
|
89
100
|
|
90
101
|
root, children = hash.first
|
91
102
|
|
@@ -127,7 +138,8 @@ module Tree::Utils::HashConverter
|
|
127
138
|
# @return [Array] Array of child nodes added
|
128
139
|
# @see ClassMethods#from_hash
|
129
140
|
def add_from_hash(children)
|
130
|
-
raise ArgumentError, "Argument must be a type of hash"
|
141
|
+
raise ArgumentError, "Argument must be a type of hash"\
|
142
|
+
unless children.is_a?(Hash)
|
131
143
|
|
132
144
|
child_nodes = []
|
133
145
|
children.each do |child, grandchildren|
|
@@ -145,7 +157,9 @@ module Tree::Utils::HashConverter
|
|
145
157
|
# root = Tree::TreeNode.new(:root, "root content")
|
146
158
|
# root << Tree::TreeNode.new(:child1, "child1 content")
|
147
159
|
# root << Tree::TreeNode.new(:child2, "child2 content")
|
148
|
-
# root.to_h # => {[:root, "root content"] =>
|
160
|
+
# root.to_h # => {[:root, "root content"] =>
|
161
|
+
# { [:child1, "child1 content"] =>
|
162
|
+
# {}, [:child2, "child2 content"] => {}}}
|
149
163
|
# @author Jen Hamon (http://www.github.com/jhamon)
|
150
164
|
# @return [Hash] Hash representation of tree.
|
151
165
|
def to_h
|
@@ -4,21 +4,21 @@
|
|
4
4
|
#
|
5
5
|
# Author:: Anupam Sengupta (anupamsg@gmail.com)
|
6
6
|
#
|
7
|
-
# Time-stamp: <
|
7
|
+
# Time-stamp: <2015-05-30 14:20:20 anupam>
|
8
8
|
#
|
9
|
-
# Copyright (C) 2012, 2013, 2014 Anupam Sengupta <anupamsg@gmail.com>
|
9
|
+
# Copyright (C) 2012, 2013, 2014, 2015 Anupam Sengupta <anupamsg@gmail.com>
|
10
10
|
#
|
11
11
|
# All rights reserved.
|
12
12
|
#
|
13
|
-
# Redistribution and use in source and binary forms, with or without
|
14
|
-
# are permitted provided that the following conditions are met:
|
13
|
+
# Redistribution and use in source and binary forms, with or without
|
14
|
+
# modification, are permitted provided that the following conditions are met:
|
15
15
|
#
|
16
16
|
# - Redistributions of source code must retain the above copyright notice, this
|
17
17
|
# list of conditions and the following disclaimer.
|
18
18
|
#
|
19
|
-
# - Redistributions in binary form must reproduce the above copyright notice,
|
20
|
-
# list of conditions and the following disclaimer in the documentation
|
21
|
-
# other materials provided with the distribution.
|
19
|
+
# - Redistributions in binary form must reproduce the above copyright notice,
|
20
|
+
# this list of conditions and the following disclaimer in the documentation
|
21
|
+
# and/or other materials provided with the distribution.
|
22
22
|
#
|
23
23
|
# - Neither the name of the organization nor the names of its contributors may
|
24
24
|
# be used to endorse or promote products derived from this software without
|
@@ -27,13 +27,13 @@
|
|
27
27
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
28
28
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
29
29
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
30
|
-
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
|
31
|
-
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
32
|
-
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
33
|
-
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
34
|
-
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
35
|
-
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
36
|
-
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
30
|
+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
|
31
|
+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
32
|
+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
33
|
+
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
34
|
+
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
35
|
+
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
36
|
+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
37
37
|
|
38
38
|
require 'json'
|
39
39
|
|
@@ -54,7 +54,8 @@ module Tree::Utils::JSONConverter
|
|
54
54
|
#
|
55
55
|
# @return A hash based representation of the JSON
|
56
56
|
#
|
57
|
-
# Rails uses JSON in ActiveSupport, and all Rails JSON encoding goes through
|
57
|
+
# Rails uses JSON in ActiveSupport, and all Rails JSON encoding goes through
|
58
|
+
# +as_json+.
|
58
59
|
#
|
59
60
|
# @see #to_json
|
60
61
|
# @see http://stackoverflow.com/a/6880638/273808
|
@@ -90,7 +91,8 @@ module Tree::Utils::JSONConverter
|
|
90
91
|
as_json.to_json(*a)
|
91
92
|
end
|
92
93
|
|
93
|
-
# ClassMethods for the {JSONConverter} module.
|
94
|
+
# ClassMethods for the {JSONConverter} module. Will become class methods in
|
95
|
+
# the +include+ target.
|
94
96
|
module ClassMethods
|
95
97
|
# Helper method to create a Tree::TreeNode instance from the JSON hash
|
96
98
|
# representation. Note that this method should *NOT* be called directly.
|