lib/jumoku/builders/extended.rb in jumoku-0.2.0 vs lib/jumoku/builders/extended.rb in jumoku-0.2.1

- old
+ new

@@ -1,7 +1,7 @@ module Jumoku - # This module provides methods extending the core TreeAPI. Most of those + # This module provides methods extending the core implementation. Most of those # methods are cloning the receiver tree and perform their operation on the # duplicated object. Some other are helpers adding reflexivity to trees. # There are also a few in-place methods. # module Extended @@ -161,15 +161,20 @@ # Removes all nodes mentionned in the specified Enumerable from the tree. # # The process relies on {RawTreeBuilder#remove_node! remove_node!}. # - # @param [#each] *a an Enumerable nodes set + # @param [#each] *nodes an Enumerable nodes set; may contain Range # @return [Tree] `self` # - def remove_nodes!(*a) - a.flatten.each { |v| remove_node! v } + # @example + # + # remove_nodes! 1, 3..5 + # + def remove_nodes!(*nodes) + nodes = nodes.to_a.map { |i| i.is_a?(Range) ? i.to_a : i }.flatten + nodes.each { |v| remove_node! v } self end alias delete_nodes! remove_nodes! # Same as {TreeBuilder#remove_nodes! remove_nodes!} but works on a copy of the receiver. @@ -263,22 +268,15 @@ # @param [*Branch, *nodes] *maybe_branches a list of branches, either as Branch # objects or as nodes pairs # @return [Boolean] # def branches?(*maybe_branches) - list = maybe_branches.create_branches_list(_branch_type) - all = true - - # Branch objects are really Edge objects within Plexus, therefore - # cannot rely on #eql? to compare those structures and must drop - # down to the attributes. - list.each do |e| # Jumoku::Branch structs - all = branches.any? do |b| # Plexus::Edge structs - (b[:source] == e[:source]) and (b[:target] == e[:target]) + maybe_branches.create_branches_list(_branch_type).all? do |maybe_branch| + branches.any? do |branch| + maybe_branch == branch end end - all end alias has_branches? branches? # Returns true if actual branches are included in the specified set of branches # (no ordering criterion). @@ -290,20 +288,14 @@ # objects or as nodes pairs # @return [Boolean] # def branches_among?(*maybe_branches) list = maybe_branches.create_branches_list(_branch_type) - all = true - - # Branch objects are really Edge objects within Plexus, therefore - # cannot rely on #eql? to compare those structures and must drop - # down to the attributes. - branches.each do |e| # Plexus::Edge structs - all = list.any? do |b| # Jumoku::Branch structs - (b[:source] == e[:source]) and (b[:target] == e[:target]) + branches.all? do |branch| + list.any? do |maybe_branch| + branch == maybe_branch end end - all end alias has_branches_among? branches_among? # Number of nodes. #