rubytree 0.9.5 → 1.0.2

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:14:09 anupam>
7
+ # Time-stamp: <2021-12-29 13:02:04 anupam>
8
8
  #
9
- # Copyright (C) 2012, 2013, 2015 Anupam Sengupta <anupamsg@gmail.com>
9
+ # Copyright (C) 2012, 2013, 2015, 2017, 2021 Anupam Sengupta <anupamsg@gmail.com>
10
10
  #
11
11
  # All rights reserved.
12
12
  #
@@ -42,38 +42,36 @@ module Tree::Utils
42
42
  # Provides utility functions to handle CamelCase methods, and redirect
43
43
  # invocation of such methods to the snake_case equivalents.
44
44
  module CamelCaseMethodHandler
45
- def self.included(base)
45
+ def self.included(_base)
46
46
  # @!visibility private
47
47
  # Allow the deprecated CamelCase method names. Display a warning.
48
48
  # :nodoc:
49
49
  def method_missing(meth, *args, &blk)
50
- if self.respond_to?(new_method_name = to_snake_case(meth))
51
- warn DeprecatedMethodWarning,
52
- "The camelCased methods are deprecated. "\
50
+ if respond_to?((new_method_name = to_snake_case(meth)))
51
+ warn StructuredWarnings::DeprecatedMethodWarning,
52
+ 'The camelCased methods are deprecated. ' +
53
53
  "Please use #{new_method_name} instead of #{meth}"
54
- return send(new_method_name, *args, &blk)
54
+ send(new_method_name, *args, &blk)
55
55
  else
56
56
  super
57
57
  end
58
58
  end
59
59
 
60
- private
61
-
62
60
  # @!visibility private
63
61
  # Convert a CamelCasedWord to a underscore separated camel_cased_word.
64
62
  #
65
63
  # @param [String] camel_cased_word The word to be converted to snake_case.
66
64
  # @return [String] the snake_cased_word.
67
65
  def to_snake_case(camel_cased_word)
68
- word = camel_cased_word.to_s.dup
66
+ word = camel_cased_word.to_s
69
67
  word.gsub!(/::/, '/')
70
- word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
71
- word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
72
- word.tr!("-", "_")
68
+ word.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
69
+ word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
70
+ word.tr!('-', '_')
73
71
  word.downcase!
74
72
  word
75
73
  end
76
-
74
+ protected :to_snake_case
77
75
  end # self.included
78
76
  end
79
77
  end
@@ -37,8 +37,9 @@
37
37
  # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38
38
  # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39
39
 
40
- module Tree::Utils::HashConverter
40
+ require_relative '../../../lib/tree/utils/utils'
41
41
 
42
+ module Tree::Utils::HashConverter
42
43
  def self.included(base)
43
44
  base.extend(ClassMethods)
44
45
  end
@@ -47,7 +48,6 @@ module Tree::Utils::HashConverter
47
48
  # class methods on any class mixing in the {Tree::Utils::HashConverter}
48
49
  # module.
49
50
  module ClassMethods
50
-
51
51
  # Factory method builds a {Tree::TreeNode} from a +Hash+.
52
52
  #
53
53
  # This method will interpret each key of your +Hash+ as a {Tree::TreeNode}.
@@ -92,84 +92,82 @@ module Tree::Utils::HashConverter
92
92
  # values that are not hashes or nils.
93
93
 
94
94
  def from_hash(hash)
95
- raise ArgumentError, "Argument must be a type of hash"\
95
+ raise ArgumentError, 'Argument must be a type of hash'\
96
96
  unless hash.is_a?(Hash)
97
97
 
98
- raise ArgumentError, "Hash must have one top-level element"\
98
+ raise ArgumentError, 'Hash must have one top-level element'\
99
99
  if hash.size != 1
100
100
 
101
101
  root, children = hash.first
102
102
 
103
- unless [Hash, NilClass].include?(children.class)
104
- raise ArgumentError, "Invalid child. Must be nil or hash."
105
- end
103
+ raise ArgumentError, 'Invalid child. Must be nil or hash.' unless [Hash, NilClass].include?(children.class)
106
104
 
107
- node = self.new(*root)
105
+ node = new(*root)
108
106
  node.add_from_hash(children) unless children.nil?
