Skip to content

Commit b39b50b

Browse files
committed
Removed the is_ and has_ prefixes from predicate methods.
This is to comply with the Ruby method naming convention for predicate methods (the ones ending with `?`). Rest of the code base has been updated to use the new method names. However, for backward compatibility, `aliases` with the old method names have been defined to the new names so that current client code can continue to work.
1 parent aff2a34 commit b39b50b

12 files changed

Lines changed: 195 additions & 173 deletions

.rubocop_todo.yml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,6 @@ Metrics/MethodLength:
3737
Metrics/PerceivedComplexity:
3838
Max: 9
3939

40-
# Offense count: 9
41-
# Configuration parameters: NamePrefix, ForbiddenPrefixes, AllowedMethods, MethodDefinitionMacros.
42-
# NamePrefix: is_, has_, have_
43-
# ForbiddenPrefixes: is_, has_, have_
44-
# AllowedMethods: is_a?
45-
# MethodDefinitionMacros: define_method, define_singleton_method
46-
Naming/PredicateName:
47-
Exclude:
48-
- 'spec/**/*'
49-
- 'lib/tree.rb'
50-
- 'lib/tree/binarytree.rb'
51-
5240
# Offense count: 5
5341
Security/MarshalLoad:
5442
Exclude:

API-CHANGES.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ smooth transition to the new APIs.
2020
* Use of integers as node-names now no longer requires the optional
2121
`num_as_name` method argument.
2222

23+
* The predicate methods beginning with `is_` or `has_` are now aliases to the
24+
real methods **without** these prefixes. For example,
25+
`Tree::TreeNode#is_root?` is now aliased to `Tree::TreeNode#root?`. This is to
26+
comply with the Ruby standard. These original prefixed method names should be
27+
considered as deprecated and the corresponding non-prefixed method names
28+
should be used instead. it is possible that the old prefixed method names
29+
might be removed in the future.
30+
2331
* [structured_warnings][] has been **removed** from the code-base and is no
2432
longer a dependency. This was a long-standing point of friction for many
2533
users.

History.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010

1111
* Support for `CamelCase` methods has been dropped.
1212

13+
* The predicate methods beginning with `is_` or `has_` are now aliases to real
14+
methods **without** these prefixes. For example, `Tree::TreeNode#is_root?` is
15+
now aliased to `Tree::TreeNode#root?`. This is to comply with the Ruby
16+
standard. The original prefixed method names should be considered as
17+
deprecated and the corresponding non-prefixed method names should be used
18+
instead. it is possible that the old prefixed method names might be removed in
19+
the future.
20+
1321
* RubyTree now supports MRI Ruby versions `2.6.x`, `2.7.x`, and `3.0.x`.
1422

