From 9be88e1de33b446565e6d36b62dfb591da2473ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20BEAU?= Date: Mon, 27 Jan 2020 16:23:05 +0100 Subject: [PATCH] Refactor code to make it more clear --- .../steam/liquid/tags/concerns/attributes.rb | 16 +++++++--------- lib/locomotive/steam/liquid/tags/with_scope.rb | 10 +++++----- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/lib/locomotive/steam/liquid/tags/concerns/attributes.rb b/lib/locomotive/steam/liquid/tags/concerns/attributes.rb index c0e0fe35..4ab25ec6 100644 --- a/lib/locomotive/steam/liquid/tags/concerns/attributes.rb +++ b/lib/locomotive/steam/liquid/tags/concerns/attributes.rb @@ -42,22 +42,20 @@ def context_evaluate(vals) def evaluate_attributes(context, lax: false) @attributes = HashWithIndifferentAccess.new.tap do |hash| attributes.each do |key, value| - # evaluate the value if possible before casting it - _value = value.is_a?(::Liquid::VariableLookup) ? context.evaluate(value) : value - - hash[cast_value(context, key, lax: lax)] = cast_value(context, _value, lax: lax) + hash[evaluate_value(context, key, lax: lax)] = evaluate_value(context, value, lax: lax) end end end - def cast_value(context, value, lax: false) + def evaluate_value(context, value, lax: false) case value - when Array then value.map { |_value| cast_value(context, _value) } - when Hash then value.transform_values { |_value| cast_value(context, _value) } - else + when ::Liquid::VariableLookup _value = context.evaluate(value) lax && _value.nil? ? value&.name : _value - _value.respond_to?(:_id) ? _value.send(:_source) : _value + when Array then value.map { |_value| evaluate_value(context, _value) } + when Hash then value.transform_values { |_value| evaluate_value(context, _value) } + else + value end end diff --git a/lib/locomotive/steam/liquid/tags/with_scope.rb b/lib/locomotive/steam/liquid/tags/with_scope.rb index ab15a7e1..5628f800 100644 --- a/lib/locomotive/steam/liquid/tags/with_scope.rb +++ b/lib/locomotive/steam/liquid/tags/with_scope.rb @@ -77,7 +77,7 @@ def render(context) def parse_attribute(value) case value when StrictRegexpFragment - # let the cast_value attribute create the Regexp (done during the rendering phase) + # let the evaluate_value attribute create the Regexp (done during the rendering phase) value when ArrayFragment $1.split(',').map { |_value| parse_attribute(_value) } @@ -86,12 +86,12 @@ def parse_attribute(value) end end - def cast_value(context, value, lax: false) - case value - when Array then value.map { |_value| cast_value(context, _value) } + def evaluate_value(context, value, lax: false) + _value = super + case _value when StrictRegexpFragment then create_regexp($1, $2) else - super + _value.respond_to?(:_id) ? _value.send(:_source) : _value end end