109
107
  node
110
108
  end
111
109
  end
112
110
 
113
- # Instantiate and insert child nodes from data in a Ruby +Hash+
114
- #
115
- # This method is used in conjunction with from_hash to provide a
116
- # convenient way of building and inserting child nodes present in a Ruby
117
- # hashes.
118
- #
119
- # This method will instantiate a node instance for each top-
120
- # level key of the input hash, to be inserted as children of the receiver
121
- # instance.
122
- #
123
- # Nested hashes are expected and further child nodes will be created and
124
- # added accordingly. If a hash key is a single value that value will be
125
- # used as the name for the node. If a hash key is an Array, both node
126
- # name and content will be populated.
127
- #
128
- # A leaf element of the tree should be represented as a hash key with
129
- # corresponding value +nil+ or {}.
130
- #
131
- # @example
132
- # root = Tree::TreeNode.new(:A, "Root content!")
133
- # root.add_from_hash({:B => {:D => {}}, [:C, "C content!"] => {}})
134
- #
135
- # @author Jen Hamon (http://www.github.com/jhamon)
136
- # @param [Hash] children The hash of child subtrees.
137
- # @raise [ArgumentError] This exception is raised if a non-hash is passed.
138
- # @return [Array] Array of child nodes added
139
- # @see ClassMethods#from_hash
140
- def add_from_hash(children)
141
- raise ArgumentError, "Argument must be a type of hash"\
142
- unless children.is_a?(Hash)
143
-
144
- child_nodes = []
145
- children.each do |child, grandchildren|
146
- child_node = self.class.from_hash({child => grandchildren})
147
- child_nodes << child_node
148
- self << child_node
149
- end
111
+ # Instantiate and insert child nodes from data in a Ruby +Hash+
112
+ #
113
+ # This method is used in conjunction with from_hash to provide a
114
+ # convenient way of building and inserting child nodes present in a Ruby
115
+ # hashes.
116
+ #
117
+ # This method will instantiate a node instance for each top-
118
+ # level key of the input hash, to be inserted as children of the receiver
119
+ # instance.
120
+ #
121
+ # Nested hashes are expected and further child nodes will be created and
122
+ # added accordingly. If a hash key is a single value that value will be
123
+ # used as the name for the node. If a hash key is an Array, both node
124
+ # name and content will be populated.
125
+ #
126
+ # A leaf element of the tree should be represented as a hash key with
127
+ # corresponding value +nil+ or {}.
128
+ #
129
+ # @example
130
+ # root = Tree::TreeNode.new(:A, "Root content!")
131
+ # root.add_from_hash({:B => {:D => {}}, [:C, "C content!"] => {}})
132
+ #
133
+ # @author Jen Hamon (http://www.github.com/jhamon)
134
+ # @param [Hash] children The hash of child subtrees.
135
+ # @raise [ArgumentError] This exception is raised if a non-hash is passed.
136
+ # @return [Array] Array of child nodes added
137
+ # @see ClassMethods#from_hash
138
+ def add_from_hash(children)
139
+ raise ArgumentError, 'Argument must be a type of hash'\
140
+ unless children.is_a?(Hash)
150
141
 
151
- child_nodes
142
+ child_nodes = []
143
+ children.each do |child, grandchildren|
144
+ child_node = self.class.from_hash({ child => grandchildren })
145
+ child_nodes << child_node
146
+ self << child_node
152
147
  end
153
148
 
154
- # Convert a node and its subtree into a Ruby hash.
155
- #
156
- # @example
157
- # root = Tree::TreeNode.new(:root, "root content")
158
- # root << Tree::TreeNode.new(:child1, "child1 content")
159
- # root << Tree::TreeNode.new(:child2, "child2 content")
160
- # root.to_h # => {[:root, "root content"] =>
161
- # { [:child1, "child1 content"] =>
162
- # {}, [:child2, "child2 content"] => {}}}
163
- # @author Jen Hamon (http://www.github.com/jhamon)
164
- # @return [Hash] Hash representation of tree.
165
- def to_h
166
- key = has_content? ? [name, content] : name
149
+ child_nodes
150
+ end
167
151
 