1523
* Explicit support for `rbx` Ruby has been removed (_might_ still work, but not

lib/tree.rb

Lines changed: 51 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -130,38 +130,44 @@ class TreeNode
130130
# @return [Tree::TreeNode] Root of the (sub)tree.
131131
def root
132132
root = self
133-
root = root.parent until root.is_root?
133+
root = root.parent until root.root?
134134
root
135135
end
136136

137-
# @!attribute [r] is_root?
137+
# @!attribute [r] root?
138138
# Returns +true+ if this is a root node. Note that
139139
# orphaned children will also be reported as root nodes.
140140
#
141141
# @return [Boolean] +true+ if this is a root node.
142-
def is_root?
142+
def root?
143143
@parent.nil?
144144
end
145145

146-
# @!attribute [r] has_content?
146+
alias is_root? root? # @todo: Aliased for eventual replacement
147+
148+
# @!attribute [r] content?
147149
# +true+ if this node has content.
148150
#
149151
# @return [Boolean] +true+ if the node has content.
150-
def has_content?
152+
def content?
151153
@content != nil
152154
end
153155

154-
# @!attribute [r] is_leaf?
156+
alias has_content? content? # @todo: Aliased for eventual replacement
157+
158+
# @!attribute [r] leaf?
155159
# +true+ if this node is a _leaf_ - i.e., one without
156160
# any children.
157161
#
158162
# @return [Boolean] +true+ if this is a leaf node.
159163
#
160-
# @see #has_children?
161-
def is_leaf?
162-
!has_children?
164+
# @see #children?
165+
def leaf?
166+
!children?
163167
end
164168

169+
alias is_leaf? leaf? # @todo: Aliased for eventual replacement
170+
165171
# @!attribute [r] parentage
166172
# An array of ancestors of this node in reversed order
167173
# (the first element is the immediate parent of this node).
@@ -171,7 +177,7 @@ def is_leaf?
171177
# @return [Array<Tree::TreeNode>] An array of ancestors of this node
172178
# @return [nil] if this is a root node.
173179
def parentage
174-
return nil if is_root?
180+
return nil if root?
175181

176182
parentage_array = []
177183
prev_parent = parent
@@ -182,16 +188,18 @@ def parentage
182188
parentage_array
183189
end
184190

185-
# @!attribute [r] has_children?
191+
# @!attribute [r] children?
186192
# +true+ if the this node has any child node.
187193
#
188194
# @return [Boolean] +true+ if child nodes exist.
189195
#
190-
# @see #is_leaf?
191-
def has_children?
196+
# @see #leaf?
197+
def children?
192198
!@children.empty?
193199
end
194200

201+
alias has_children? children? # @todo: Aliased for eventual replacement
202+
195203
# @!group Node Creation
196204

197205
# Creates a new node with a name and optional content.
@@ -266,7 +274,7 @@ def marshal_dump
266274
# a hash.
267275
def create_dump_rep # :nodoc:
268276
{ name: @name,
269-
parent: (is_root? ? nil : @parent.name),
277+
parent: (root? ? nil : @parent.name),
270278
content: Marshal.dump(@content) }
271279
end
272280

@@ -311,7 +319,7 @@ def marshal_load(dumped_tree_array)
311319
# @return [String] A string representation of the node.
312320
def to_s
313321
"Node Name: #{@name} Content: #{@content.to_s || '<Empty>'} " \
314-
"Parent: #{is_root? ? '<None>' : @parent.name.to_s} " \
322+
"Parent: #{root? ? '<None>' : @parent.name.to_s} " \
315323
"Children: #{@children.length} Total Nodes: #{size}"
316324
end
317325

@@ -425,7 +433,7 @@ def insertion_range
425433
def rename(new_name)
426434
old_name = @name
427435

428-
if is_root?
436+
if root?
429437
self.name = new_name
430438
else
431439
@parent.rename_child old_name, new_name
@@ -519,7 +527,7 @@ def parent=(parent) # :nodoc:
519527
#
520528
# @see #remove_all!
521529
def remove_from_parent!
522-
@parent.remove!(self) unless is_root?
530+
@parent.remove!(self) unless root?
523531
end
524532

525533
# Removes all children from this node. If an independent reference exists to
@@ -657,7 +665,7 @@ def postordered_each
657665

658666
until node_stack.empty?
659667
peek_node = node_stack[0]
660-
if peek_node.node.has_children? && !peek_node.visited
668+
if peek_node.node.children? && !peek_node.visited
661669
peek_node.visited = true
662670
# Add the children to the stack. Use the marking structure.
663671
marked_children =
@@ -737,10 +745,10 @@ def children(&block)
737745
# noinspection RubyUnusedLocalVariable
738746
def each_leaf
739747
if block_given?
740-
each { |node| yield(node) if node.is_leaf? }
748+
each { |node| yield(node) if node.leaf? }
741749
self
742750
else
743-
self.select(&:is_leaf?)
751+
self.select(&:leaf?)
744752
end
745753
end
746754

@@ -798,22 +806,24 @@ def last_child
798806
#
799807
# @return [Tree::TreeNode] The first sibling node.
800808
#
801-
# @see #is_first_sibling?
809+
# @see #first_sibling?
802810
# @see #last_sibling
803811
def first_sibling
804-
is_root? ? self : parent.children.first
812+
root? ? self : parent.children.first
805813
end
806814

807815
# Returns +true+ if this node is the first sibling at its level.
808816
#
809817
# @return [Boolean] +true+ if this is the first sibling.
810818
#
811-
# @see #is_last_sibling?
819+
# @see #last_sibling?
812820
# @see #first_sibling
813-
def is_first_sibling?
821+
def first_sibling?
814822
first_sibling == self
815823
end
816824

825+
alias is_first_sibling? first_sibling? # @todo: Aliased for eventual replacement
826+
817827
# Last sibling of this node. If this is the root node, then returns
818828
# itself.
819829
#
@@ -824,22 +834,24 @@ def is_first_sibling?
824834
#
825835
# @return [Tree::TreeNode] The last sibling node.
826836
#
827-
# @see #is_last_sibling?
837+
# @see #last_sibling?
828838
# @see #first_sibling
829839
def last_sibling
830-
is_root? ? self : parent.children.last
840+
root? ? self : parent.children.last
831841
end
832842

833843
# Returns +true+ if this node is the last sibling at its level.
834844
#
835845
# @return [Boolean] +true+ if this is the last sibling.
836846
#
837-
# @see #is_first_sibling?
847+
# @see #first_sibling?
838848
# @see #last_sibling
839-
def is_last_sibling?
849+
def last_sibling?
840850
last_sibling == self
841851
end
842852

853+
alias is_last_sibling? last_sibling? # @todo: Aliased for eventual replacement
854+
843855
# An array of siblings for this node. This node is excluded.
844856
#
845857
# If a block is provided, yields each of the sibling nodes to the block.
@@ -859,7 +871,7 @@ def siblings
859871
parent.children.each { |sibling| yield sibling if sibling != self }
860872
self
861873
else
862-
return [] if is_root?
874+
return [] if root?
863875

864876
siblings = []
865877
parent.children do |my_sibling|
@@ -876,10 +888,12 @@ def siblings
876888
# @return [Boolean] +true+ if this is the only child of its parent.
877889
#
878890
# @see #siblings
879-
def is_only_child?
880-
is_root? ? true : parent.children.size == 1
891+
def only_child?
892+
root? ? true : parent.children.size == 1
881893
end
882894

895+
alias is_only_child? only_child? # @todo: Aliased for eventual replacement
896+
883897
# Next sibling for this node.
884898
# The _next_ node is defined as the node to right of this node.
885899
#
@@ -891,7 +905,7 @@ def is_only_child?
891905
# @see #previous_sibling
892906
# @see #siblings
893907
def next_sibling
894-
return nil if is_root?
908+
return nil if root?
895909

896910
idx = parent.children.index(self)
897911
parent.children.at(idx + 1) if idx
@@ -908,7 +922,7 @@ def next_sibling
908922
# @see #next_sibling
909923
# @see #siblings
910924
def previous_sibling
911-
return nil if is_root?
925+
return nil if root?
912926

913927
idx = parent.children.index(self)
914928
parent.children.at(idx - 1) if idx&.positive?
@@ -943,14 +957,14 @@ def print_tree(level = node_depth, max_depth = nil,
943957
})
944958
prefix = ''.dup # dup NEEDs to be invoked to make this mutable.
945959

