mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-15 08:50:45 -07:00
Fix mapping over procs
This commit is contained in:
@@ -101,11 +101,12 @@ module Liquid
|
|||||||
def map(input, property)
|
def map(input, property)
|
||||||
ary = [input].flatten
|
ary = [input].flatten
|
||||||
ary.map do |e|
|
ary.map do |e|
|
||||||
if e.respond_to?(:to_liquid)
|
e = e.call if e.is_a?(Proc)
|
||||||
e = e.to_liquid
|
e = e.to_liquid if e.respond_to?(:to_liquid)
|
||||||
end
|
|
||||||
|
|
||||||
if e.respond_to?('[]')
|
if property == "to_liquid"
|
||||||
|
e
|
||||||
|
elsif e.respond_to?(:[])
|
||||||
e[property]
|
e[property]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -6,6 +6,27 @@ class Filters
|
|||||||
include Liquid::StandardFilters
|
include Liquid::StandardFilters
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class TestThing
|
||||||
|
def initialize
|
||||||
|
@foo = 0
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_s
|
||||||
|
"woot: #{@foo}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_liquid
|
||||||
|
@foo += 1
|
||||||
|
self
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class TestDrop < Liquid::Drop
|
||||||
|
def test
|
||||||
|
"testfoo"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class StandardFiltersTest < Test::Unit::TestCase
|
class StandardFiltersTest < Test::Unit::TestCase
|
||||||
include Liquid
|
include Liquid
|
||||||
|
|
||||||
@@ -102,6 +123,18 @@ class StandardFiltersTest < Test::Unit::TestCase
|
|||||||
assert_equal "", Liquid::Template.parse('{{ "foo" | map: "inspect" }}').render
|
assert_equal "", Liquid::Template.parse('{{ "foo" | map: "inspect" }}').render
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_map_calls_to_liquid
|
||||||
|
t = TestThing.new
|
||||||
|
assert_equal "woot: 1", Liquid::Template.parse('{{ foo }}').render("foo" => t)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_map_over_proc
|
||||||
|
drop = TestDrop.new
|
||||||
|
p = Proc.new{ drop }
|
||||||
|
templ = '{{ procs | map: "test" }}'
|
||||||
|
assert_equal "testfoo", Liquid::Template.parse(templ).render("procs" => [p])
|
||||||
|
end
|
||||||
|
|
||||||
def test_date
|
def test_date
|
||||||
assert_equal 'May', @filters.date(Time.parse("2006-05-05 10:00:00"), "%B")
|
assert_equal 'May', @filters.date(Time.parse("2006-05-05 10:00:00"), "%B")
|
||||||
assert_equal 'June', @filters.date(Time.parse("2006-06-05 10:00:00"), "%B")
|
assert_equal 'June', @filters.date(Time.parse("2006-06-05 10:00:00"), "%B")
|
||||||
|
|||||||
Reference in New Issue
Block a user