implement a cache within the where filter (#6868)

Merge pull request 6868
This commit is contained in:
ashmaroli 2018-04-12 21:06:53 +05:30 committed by jekyllbot
parent beada92da8
commit 053b522a82
1 changed files with 16 additions and 4 deletions

View File

@ -167,11 +167,23 @@ module Jekyll
#
# Returns the filtered array of objects
def where(input, property, value)
return input if property.nil? || value.nil?
return input unless input.respond_to?(:select)
input = input.values if input.is_a?(Hash)
input.select do |object|
Array(item_property(object, property)).map!(&:to_s).include?(value.to_s)
end || []
input = input.values if input.is_a?(Hash)
input_id = input.hash
# implement a hash based on method parameters to cache the end-result
# for given parameters.
@where_filter_cache ||= {}
@where_filter_cache[input_id] ||= {}
@where_filter_cache[input_id][property] ||= {}
# stash or retrive results to return
@where_filter_cache[input_id][property][value] ||= begin
input.select do |object|
Array(item_property(object, property)).map!(&:to_s).include?(value.to_s)
end || []
end
end
# Filters an array of objects against an expression