Sha256: a5b3252c891bd6b9404a039107571f9948ac25829e9723107ae4545a1478cb5d

Contents?: true

Size: 780 Bytes

Versions: 2

Compression:

Stored size: 780 Bytes

Contents

module Trailblazer
  class Finder
    module Utils
      module DeepLocate
        def self.deep_locate(comparator, object, result = [])
          if object.is_a?(::Enumerable)
            if object.any? { |value| match_comparator?(value, comparator, object) }
              result.push object
            end
            (object.respond_to?(:values) ? object.values : object.entries).each do |value|
              deep_locate(comparator, value, result)
            end
          end

          result
        end

        def self.match_comparator?(value, comparator, object)
          if object.is_a?(::Hash)
            key, value = value
          else
            key = nil
          end

          comparator.call(key, value, object)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
trailblazer-finder-0.1.4 lib/trailblazer/finder/utils/deep_locate.rb
trailblazer-finder-0.1.3 lib/trailblazer/finder/utils/deep_locate.rb