mirror of
https://github.com/kemko/liquid.git
synced 2026-01-02 00:05:42 +03:00
Compare commits
2 Commits
fix-flaky-
...
context-dr
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
273a9f7a97 | ||
|
|
fbe14cd0b7 |
@@ -128,13 +128,13 @@ module Liquid
|
|||||||
|
|
||||||
# Join elements of the array with certain character between them
|
# Join elements of the array with certain character between them
|
||||||
def join(input, glue = ' ')
|
def join(input, glue = ' ')
|
||||||
InputIterator.new(input).join(glue)
|
InputIterator.new(input, context).join(glue)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Sort elements of the array
|
# Sort elements of the array
|
||||||
# provide optional property with which to sort an array of hashes or drops
|
# provide optional property with which to sort an array of hashes or drops
|
||||||
def sort(input, property = nil)
|
def sort(input, property = nil)
|
||||||
ary = InputIterator.new(input)
|
ary = InputIterator.new(input, context)
|
||||||
|
|
||||||
return [] if ary.empty?
|
return [] if ary.empty?
|
||||||
|
|
||||||
@@ -154,7 +154,7 @@ module Liquid
|
|||||||
# Sort elements of an array ignoring case if strings
|
# Sort elements of an array ignoring case if strings
|
||||||
# provide optional property with which to sort an array of hashes or drops
|
# provide optional property with which to sort an array of hashes or drops
|
||||||
def sort_natural(input, property = nil)
|
def sort_natural(input, property = nil)
|
||||||
ary = InputIterator.new(input)
|
ary = InputIterator.new(input, context)
|
||||||
|
|
||||||
return [] if ary.empty?
|
return [] if ary.empty?
|
||||||
|
|
||||||
@@ -174,7 +174,7 @@ module Liquid
|
|||||||
# Filter the elements of an array to those with a certain property value.
|
# Filter the elements of an array to those with a certain property value.
|
||||||
# By default the target is any truthy value.
|
# By default the target is any truthy value.
|
||||||
def where(input, property, target_value = nil)
|
def where(input, property, target_value = nil)
|
||||||
ary = InputIterator.new(input)
|
ary = InputIterator.new(input, context)
|
||||||
|
|
||||||
if ary.empty?
|
if ary.empty?
|
||||||
[]
|
[]
|
||||||
@@ -196,7 +196,7 @@ module Liquid
|
|||||||
# Remove duplicate elements from an array
|
# Remove duplicate elements from an array
|
||||||
# provide optional property with which to determine uniqueness
|
# provide optional property with which to determine uniqueness
|
||||||
def uniq(input, property = nil)
|
def uniq(input, property = nil)
|
||||||
ary = InputIterator.new(input)
|
ary = InputIterator.new(input, context)
|
||||||
|
|
||||||
if property.nil?
|
if property.nil?
|
||||||
ary.uniq
|
ary.uniq
|
||||||
@@ -213,13 +213,13 @@ module Liquid
|
|||||||
|
|
||||||
# Reverse the elements of an array
|
# Reverse the elements of an array
|
||||||
def reverse(input)
|
def reverse(input)
|
||||||
ary = InputIterator.new(input)
|
ary = InputIterator.new(input, context)
|
||||||
ary.reverse
|
ary.reverse
|
||||||
end
|
end
|
||||||
|
|
||||||
# map/collect on a given property
|
# map/collect on a given property
|
||||||
def map(input, property)
|
def map(input, property)
|
||||||
InputIterator.new(input).map do |e|
|
InputIterator.new(input, context).map do |e|
|
||||||
e = e.call if e.is_a?(Proc)
|
e = e.call if e.is_a?(Proc)
|
||||||
|
|
||||||
if property == "to_liquid"
|
if property == "to_liquid"
|
||||||
@@ -236,7 +236,7 @@ module Liquid
|
|||||||
# Remove nils within an array
|
# Remove nils within an array
|
||||||
# provide optional property with which to check for nil
|
# provide optional property with which to check for nil
|
||||||
def compact(input, property = nil)
|
def compact(input, property = nil)
|
||||||
ary = InputIterator.new(input)
|
ary = InputIterator.new(input, context)
|
||||||
|
|
||||||
if property.nil?
|
if property.nil?
|
||||||
ary.compact
|
ary.compact
|
||||||
@@ -280,7 +280,7 @@ module Liquid
|
|||||||
unless array.respond_to?(:to_ary)
|
unless array.respond_to?(:to_ary)
|
||||||
raise ArgumentError, "concat filter requires an array argument"
|
raise ArgumentError, "concat filter requires an array argument"
|
||||||
end
|
end
|
||||||
InputIterator.new(input).concat(array)
|
InputIterator.new(input, context).concat(array)
|
||||||
end
|
end
|
||||||
|
|
||||||
# prepend a string to another
|
# prepend a string to another
|
||||||
@@ -432,6 +432,8 @@ module Liquid
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
attr_reader :context
|
||||||
|
|
||||||
def raise_property_error(property)
|
def raise_property_error(property)
|
||||||
raise Liquid::ArgumentError, "cannot select the property '#{property}'"
|
raise Liquid::ArgumentError, "cannot select the property '#{property}'"
|
||||||
end
|
end
|
||||||
@@ -460,7 +462,8 @@ module Liquid
|
|||||||
class InputIterator
|
class InputIterator
|
||||||
include Enumerable
|
include Enumerable
|
||||||
|
|
||||||
def initialize(input)
|
def initialize(input, context)
|
||||||
|
@context = context
|
||||||
@input = if input.is_a?(Array)
|
@input = if input.is_a?(Array)
|
||||||
input.flatten
|
input.flatten
|
||||||
elsif input.is_a?(Hash)
|
elsif input.is_a?(Hash)
|
||||||
@@ -499,6 +502,7 @@ module Liquid
|
|||||||
|
|
||||||
def each
|
def each
|
||||||
@input.each do |e|
|
@input.each do |e|
|
||||||
|
e.context = @context if e.respond_to?(:context=)
|
||||||
yield(e.respond_to?(:to_liquid) ? e.to_liquid : e)
|
yield(e.respond_to?(:to_liquid) ? e.to_liquid : e)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -179,6 +179,11 @@ class DropsTest < Minitest::Test
|
|||||||
assert_equal(' carrot ', output)
|
assert_equal(' carrot ', output)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_context_drop_array_with_map
|
||||||
|
output = Liquid::Template.parse(' {{ contexts | map: "bar" }} ').render!('contexts' => [ContextDrop.new, ContextDrop.new], 'bar' => "carrot")
|
||||||
|
assert_equal(' carrotcarrot ', output)
|
||||||
|
end
|
||||||
|
|
||||||
def test_nested_context_drop
|
def test_nested_context_drop
|
||||||
output = Liquid::Template.parse(' {{ product.context.foo }} ').render!('product' => ProductDrop.new, 'foo' => "monkey")
|
output = Liquid::Template.parse(' {{ product.context.foo }} ').render!('product' => ProductDrop.new, 'foo' => "monkey")
|
||||||
assert_equal(' monkey ', output)
|
assert_equal(' monkey ', output)
|
||||||
|
|||||||
Reference in New Issue
Block a user