rubytree 1.0.2 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,9 +4,9 @@
4
4
  #
5
5
  # Author:: Anupam Sengupta (anupamsg@gmail.com)
6
6
  #
7
- # Time-stamp: <2015-05-30 14:20:20 anupam>
7
+ # Time-stamp: <2022-06-20 22:16:46 anupam>
8
8
  #
9
- # Copyright (C) 2012, 2013, 2014, 2015 Anupam Sengupta <anupamsg@gmail.com>
9
+ # Copyright (C) 2012, 2013, 2014, 2015, 2022 Anupam Sengupta <anupamsg@gmail.com>
10
10
  #
11
11
  # All rights reserved.
12
12
  #
@@ -34,93 +34,96 @@
34
34
  # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
35
35
  # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36
36
  # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37
+ #
38
+ # frozen_string_literal: true
37
39
 
38
- require_relative '../utils/utils'
39
40
  require 'json'
40
41
 
41
- # Provides utility methods to convert a {Tree::TreeNode} to and from
42
- # JSON[http://flori.github.com/json/].
43
- module Tree::Utils::JSONConverter
44
- def self.included(base)
45
- base.extend(ClassMethods)
46
- end
42
+ module Tree
43
+ module Utils
44
+ # Provides utility methods to convert a {Tree::TreeNode} to and from
45
+ # JSON[http://flori.github.com/json/].
46
+ module JSONConverter
47
+ def self.included(base)
48
+ base.extend(ClassMethods)
49
+ end
47
50
 
48
- # @!group Converting to/from JSON
51
+ # @!group Converting to/from JSON
49
52
 
50
- # Creates a JSON ready Hash for the #to_json method.
51
- #
52
- # @author Eric Cline (https://github.com/escline)
53
- # @since 0.8.3
54
- #
55
- # @return A hash based representation of the JSON
56
- #
57
- # Rails uses JSON in ActiveSupport, and all Rails JSON encoding goes through
58
- # +as_json+.
59
- #
60
- # @param [Object] options
61
- #
62
- # @see #to_json
63
- # @see http://stackoverflow.com/a/6880638/273808
64
- # noinspection RubyUnusedLocalVariable
65
- def as_json(_options = {})
66
- json_hash = {
67
- name: name,
68
- content: content,
69
- JSON.create_id => self.class.name
70
- }
53
+ # Creates a JSON ready Hash for the #to_json method.
54
+ #
55
+ # @author Eric Cline (https://github.com/escline)
56
+ # @since 0.8.3
57
+ #
58
+ # @return A hash based representation of the JSON
59
+ #
60
+ # Rails uses JSON in ActiveSupport, and all Rails JSON encoding goes through
61
+ # +as_json+.
62
+ #
63
+ # @param [Object] _options
64
+ #
65
+ # @see #to_json
66
+ # @see http://stackoverflow.com/a/6880638/273808
67
+ # noinspection RubyUnusedLocalVariable
68
+ def as_json(_options = {})
69
+ json_hash = {
70
+ name: name,
71
+ content: content,
72
+ JSON.create_id => self.class.name
73
+ }
71
74
 
72
- json_hash['children'] = children if has_children?
75
+ json_hash['children'] = children if children?
73
76
 
74
- json_hash
75
- end
77
+ json_hash
78
+ end
76
79
 
77
- # Creates a JSON representation of this node including all it's children.
78
- # This requires the JSON gem to be available, or else the operation fails with
79
- # a warning message. Uses the Hash output of #as_json method.
80
- #
81
- # @author Dirk Breuer (http://github.com/railsbros-dirk)
82
- # @since 0.7.0
83
- #
84
- # @return The JSON representation of this subtree.
85
- #
86
- # @see ClassMethods#json_create
87
- # @see #as_json
88
- # @see http://flori.github.com/json
89
- def to_json(*a)
90
- as_json.to_json(*a)
91
- end
80
+ # Creates a JSON representation of this node including all it's children.
81
+ # This requires the JSON gem to be available, or else the operation fails with
82
+ # a warning message. Uses the Hash output of #as_json method.
83
+ #
84
+ # @author Dirk Breuer (http://github.com/railsbros-dirk)
85
+ # @since 0.7.0
86
+ #
87
+ # @return The JSON representation of this subtree.
88
+ #
89
+ # @see ClassMethods#json_create
90
+ # @see #as_json
91
+ # @see http://flori.github.com/json
92
+ def to_json(*args)
93
+ as_json.to_json(*args)
94
+ end
92
95
 