168
- children_hash = {}
169
- children do |child|
170
- children_hash.merge! child.to_h
171
- end
152
+ # Convert a node and its subtree into a Ruby hash.
153
+ #
154
+ # @example
155
+ # root = Tree::TreeNode.new(:root, "root content")
156
+ # root << Tree::TreeNode.new(:child1, "child1 content")
157
+ # root << Tree::TreeNode.new(:child2, "child2 content")
158
+ # root.to_h # => {[:root, "root content"] =>
159
+ # { [:child1, "child1 content"] =>
160
+ # {}, [:child2, "child2 content"] => {}}}
161
+ # @author Jen Hamon (http://www.github.com/jhamon)
162
+ # @return [Hash] Hash representation of tree.
163
+ def to_h
164
+ key = has_content? ? [name, content] : name
172
165
 
173
- { key => children_hash }
166
+ children_hash = {}
167
+ children do |child|
168
+ children_hash.merge! child.to_h
174
169
  end
170
+
171
+ { key => children_hash }
172
+ end
175
173
  end
@@ -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
+ require_relative '../utils/utils'
38
39
  require 'json'
39
40
 
40
41
  # Provides utility methods to convert a {Tree::TreeNode} to and from
41
42
  # JSON[http://flori.github.com/json/].
42
43
  module Tree::Utils::JSONConverter
43
-
44
44
  def self.included(base)
45
45
  base.extend(ClassMethods)
46
46
  end
@@ -57,22 +57,21 @@ module Tree::Utils::JSONConverter
57
57
  # Rails uses JSON in ActiveSupport, and all Rails JSON encoding goes through
58
58
  # +as_json+.
59
59
  #
60
+ # @param [Object] options
61
+ #
60
62
  # @see #to_json
61
63
  # @see http://stackoverflow.com/a/6880638/273808
62
- def as_json(options = {})
63
-
64
+ # noinspection RubyUnusedLocalVariable
65
+ def as_json(_options = {})
64
66
  json_hash = {
65
- "name" => name,
66
- "content" => content,
67
+ name: name,
68
+ content: content,
67
69
  JSON.create_id => self.class.name
68
70
  }
69
71
 
70
- if has_children?
71
- json_hash["children"] = children
72
- end
73
-
74
- return json_hash
72
+ json_hash['children'] = children if has_children?
75
73
 
74
+ json_hash
76
75
  end
77
76
 
78
77
  # Creates a JSON representation of this node including all it's children.
@@ -113,15 +112,15 @@ module Tree::Utils::JSONConverter
113
112
  # @see #to_json
114
113
  # @see http://flori.github.com/json
115
114
  def json_create(json_hash)
115
+ node = new(json_hash['name'], json_hash['content'])
116
116
 
117
- node = new(json_hash["name"], json_hash["content"])
118
-
119
- json_hash["children"].each do |child|
120
- node << child
121
- end if json_hash["children"]
122
-
123
- return node
117
+ if json_hash['children']
118
+ json_hash['children'].each do |child|
119
+ node << child
120
+ end
121
+ end
124
122
 
123
+ node
125
124
  end
126
125
  end
127
126
  end
@@ -4,9 +4,9 @@
4
4
  #
5
5
  # Author:: Anupam Sengupta (anupamsg@gmail.com)
6
6
  #
7
- # Time-stamp: <2015-05-30 14:23:23 anupam>
7
+ # Time-stamp: <2021-12-29 13:01:54 anupam>
8
8
  #
9
- # Copyright (C) 2013, 2015 Anupam Sengupta <anupamsg@gmail.com>
9
+ # Copyright (C) 2013, 2015, 2017, 2021 Anupam Sengupta <anupamsg@gmail.com>
10
10
  #
11
11
  # All rights reserved.
12
12
  #
@@ -41,8 +41,8 @@ require 'structured_warnings'
41
41
  module Tree::Utils
42
42
  # Provides utility functions to measure various tree metrics.
43
43
  module TreeMetricsHandler
44
- def self.included(base)
45
-
44
+ # noinspection RubyUnusedLocalVariable
45
+ def self.included(_base)
46
46
  # @!group Metrics and Measures
47
47
 
48
48
  # @!attribute [r] size