946-
if is_root?
960+
if root?
947961
prefix << '*'
948962
else
949-
prefix << '|' unless parent.is_last_sibling?
963+
prefix << '|' unless parent.last_sibling?
950964
prefix << (' ' * (level - 1) * 4)
951-
prefix << (is_last_sibling? ? '+' : '|')
965+
prefix << (last_sibling? ? '+' : '|')
952966
prefix << '---'
953-
prefix << (has_children? ? '+' : '>')
967+
prefix << (children? ? '+' : '>')
954968
end
955969

956970
block.call(self, prefix)

lib/tree/binarytree.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,28 +75,32 @@ def right_child
7575
children[1]
7676
end
7777

78-
# @!attribute is_left_child?
78+
# @!attribute left_child?
7979
# +true+ if the receiver node is the left child of its parent.
8080
# Always returns +false+ if it is a root node.
8181
#
8282
# @return [Boolean] +true+ if this is the left child of its parent.
83-
def is_left_child?
84-
return false if is_root?
83+
def left_child?
84+
return false if root?
8585

8686
self == parent.left_child
8787
end
8888

89-
# @!attribute [r] is_right_child?
89+
alias is_left_child? left_child? # @todo: Aliased for eventual replacement
90+
91+
# @!attribute [r] right_child?
9092
# +true+ if the receiver node is the right child of its parent.
9193
# Always returns +false+ if it is a root node.
9294
#
9395
# @return [Boolean] +true+ if this is the right child of its parent.
94-
def is_right_child?
95-
return false if is_root?
96+
def right_child?
97+
return false if root?
9698

9799
self == parent.right_child
98100
end
99101

102+
alias is_right_child? right_child? # @todo: Aliased for eventual replacement
103+
100104
# @!group Structure Modification
101105

102106
# Adds the specified child node to the receiver node. The child node's

lib/tree/utils/hash_converter.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#
66
# Author:: Jen Hamon (http://www.github.com/jhamon)
77
#
8-
# Time-stamp: <2022-06-20 01:20:34 anupam>
8+
# Time-stamp: <2022-06-20 22:16:39 anupam>
99
#
1010
# Copyright (C) 2014, 2015, 2022, 2022 Jen Hamon (http://www.github.com/jhamon) and
1111
# Anupam Sengupta <anupamsg@gmail.com>
@@ -167,7 +167,7 @@ def add_from_hash(children)
167167
# @author Jen Hamon (http://www.github.com/jhamon)
168168
# @return [Hash] Hash representation of tree.
169169
def to_h
170-
key = has_content? ? [name, content] : name
170+
key = content? ? [name, content] : name
171171

172172
children_hash = {}
173173
children do |child|

lib/tree/utils/json_converter.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#
55
# Author:: Anupam Sengupta (anupamsg@gmail.com)
66
#
7-
# Time-stamp: <2022-06-20 01:18:54 anupam>
7+
# Time-stamp: <2022-06-20 22:16:46 anupam>
88
#
99
# Copyright (C) 2012, 2013, 2014, 2015, 2022 Anupam Sengupta <anupamsg@gmail.com>
1010
#
@@ -72,7 +72,7 @@ def as_json(_options = {})
7272
JSON.create_id => self.class.name
7373
}
7474

75-
json_hash['children'] = children if has_children?
75+
json_hash['children'] = children if children?
7676

7777
json_hash
7878
end

0 commit comments

Comments
 (0)