mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-12 23:40:45 -07:00
Merge branch 'master' into recursive-parsing
This commit is contained in:
+14
-1
@@ -44,6 +44,10 @@ module Liquid
|
||||
true
|
||||
end
|
||||
|
||||
def inspect
|
||||
self.class.to_s
|
||||
end
|
||||
|
||||
def to_liquid
|
||||
self
|
||||
end
|
||||
@@ -54,7 +58,16 @@ module Liquid
|
||||
|
||||
# Check for method existence without invoking respond_to?, which creates symbols
|
||||
def self.invokable?(method_name)
|
||||
@invokable_methods ||= Set.new((public_instance_methods - Liquid::Drop.public_instance_methods).map(&:to_s))
|
||||
unless @invokable_methods
|
||||
# Ruby 1.8 compatibility: call to_s on method names (which are strings in 1.8, but already symbols in 1.9)
|
||||
blacklist = (Liquid::Drop.public_instance_methods + [:each]).map(&:to_s)
|
||||
if include?(Enumerable)
|
||||
blacklist += Enumerable.public_instance_methods.map(&:to_s)
|
||||
blacklist -= [:sort, :count, :first, :min, :max, :include?].map(&:to_s)
|
||||
end
|
||||
whitelist = [:to_liquid] + (public_instance_methods.map(&:to_s) - blacklist.map(&:to_s))
|
||||
@invokable_methods = Set.new(whitelist.map(&:to_s))
|
||||
end
|
||||
@invokable_methods.include?(method_name.to_s)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -81,7 +81,7 @@ module Liquid
|
||||
# Sort elements of the array
|
||||
# provide optional property with which to sort an array of hashes or drops
|
||||
def sort(input, property = nil)
|
||||
ary = [input].flatten
|
||||
ary = flatten_if_necessary(input)
|
||||
if property.nil?
|
||||
ary.sort
|
||||
elsif ary.first.respond_to?('[]') and !ary.first[property].nil?
|
||||
@@ -99,11 +99,14 @@ module Liquid
|
||||
|
||||
# map/collect on a given property
|
||||
def map(input, property)
|
||||
ary = [input].flatten
|
||||
if ary.first.respond_to?('[]') and !ary.first[property].nil?
|
||||
ary.map {|e| e[property] }
|
||||
elsif ary.first.respond_to?(property)
|
||||
ary.map {|e| e.send(property) }
|
||||
flatten_if_necessary(input).map do |e|
|
||||
e = e.call if e.is_a?(Proc)
|
||||
|
||||
if property == "to_liquid"
|
||||
e
|
||||
elsif e.respond_to?(:[])
|
||||
e[property]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -244,6 +247,17 @@ module Liquid
|
||||
|
||||
private
|
||||
|
||||
def flatten_if_necessary(input)
|
||||
ary = if input.is_a?(Array)
|
||||
input.flatten
|
||||
elsif input.kind_of?(Enumerable)
|
||||
input
|
||||
else
|
||||
[input].flatten
|
||||
end
|
||||
ary.map{ |e| e.respond_to?(:to_liquid) ? e.to_liquid : e }
|
||||
end
|
||||
|
||||
def to_number(obj)
|
||||
case obj
|
||||
when Float
|
||||
|
||||
Reference in New Issue
Block a user