93
- # ClassMethods for the {JSONConverter} module. Will become class methods in
94
- # the +include+ target.
95
- module ClassMethods
96
- # Helper method to create a Tree::TreeNode instance from the JSON hash
97
- # representation. Note that this method should *NOT* be called directly.
98
- # Instead, to convert the JSON hash back to a tree, do:
99
- #
100
- # tree = JSON.parse(the_json_hash)
101
- #
102
- # This operation requires the {JSON gem}[http://flori.github.com/json/] to
103
- # be available, or else the operation fails with a warning message.
104
- #
105
- # @author Dirk Breuer (http://github.com/railsbros-dirk)
106
- # @since 0.7.0
107
- #
108
- # @param [Hash] json_hash The JSON hash to convert from.
109
- #
110
- # @return [Tree::TreeNode] The created tree.
111
- #
112
- # @see #to_json
113
- # @see http://flori.github.com/json
114
- def json_create(json_hash)
115
- node = new(json_hash['name'], json_hash['content'])
96
+ # ClassMethods for the {JSONConverter} module. Will become class methods in
97
+ # the +include+ target.
98
+ module ClassMethods
99
+ # Helper method to create a Tree::TreeNode instance from the JSON hash
100
+ # representation. Note that this method should *NOT* be called directly.
101
+ # Instead, to convert the JSON hash back to a tree, do:
102
+ #
103
+ # tree = JSON.parse(the_json_hash)
104
+ #
105
+ # This operation requires the {JSON gem}[http://flori.github.com/json/] to
106
+ # be available, or else the operation fails with a warning message.
107
+ #
108
+ # @author Dirk Breuer (http://github.com/railsbros-dirk)
109
+ # @since 0.7.0
110
+ #
111
+ # @param [Hash] json_hash The JSON hash to convert from.
112
+ #
113
+ # @return [Tree::TreeNode] The created tree.
114
+ #
115
+ # @see #to_json
116
+ # @see http://flori.github.com/json
117
+ def json_create(json_hash)
118
+ node = new(json_hash['name'], json_hash['content'])
116
119
 
117
- if json_hash['children']
118
- json_hash['children'].each do |child|
119
- node << child
120
+ json_hash['children']&.each do |child|
121
+ node << child
122
+ end
123
+
124
+ node
120
125
  end
121
126
  end
122
-
123
- node
124
127
  end
125
128
  end
126
129
  end
@@ -4,9 +4,9 @@
4
4
  #
5
5
  # Author:: Anupam Sengupta (anupamsg@gmail.com)
6
6
  #
7
- # Time-stamp: <2021-12-29 13:01:54 anupam>
7
+ # Time-stamp: <2022-06-20 22:17:00 anupam>
8
8
  #
9
- # Copyright (C) 2013, 2015, 2017, 2021 Anupam Sengupta <anupamsg@gmail.com>
9
+ # Copyright (C) 2013, 2015, 2017, 2021, 2022 Anupam Sengupta <anupamsg@gmail.com>
10
10
  #
11
11
  # All rights reserved.
12
12
  #
@@ -35,14 +35,12 @@
35
35
  # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36
36
  # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37
37
  #
38
+ # frozen_string_literal: true
38
39
 