@@ -54,7 +54,7 @@ module Tree::Utils
54
54
  #
55
55
  # @return [Integer] Total number of nodes in this (sub)tree.
56
56
  def size
57
- inject(0) {|sum, node| sum + 1 if node}
57
+ inject(0) { |sum, node| sum + 1 if node }
58
58
  end
59
59
 
60
60
  # @!attribute [r] length
@@ -66,7 +66,7 @@ module Tree::Utils
66
66
  # @return [Integer] The total number of nodes in this (sub)tree.
67
67
  # @see #size
68
68
  def length
69
- size()
69
+ size
70
70
  end
71
71
 
72
72
  # @!attribute [r] node_height
@@ -80,6 +80,7 @@ module Tree::Utils
80
80
  # @return [Integer] Height of the node.
81
81
  def node_height
82
82
  return 0 if is_leaf?
83
+
83
84
  1 + @children.collect { |child| child.node_height }.max
84
85
  end
85
86
 
@@ -98,6 +99,7 @@ module Tree::Utils
98
99
  # @return [Integer] Depth of this node.
99
100
  def node_depth
100
101
  return 0 if is_root?
102
+
101
103
  1 + parent.node_depth
102
104
  end
103
105
 
@@ -127,11 +129,12 @@ module Tree::Utils
127
129
  #
128
130
  # @see #node_depth
129
131
  def depth
130
- warn DeprecatedMethodWarning,
131
- "This method is deprecated. "\
132
- "Please use node_depth() or node_height() instead (bug # 22535)"
132
+ warn StructuredWarnings::DeprecatedMethodWarning,
133
+ 'This method is deprecated. '\
134
+ 'Please use node_depth() or node_height() instead (bug # 22535)'
133
135
 
134
136
  return 1 if is_leaf?
137
+
135
138
  1 + @children.collect { |child| child.depth }.max
136
139
  end
137
140
 
@@ -4,9 +4,9 @@
4
4
  #
5
5
  # Author:: Marco Ziccardi and Anupam Sengupta (anupamsg@gmail.com)
6
6
  #
7
- # Time-stamp: <2015-05-30 16:04:00 anupam>
7
+ # Time-stamp: <2021-12-29 13:01:58 anupam>
8
8
  #
9
- # Copyright (C) 2015 Anupam Sengupta <anupamsg@gmail.com>
9
+ # Copyright (C) 2015, 2021 Anupam Sengupta <anupamsg@gmail.com>
10
10
  #
11
11
  # All rights reserved.
12
12
  #
@@ -39,8 +39,8 @@
39
39
  module Tree::Utils
40
40
  # Provides utility methods for path extraction
41
41
  module TreePathHandler
42
- def self.included(base)
43
-
42
+ # noinspection RubyUnusedLocalVariable
43
+ def self.included(_base)
44
44
  # @!group Node Path
45
45
 
46
46
  # Returns the path of this node from the root as a string, with the node
@@ -53,7 +53,7 @@ module Tree::Utils
53
53
  # @return [String] The node path with names separated using the specified
54
54
  # separator.
55
55
  def path_as_string(separator = '=>')
56
- path_as_array().join(separator)
56
+ path_as_array.join(separator)
57
57
  end
58
58
 
59
59
  # Returns the node-names from this node to the root as an array. The first
@@ -61,8 +61,8 @@ module Tree::Utils
61
61
  #
62
62
  # @return [Array] The array containing the node names for the path to this
63
63
  # node
64
- def path_as_array()
65
- get_path_name_array().reverse
64
+ def path_as_array
65
+ get_path_name_array.reverse
66
66
  end
67
67
 
68
68
  # @!visibility private
@@ -76,10 +76,9 @@ module Tree::Utils
76
76
  path_array = current_array_path + [name]
77
77
 
78
78
  if !parent # If detached node or root node.
79
- return path_array
79
+ path_array
80
80
  else # Else recurse to parent node.
81
81
  path_array = parent.get_path_name_array(path_array)
82
- return path_array
83
82
  end
84
83
  end
85
84
 
@@ -88,5 +87,4 @@ module Tree::Utils
88
87
  # @!endgroup
89
88
  end # self.included
90
89
  end
91
-
92
90
  end
