lib/active_enumerable/finder.rb in active_enumerable-1.0.0 vs lib/active_enumerable/finder.rb in active_enumerable-1.1.0
- old
+ new
@@ -36,22 +36,35 @@
# Finder.new(record).is_of(age: 10)
# #=> true
#
# @param [Hash] conditions
# @return [true, false]
- def is_of(conditions={})
+ def is_of(conditions = {})
conditions.all? do |col, match|
- if match.is_a? Hash
+ case match
+ when Proc
+ proc_match(col, match)
+ when Hash
hash_match(col, match)
- elsif match.is_a? Array
+ when Array
array_match(col, match)
else
compare(col, match)
end
end
end
private
+
+ def proc_match(col, match)
+ return @method_caller.instance_exec(&match) unless col
+ next_record = @method_caller.call(col)
+ if next_record.is_a? Array
+ next_record.all? { |record| Finder.new(record).is_of({nil => match}) }
+ else
+ MethodCaller.new(next_record).instance_exec(&match)
+ end
+ end
def hash_match(col, match)
next_record = @method_caller.call(col)
if next_record.is_a? Array
next_record.any? { |record| Finder.new(record).is_of(match) }