Skip to content

Commit

Permalink
rubocop: fix code style
Browse files Browse the repository at this point in the history
  • Loading branch information
ayastreb committed Jun 7, 2016
1 parent 2f95517 commit 5f2bb5d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 33 deletions.
1 change: 0 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ AllCops:
- lib/jekyll/configuration.rb
- lib/jekyll/convertible.rb
- lib/jekyll/document.rb
- lib/jekyll/filters.rb
- lib/jekyll/regenerator.rb
- lib/jekyll/renderer.rb
- lib/jekyll/static_file.rb
Expand Down
77 changes: 45 additions & 32 deletions lib/jekyll/filters.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'uri'
require 'json'
require 'date'
require 'liquid'
require "uri"
require "json"
require "date"
require "liquid"

module Jekyll
module Filters
Expand Down Expand Up @@ -56,7 +56,7 @@ def scssify(input)
#
# Returns the given filename or title as a lowercase URL String.
# See Utils.slugify for more detail.
def slugify(input, mode=nil)
def slugify(input, mode = nil)
Utils.slugify(input, :mode => mode)
end

Expand Down Expand Up @@ -118,7 +118,7 @@ def date_to_rfc822(date)
#
# Returns the escaped String.
def xml_escape(input)
input.to_s.encode(:xml => :attr).gsub(/\A"|"\Z/, "")
input.to_s.encode(:xml => :attr).gsub(%r!\A"|"\Z!, "")
end

# CGI escape a string for use in a URL. Replaces any special characters
Expand All @@ -133,7 +133,7 @@ def xml_escape(input)
#
# Returns the escaped String.
def cgi_escape(input)
CGI::escape(input)
CGI.escape(input)
end

# URI escape a string.
Expand Down Expand Up @@ -180,7 +180,7 @@ def array_to_sentence_string(array)
when 2
"#{array[0]} #{connector} #{array[1]}"
else
"#{array[0...-1].join(', ')}, #{connector} #{array[-1]}"
"#{array[0...-1].join(", ")}, #{connector} #{array[-1]}"
end
end

Expand All @@ -203,11 +203,14 @@ def jsonify(input)
# "items" => [...] } # all the items where `property` == "larry"
def group_by(input, property)
if groupable?(input)
input.group_by do |item|
item_property(item, property).to_s
end.inject([]) do |memo, i|
memo << { "name" => i.first, "items" => i.last, "size" => i.last.size }
end
input.group_by { |item| item_property(item, property).to_s }
.each_with_object([]) do |item, array|
array << {
"name" => item.first,
"items" => item.last,
"size" => item.last.size
}
end
else
input
end
Expand All @@ -223,7 +226,9 @@ def group_by(input, property)
def where(input, property, value)
return input unless input.is_a?(Enumerable)
input = input.values if input.is_a?(Hash)
input.select { |object| Array(item_property(object, property)).map(&:to_s).include?(value.to_s) }
input.select do |object|
Array(item_property(object, property)).map(&:to_s).include?(value.to_s)
end
end

# Filters an array of objects against an expression
Expand Down Expand Up @@ -255,33 +260,21 @@ def where_exp(input, variable, expression)
# Returns the filtered array of objects
def sort(input, property = nil, nils = "first")
if input.nil?
raise ArgumentError.new("Cannot sort a null object.")
raise ArgumentError, "Cannot sort a null object."
end
if property.nil?
input.sort
else
case
when nils == "first"
if nils == "first"
order = - 1
when nils == "last"
elsif nils == "last"
order = + 1
else
raise ArgumentError.new("Invalid nils order: " \
"'#{nils}' is not a valid nils order. It must be 'first' or 'last'.")
raise ArgumentError, "Invalid nils order: " \
"'#{nils}' is not a valid nils order. It must be 'first' or 'last'."
end

input.sort do |apple, orange|
apple_property = item_property(apple, property)
orange_property = item_property(orange, property)

if !apple_property.nil? && orange_property.nil?
- order
elsif apple_property.nil? && !orange_property.nil?
+ order
else
apple_property <=> orange_property
end
end
sort_input(input, property, order)
end
end

Expand Down Expand Up @@ -332,6 +325,22 @@ def inspect(input)
xml_escape(input.inspect)
end

private
def sort_input(input, property, order)
input.sort do |apple, orange|
apple_property = item_property(apple, property)
orange_property = item_property(orange, property)

if !apple_property.nil? && orange_property.nil?
- order
elsif apple_property.nil? && !orange_property.nil?
+ order
else
apple_property <=> orange_property
end
end
end

private
def time(input)
case input
Expand All @@ -349,10 +358,12 @@ def time(input)
end.localtime
end

private
def groupable?(element)
element.respond_to?(:group_by)
end

private
def item_property(item, property)
if item.respond_to?(:to_liquid)
item.to_liquid[property.to_s]
Expand All @@ -363,6 +374,7 @@ def item_property(item, property)
end
end

private
def as_liquid(item)
case item
when Hash
Expand All @@ -386,6 +398,7 @@ def as_liquid(item)
end

# Parse a string to a Liquid Condition
private
def parse_condition(exp)
parser = Liquid::Parser.new(exp)
left_expr = parser.expression
Expand Down

0 comments on commit 5f2bb5d

Please sign in to comment.