lib/cql/map_reduce.rb in cql-1.0.1 vs lib/cql/map_reduce.rb in cql-1.1.0

- old
+ new

@@ -1,23 +1,44 @@ require 'cql/dsl' require 'cql/feature_filters' require 'cql/sso_filters' +require 'cql/dsl' module CQL class MapReduce + extend Dsl + def self.gather_objects(current_object, target_classes, filters) gathered_objects = Array.new.tap { |gathered_objects| collect_all_in(target_classes, current_object, gathered_objects) } if filters filters.each do |filter| if filter.is_a?(Proc) - gathered_objects.select!(&filter) + gathered_objects = filter_with_proc(gathered_objects, filter) + elsif filter.is_a?(Hash) + filter.keys.each do |filtered_class| + clazz = determine_class(filtered_class) + + gathered_objects = gathered_objects.select do |object| + if object.is_a?(clazz) + if filter[filtered_class].is_a?(Proc) + filter[filtered_class].call(object) + else + # Must be a predefined filter otherwise + !filter_with_predefined([object], filter[filtered_class]).empty? + end + else + true + end + end + end else - gathered_objects = filter.execute(gathered_objects) + # Must be a predefined filter otherwise + gathered_objects = filter_with_predefined(gathered_objects, filter) end end end gathered_objects @@ -27,9 +48,17 @@ class << self private + + def filter_with_proc(objects, filter) + objects.select(&filter) + end + + def filter_with_predefined(objects, filter) + filter.execute(objects) + end # Recursively gathers all objects of the given class(es) found in the passed object (including itself). def collect_all_in(targeted_classes, current_object, accumulated_objects) accumulated_objects << current_object if targeted_classes.any? { |targeted_class| current_object.is_a?(targeted_class) }