Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
karreiro committed Dec 2, 2024
1 parent f1cac0c commit adde645
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 8 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ end
gemspec

gem "base64"
gem "webrick"

group :benchmark, :test do
gem 'benchmark-ips'
Expand Down
39 changes: 35 additions & 4 deletions example/server/example_servlet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def paragraph(p)

class Servlet < LiquidServlet
def index
{ 'date' => Time.now }
{ 'date' => Time.now, 'products' => products_list }
end

def products
Expand All @@ -29,11 +29,42 @@ def products

private

class Name < Liquid::Drop
attr_reader :raw, :origin

def initialize(raw, origin)
super
@raw = raw
@origin = origin
end
end

class Price < Liquid::Drop
attr_reader :value, :unit

def initialize(value, unit = 'USD')
super
@value = value
@unit = unit
end
end

class Product < Liquid::Drop
attr_reader :name, :price, :description

def initialize(name, origin, price, description)
super
@name = Name.new(name, origin)
@price = Price.new(price)
@description = description
end
end

def products_list
[
{ 'name' => 'Arbor Draft', 'price' => 39900, 'description' => 'the *arbor draft* is a excellent product' },
{ 'name' => 'Arbor Element', 'price' => 40000, 'description' => 'the *arbor element* rocks for freestyling' },
{ 'name' => 'Arbor Diamond', 'price' => 59900, 'description' => 'the *arbor diamond* is a made up product because im obsessed with arbor and have no creativity' }
Product.new('Draft', 'Arbor', 39900, 'the *arbor draft* is a excellent product'),
Product.new('Element', 'Arbor', 40000, 'the *arbor element* rocks for freestyling'),
Product.new('Diamond', 'Arbor', 59900, 'the *arbor diamond* is a made up product because im obsessed with arbor and have no creativity')
]
end

Expand Down
8 changes: 7 additions & 1 deletion example/server/templates/index.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,11 @@

<p>It is {{date}}</p>

<br>

<p>Check out the <a href="/products">Products</a> screen </p>
{{ products.size }}


<br><br>

{{ products | sum: 'price.value' }}
13 changes: 13 additions & 0 deletions lib/liquid/drop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ def to_s

alias_method :[], :invoke_drop

def dig(keys)
if self.class.invokable?(keys)
# For backward compatibility - historically some drops may have
# properties containing dots in their names (e.g. "foo.bar").
# We first attempt to look up the exact key before falling
# back to treating dots as nested property access.
return invoke_drop(keys)
end

keys = keys.to_s.split('.') unless keys.is_a?(Array)
keys.inject(self) { |obj, key| obj.respond_to?(:[]) ? obj[key] : nil }
end

# Check for method existence without invoking respond_to?, which creates symbols
def self.invokable?(method_name)
invokable_methods.include?(method_name.to_s)
Expand Down
37 changes: 34 additions & 3 deletions lib/liquid/standardfilters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,12 @@ def sort(input, property = nil)
ary.sort do |a, b|
nil_safe_compare(a, b)
end
elsif ary.all? { |el| el.respond_to?(:dig) }
begin
ary.sort { |a, b| nil_safe_compare(a.dig(property), b.dig(property)) }
rescue TypeError
raise_property_error(property)
end
elsif ary.all? { |el| el.respond_to?(:[]) }
begin
ary.sort { |a, b| nil_safe_compare(a[property], b[property]) }
Expand Down Expand Up @@ -405,6 +411,12 @@ def sort_natural(input, property = nil)
ary.sort do |a, b|
nil_safe_casecmp(a, b)
end
elsif ary.all? { |el| el.respond_to?(:dig) }
begin
ary.sort { |a, b| nil_safe_casecmp(a.dig(property), b.dig(property)) }
rescue TypeError
raise_property_error(property)
end
elsif ary.all? { |el| el.respond_to?(:[]) }
begin
ary.sort { |a, b| nil_safe_casecmp(a[property], b[property]) }
Expand Down Expand Up @@ -495,7 +507,11 @@ def uniq(input, property = nil)
[]
else
ary.uniq do |item|
item[property]
if item.respond_to?(:dig)
item.dig(property)
else
item[property]
end
rescue TypeError
raise_property_error(property)
rescue NoMethodError
Expand Down Expand Up @@ -530,6 +546,9 @@ def map(input, property)

if property == "to_liquid"
e
elsif e.respond_to?(:dig)
r = e.dig(property)
r.is_a?(Proc) ? r.call : r
elsif e.respond_to?(:[])
r = e[property]
r.is_a?(Proc) ? r.call : r
Expand All @@ -555,7 +574,11 @@ def compact(input, property = nil)
[]
else
ary.reject do |item|
item[property].nil?
if item.respond_to?(:dig)
item.dig(property).nil?
else
item[property].nil?
end
rescue TypeError
raise_property_error(property)
rescue NoMethodError
Expand Down Expand Up @@ -928,6 +951,8 @@ def sum(input, property = nil)
values_for_sum = ary.map do |item|
if property.nil?
item
elsif item.respond_to?(:dig)
item.dig(property)
elsif item.respond_to?(:[])
item[property]
else
Expand Down Expand Up @@ -955,7 +980,13 @@ def filter_array(input, property, target_value, method)

ary.public_send(method) do |item|
if target_value.nil?
item[property]
if item.respond_to?(:dig)
item.dig(property)
else
item[property]
end
elsif item.respond_to?(:dig)
item.dig(property) == target_value
else
item[property] == target_value
end
Expand Down

0 comments on commit adde645

Please sign in to comment.