39
- require 'structured_warnings'
40
-
41
- module Tree::Utils
42
- # Provides utility functions to measure various tree metrics.
43
- module TreeMetricsHandler
44
- # noinspection RubyUnusedLocalVariable
45
- def self.included(_base)
40
+ module Tree
41
+ module Utils
42
+ # Provides utility functions to measure various tree metrics.
43
+ module TreeMetricsHandler
46
44
  # @!group Metrics and Measures
47
45
 
48
46
  # @!attribute [r] size
@@ -79,9 +77,9 @@ module Tree::Utils
79
77
  #
80
78
  # @return [Integer] Height of the node.
81
79
  def node_height
82
- return 0 if is_leaf?
80
+ return 0 if leaf?
83
81
 
84
- 1 + @children.collect { |child| child.node_height }.max
82
+ 1 + @children.collect(&:node_height).max
85
83
  end
86
84
 
87
85
  # @!attribute [r] node_depth
@@ -90,15 +88,11 @@ module Tree::Utils
90
88
  # Depth:: Length of the node's path to its root. Depth of a root node is
91
89
  # zero.
92
90
  #
93
- # *Note* that the deprecated method {#depth} was incorrectly computing
94
- # this value. Please replace all calls to the old method with
95
- # {#node_depth} instead.
96
- #
97
91
  # {#level} is an alias for this method.
98
92
  #
99
93
  # @return [Integer] Depth of this node.
100
94
  def node_depth
101
- return 0 if is_root?
95
+ return 0 if root?
102
96
 
103
97
  1 + parent.node_depth
104
98
  end
@@ -111,33 +105,6 @@ module Tree::Utils
111
105
  node_depth
112
106
  end
113
107
 
114
- # @!attribute [r] depth
115
- # Depth of the tree from this node. A single leaf node has a depth of 1.
116
- #
117
- # This method is *DEPRECATED* and may be removed in the subsequent
118
- # releases. Note that the value returned by this method is actually the:
119
- #
120
- # _height_ + 1 of the node, *NOT* the _depth_.
121
- #
122
- # For correct and conventional behavior, please use {#node_depth} and
123
- # {#node_height} methods instead.
124
- #
125
- # @return [Integer] depth of the node.
126
- #
127
- # @deprecated This method returns an incorrect value. Use the
128
- # {#node_depth} method instead.
129
- #
130
- # @see #node_depth
131
- def depth
132
- warn StructuredWarnings::DeprecatedMethodWarning,
133
- 'This method is deprecated. '\
134
- 'Please use node_depth() or node_height() instead (bug # 22535)'
135
-
136
- return 1 if is_leaf?
137
-
138
- 1 + @children.collect { |child| child.depth }.max
139
- end
140
-
141
108
  # @!attribute [r] breadth
142
109
  # Breadth of the tree at this node's level.
143
110
  # A single node without siblings has a breadth of 1.
@@ -148,7 +115,7 @@ module Tree::Utils
148
115
  #
149
116
  # @return [Integer] breadth of the node's level.
150
117
  def breadth
151
- is_root? ? 1 : parent.children.size
118
+ root? ? 1 : parent.children.size
152
119
  end
153
120
 
154
121
  # @!attribute [r] in_degree
@@ -163,7 +130,7 @@ module Tree::Utils
163
130
  #
164
131
  # @return [Integer] The in-degree of this node.
165
132
  def in_degree
166
- is_root? ? 0 : 1
133
+ root? ? 0 : 1
167
134
  end
168
135
 
169
136
  # @!attribute [r] out_degree
@@ -174,10 +141,10 @@ module Tree::Utils
174
141
  #
175
142
  # @return [Integer] The out-degree of this node.
176
143
  def out_degree
177
- is_leaf? ? 0 : children.size
144
+ leaf? ? 0 : children.size
178
145
  end
179
146
 
180
147
  # @!endgroup
181
- end # self.included
148
+ end
182
149
  end
183
150
  end
@@ -4,9 +4,9 @@
4
4
  #
5
5
  # Author:: Marco Ziccardi and Anupam Sengupta (anupamsg@gmail.com)
6
6
  #
