Skip to content

Commit

Permalink
Optimize Jekyll::Filters#item_property (#7696)
Browse files Browse the repository at this point in the history
Merge pull request 7696
  • Loading branch information
ashmaroli committed Feb 6, 2020
1 parent 4406256 commit 8bb76c8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .rubocop.yml
Expand Up @@ -79,6 +79,8 @@ Metrics/MethodLength:
Severity: error
Metrics/ModuleLength:
Max: 240
Exclude:
- lib/jekyll/filters.rb
Metrics/ParameterLists:
Max: 4
Metrics/PerceivedComplexity:
Expand Down
27 changes: 18 additions & 9 deletions lib/jekyll/filters.rb
Expand Up @@ -356,15 +356,24 @@ def item_property(item, property)
@item_property_cache ||= {}
@item_property_cache[property] ||= {}
@item_property_cache[property][item] ||= begin
if item.respond_to?(:to_liquid)
property.to_s.split(".").reduce(item.to_liquid) do |subvalue, attribute|
parse_sort_input(subvalue[attribute])
end
elsif item.respond_to?(:data)
parse_sort_input(item.data[property.to_s])
else
parse_sort_input(item[property.to_s])
end
property = property.to_s
property = if item.respond_to?(:to_liquid)
read_liquid_attribute(item.to_liquid, property)
elsif item.respond_to?(:data)
item.data[property]
else
item[property]
end

parse_sort_input(property)
end
end

def read_liquid_attribute(liquid_data, property)
return liquid_data[property] unless property.include?(".")

property.split(".").reduce(liquid_data) do |data, key|
data.respond_to?(:[]) && data[key]
end
end

Expand Down

0 comments on commit 8bb76c8

Please sign in to comment.