@@ -35,10 +35,11 @@
35
35
  # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
36
  #
37
37
 
38
+ require_relative '../../../lib/tree/utils/utils'
39
+
38
40
  # Provides utility methods to merge two {Tree::TreeNode} based trees.
39
41
  # @since 0.9.0
40
42
  module Tree::Utils::TreeMergeHandler
41
-
42
43
  # @!group Merging Trees
43
44
 
44
45
  # Merge two trees that share the same root node and returns <em>a new
@@ -60,8 +61,7 @@ module Tree::Utils::TreeMergeHandler
60
61
  # have the same root node as self.
61
62
  def merge(other_tree)
62
63
  check_merge_prerequisites(other_tree)
63
- new_tree = merge_trees(self.root.dup, other_tree.root)
64
- return new_tree
64
+ merge_trees(root.dup, other_tree.root)
65
65
  end
66
66
 
67
67
  # Merge in another tree (that shares the same root node) into +this+ tree.
@@ -78,8 +78,8 @@ module Tree::Utils::TreeMergeHandler
78
78
  # @raise [ArgumentError] This exception is raised if _other_tree_ does not
79
79
  # have the same root node as self.
80
80
  def merge!(other_tree)
81
- check_merge_prerequisites( other_tree )
82
- merge_trees( self.root, other_tree.root )
81
+ check_merge_prerequisites(other_tree)
82
+ merge_trees(root, other_tree.root)
83
83
  end
84
84
 
85
85
  private
@@ -96,13 +96,13 @@ module Tree::Utils::TreeMergeHandler
96
96
  'You can only merge in another instance of Tree::TreeNode'
97
97
  end
98
98
 
99
- unless self.root.name == other_tree.root.name
99
+ unless root.name == other_tree.root.name
100
100
  raise ArgumentError,
101
101
  'Unable to merge trees as they do not share the same root'
102
102
  end
103
103
  end
104
104
 
105
- # Utility function to recursivley merge two subtrees.
105
+ # Utility function to recursively merge two subtrees.
106
106
  #
107
107
  # @author Darren Oakley (https://github.com/dazoakley)
108
108
  #
@@ -120,10 +120,9 @@ module Tree::Utils::TreeMergeHandler
120
120
  end
121
121
 
122
122
  tree1.children.each do |child|
123
- merge_trees( child, tree2[child.name] ) unless tree2[child.name].nil?
123
+ merge_trees(child, tree2[child.name]) unless tree2[child.name].nil?
124
124
  end
125
125
 
126
- return tree1
126
+ tree1
127
127
  end
128
-
129
128
  end
@@ -4,9 +4,9 @@
4
4
  #
5
5
  # Author:: Anupam Sengupta (anupamsg@gmail.com)
6
6
  #
7
- # Time-stamp: <2015-05-30 14:25:57 anupam>
7
+ # Time-stamp: <2021-12-29 13:02:08 anupam>
8
8
  #
9
- # Copyright (C) 2012, 2015 Anupam Sengupta <anupamsg@gmail.com>
9
+ # Copyright (C) 2012, 2015, 2021 Anupam Sengupta <anupamsg@gmail.com>
10
10
  #
11
11
  # All rights reserved.
12
12
  #
@@ -36,6 +36,7 @@
36
36
  # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37
37
 
38
38
  # Provides utilities and mixin modules for RubyTree.
39
+
39
40
  module Tree::Utils
40
41
  # Empty module. Being used as a namespace.
41
42
  end
data/lib/tree/version.rb CHANGED
@@ -4,7 +4,7 @@
4
4
  #
5
5
  # Author:: Anupam Sengupta (anupamsg@gmail.com)
6
6
  #
7
- # Copyright (c) 2012, 2013, 2014, 2015 Anupam Sengupta
7
+ # Copyright (c) 2012, 2013, 2014, 2015, 2017, 2020, 2021 Anupam Sengupta
8
8
  #
9
9
  # All rights reserved.
10
10
  #
@@ -34,8 +34,7 @@
34
34
  # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
35
  #
36
36
 
37
- #
38
37
  module Tree
39
38
  # Rubytree Package Version
40
- VERSION = '0.9.5'
39
+ VERSION = '1.0.2'.freeze
41
40
  end