7
- # Time-stamp: <2021-12-29 13:01:58 anupam>
7
+ # Time-stamp: <2022-06-20 02:00:11 anupam>
8
8
  #
9
- # Copyright (C) 2015, 2021 Anupam Sengupta <anupamsg@gmail.com>
9
+ # Copyright (C) 2015, 2021, 2022 Anupam Sengupta <anupamsg@gmail.com>
10
10
  #
11
11
  # All rights reserved.
12
12
  #
@@ -35,12 +35,12 @@
35
35
  # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36
36
  # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37
37
  #
38
+ # frozen_string_literal: true
38
39
 
39
- module Tree::Utils
40
- # Provides utility methods for path extraction
41
- module TreePathHandler
42
- # noinspection RubyUnusedLocalVariable
43
- def self.included(_base)
40
+ module Tree
41
+ module Utils
42
+ # Provides utility methods for path extraction
43
+ module TreePathHandler
44
44
  # @!group Node Path
45
45
 
46
46
  # Returns the path of this node from the root as a string, with the node
@@ -75,16 +75,16 @@ module Tree::Utils
75
75
  def get_path_name_array(current_array_path = [])
76
76
  path_array = current_array_path + [name]
77
77
 
78
- if !parent # If detached node or root node.
78
+ if parent # Recurse to parent node.
79
+ parent.get_path_name_array(path_array)
80
+ else # else If detached node or root node.
79
81
  path_array
80
- else # Else recurse to parent node.
81
- path_array = parent.get_path_name_array(path_array)
82
82
  end
83
83
  end
84
84
 
85
85
  protected :get_path_name_array
86
86
 
87
87
  # @!endgroup
88
- end # self.included
88
+ end
89
89
  end
90
90
  end
@@ -3,9 +3,9 @@
3
3
  # tree_merge_handler.rb
4
4
  #
5
5
  # Author: Anupam Sengupta
6
- # Time-stamp: <2015-05-30 16:06:18 anupam>
6
+ # Time-stamp: <2022-06-20 22:17:12 anupam>
7
7
  #
8
- # Copyright (C) 2013, 2015 Anupam Sengupta (anupamsg@gmail.com)
8
+ # Copyright (C) 2013, 2015, 2022 Anupam Sengupta (anupamsg@gmail.com)
9
9
  #
10
10
  # All rights reserved.
11
11
  #
@@ -34,95 +34,95 @@
34
34
  # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35
35
  # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
36
  #
37
-
38
- require_relative '../../../lib/tree/utils/utils'
37
+ # frozen_string_literal: true
39
38
 
40
39
  # Provides utility methods to merge two {Tree::TreeNode} based trees.
41
40
  # @since 0.9.0
42
- module Tree::Utils::TreeMergeHandler
43
- # @!group Merging Trees
41
+ module Tree
42
+ module Utils
43
+ # Handles merging of two trees.
44
+ module TreeMergeHandler
45
+ # @!group Merging Trees
44
46
 
45
- # Merge two trees that share the same root node and returns <em>a new
46
- # tree</em>.
47
- #
48
- # The new tree contains the contents of the merge between _other_tree_ and
49
- # self. Duplicate nodes (coming from _other_tree_) will *NOT* be overwritten
50
- # in self.
51
- #
52
- # @author Darren Oakley (https://github.com/dazoakley)
53
- #
54
- # @param [Tree::TreeNode] other_tree The other tree to merge with.
55
- # @return [Tree::TreeNode] the resulting tree following the merge.
56
- #
57
- # @raise [TypeError] This exception is raised if _other_tree_ is not a
58
- # {Tree::TreeNode}.
59
- #
60
- # @raise [ArgumentError] This exception is raised if _other_tree_ does not
61
- # have the same root node as self.
62
- def merge(other_tree)
63
- check_merge_prerequisites(other_tree)
64
- merge_trees(root.dup, other_tree.root)
65
- end
47
+ # Merge two trees that share the same root node and returns <em>a new
48
+ # tree</em>.
49
+ #
50
+ # The new tree contains the contents of the merge between _other_tree_ and
51
+ # self. Duplicate nodes (coming from _other_tree_) will *NOT* be overwritten
52
+ # in self.
53
+ #
54
+ # @author Darren Oakley (https://github.com/dazoakley)
55
+ #
56
+ # @param [Tree::TreeNode] other_tree The other tree to merge with.
57
+ # @return [Tree::TreeNode] the resulting tree following the merge.
58
+ #
59
+ # @raise [TypeError] This exception is raised if _other_tree_ is not a
60
+ # {Tree::TreeNode}.
61
+ #
62
+ # @raise [ArgumentError] This exception is raised if _other_tree_ does not
63
+ # have the same root node as self.
64
+ def merge(other_tree)
65
+ check_merge_prerequisites(other_tree)
66
+ merge_trees(root.dup, other_tree.root)
67
+ end
66
68
 
