lib/abstractivator/trees/tree_compare.rb in abstractivator-0.0.23 vs lib/abstractivator/trees/tree_compare.rb in abstractivator-0.0.24

- old
+ new

@@ -1,7 +1,8 @@ require 'active_support/core_ext/object/deep_dup' require 'abstractivator/trees/block_collector' +require 'abstractivator/proc_ext' require 'sourcify' require 'delegate' require 'set' module Abstractivator @@ -10,18 +11,21 @@ SetMask = Struct.new(:items, :get_key) def set_mask(items, get_key) SetMask.new(items, get_key) end + # Compares a tree to a mask. + # Returns a diff of where the tree differs from the mask. + # Ignores parts of the tree not specified in the mask. def tree_compare(tree, mask, path=[], index=nil) if mask == [:*] && tree.is_a?(Enumerable) [] elsif mask == :+ && tree != :__missing__ [] elsif mask == :- && tree != :__missing__ [diff(path, tree, :__absent__)] - elsif mask.respond_to?(:call) + elsif mask.callable? are_equivalent = mask.call(tree) are_equivalent ? [] : [diff(path, tree, mask)] else case mask when Hash @@ -107,10 +111,10 @@ def diff(path, tree, mask) {path: path_string(path), tree: tree, mask: massage_mask_for_diff(mask)} end def massage_mask_for_diff(mask) - if mask.respond_to?(:call) + if mask.callable? massaged = :__predicate__ begin massaged = mask.to_source rescue Exception => e raise unless e.class.name.start_with?('Sourcify')