Skip to content

Commit

Permalink
Refactor code to make it more clear
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienbeau committed Jan 27, 2020
1 parent d77feb5 commit 9be88e1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
16 changes: 7 additions & 9 deletions lib/locomotive/steam/liquid/tags/concerns/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 5 additions & 5 deletions lib/locomotive/steam/liquid/tags/with_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand All @@ -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

Expand Down

0 comments on commit 9be88e1

Please sign in to comment.