67
- # Merge in another tree (that shares the same root node) into +this+ tree.
68
- # Duplicate nodes (coming from _other_tree_) will NOT be overwritten in
69
- # self.
70
- #
71
- # @author Darren Oakley (https://github.com/dazoakley)
72
- #
73
- # @param [Tree::TreeNode] other_tree The other tree to merge with.
74
- #
75
- # @raise [TypeError] This exception is raised if _other_tree_ is not a
76
- # {Tree::TreeNode}.
77
- #
78
- # @raise [ArgumentError] This exception is raised if _other_tree_ does not
79
- # have the same root node as self.
80
- def merge!(other_tree)
81
- check_merge_prerequisites(other_tree)
82
- merge_trees(root, other_tree.root)
83
- end
69
+ # Merge in another tree (that shares the same root node) into +this+ tree.
70
+ # Duplicate nodes (coming from _other_tree_) will NOT be overwritten in
71
+ # self.
72
+ #
73
+ # @author Darren Oakley (https://github.com/dazoakley)
74
+ #
75
+ # @param [Tree::TreeNode] other_tree The other tree to merge with.
76
+ #
77
+ # @raise [TypeError] This exception is raised if _other_tree_ is not a
78
+ # {Tree::TreeNode}.
79
+ #
80
+ # @raise [ArgumentError] This exception is raised if _other_tree_ does not
81
+ # have the same root node as self.
82
+ def merge!(other_tree)
83
+ check_merge_prerequisites(other_tree)
84
+ merge_trees(root, other_tree.root)
85
+ end
84
86
 
85
- private
87
+ private
86
88
 
87
- # Utility function to check that the conditions for a tree merge are met.
88
- #
89
- # @author Darren Oakley (https://github.com/dazoakley)
90
- #
91
- # @see #merge
92
- # @see #merge!
93
- def check_merge_prerequisites(other_tree)
94
- unless other_tree.is_a?(Tree::TreeNode)
95
- raise TypeError,
96
- 'You can only merge in another instance of Tree::TreeNode'
97
- end
89
+ # Utility function to check that the conditions for a tree merge are met.
90
+ #
91
+ # @author Darren Oakley (https://github.com/dazoakley)
92
+ #
93
+ # @see #merge
94
+ # @see #merge!
95
+ def check_merge_prerequisites(other_tree)
96
+ raise TypeError, 'You can only merge in another instance of Tree::TreeNode' \
97
+ unless other_tree.is_a?(Tree::TreeNode)
98
98
 
99
- unless root.name == other_tree.root.name
100
- raise ArgumentError,
101
- 'Unable to merge trees as they do not share the same root'
102
- end
103
- end
99
+ raise ArgumentError, 'Unable to merge trees as they do not share the same root' \
100
+ unless root.name == other_tree.root.name
101
+ end
104
102
 
105
- # Utility function to recursively merge two subtrees.
106
- #
107
- # @author Darren Oakley (https://github.com/dazoakley)
108
- #
109
- # @param [Tree::TreeNode] tree1 The target tree to merge into.
110
- # @param [Tree::TreeNode] tree2 The donor tree (that will be merged
111
- # into target).
112
- # @raise [Tree::TreeNode] The merged tree.
113
- def merge_trees(tree1, tree2)
114
- names1 = tree1.has_children? ? tree1.children.map { |child| child.name } : []
115
- names2 = tree2.has_children? ? tree2.children.map { |child| child.name } : []
103
+ # Utility function to recursively merge two subtrees.
104
+ #
105
+ # @author Darren Oakley (https://github.com/dazoakley)
106
+ #
107
+ # @param [Tree::TreeNode] tree1 The target tree to merge into.
108
+ # @param [Tree::TreeNode] tree2 The donor tree (that will be merged
109
+ # into target).
110
+ # @raise [Tree::TreeNode] The merged tree.
111
+ def merge_trees(tree1, tree2)
112
+ names1 = tree1.children? ? tree1.children.map(&:name) : []
113
+ names2 = tree2.children? ? tree2.children.map(&:name) : []
116
114
 
117
- names_to_merge = names2 - names1
118
- names_to_merge.each do |name|
119
- tree1 << tree2[name].detached_subtree_copy
120
- end
115
+ names_to_merge = names2 - names1
116
+ names_to_merge.each do |name|
117
+ tree1 << tree2[name].detached_subtree_copy
118
+ end
121
119
 
122
- tree1.children.each do |child|
123
- merge_trees(child, tree2[child.name]) unless tree2[child.name].nil?
124
- end
120
+ tree1.children.each do |child|
121
+ merge_trees(child, tree2[child.name]) unless tree2[child.name].nil?
122
+ end
125
123
 
126
- tree1
124
+ tree1
125
+ end
126
+ end
127
127
  end
128
128
  end
@@ -4,11 +4,9 @@
4
4
  #
5
5
  # Author:: Anupam Sengupta (anupamsg@gmail.com)
6
6
  #
7
- # Time-stamp: <2021-12-29 13:02:08 anupam>
7
+ # Time-stamp: <2022-06-20 01:21:38 anupam>
8
8
  #
9
- # Copyright (C) 2012, 2015, 2021 Anupam Sengupta <anupamsg@gmail.com>
10
- #
11
- # All rights reserved.
9
+ # Copyright (C) 2012-2022 Anupam Sengupta. All rights reserved.
12
10
  #
13
11
  # Redistribution and use in source and binary forms, with or without
14
12
  # modification, are permitted provided that the following conditions are met:
@@ -34,9 +32,13 @@
34
32
  # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
35
33
  # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36
34
  # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
+ #
36
+ # frozen_string_literal: true
37
37
 
38
38
  # Provides utilities and mixin modules for RubyTree.
39
-
40
- module Tree::Utils
41
- # Empty module. Being used as a namespace.
39
+ module Tree
40
+ # Provides various utilities for the TreeNode class.
41
+ module Utils
42
+ # Empty module. Being used as a namespace.
43
+ end
42
44
  end
data/lib/tree/version.rb CHANGED
@@ -4,9 +4,7 @@
4
4
  #
5
5
  # Author:: Anupam Sengupta (anupamsg@gmail.com)
6
6
  #
7
- # Copyright (c) 2012, 2013, 2014, 2015, 2017, 2020, 2021 Anupam Sengupta
8
- #
9
- # All rights reserved.
7
+ # Copyright (c) 2012-2022 Anupam Sengupta. All rights reserved.
10
8
  #
11
9
  # Redistribution and use in source and binary forms, with or without
12
10
  # modification, are permitted provided that the following conditions are met:
@@ -33,8 +31,9 @@
33
31
  # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34
32
  # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
33
  #
34
+ # frozen_string_literal: true
36
35
 
37
36
  module Tree
38
37
  # Rubytree Package Version
39
- VERSION = '1.0.2'.freeze
38
+ VERSION = '2.0.0'
40
39
  end