Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move code to AttributesParser so it can be reused #240

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion lib/locomotive/steam/liquid/tags/concerns/attributes_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,17 @@ module Concerns
# approach (see the SimpleAttributesParser for instance)
module AttributesParser
extend ActiveSupport::Concern


RegexpFragment = /\/([^\/]+)\/([imx]+)?/o.freeze
StrictRegexpFragment = /\A#{RegexpFragment}\z/o.freeze
REGEX_OPTIONS = {
'i' => Regexp::IGNORECASE,
'm' => Regexp::MULTILINE,
'x' => Regexp::EXTENDED
}.freeze



def parse_markup(markup)
parser = self.class.current_parser

Expand All @@ -21,6 +31,42 @@ def parse_markup(markup)
AstProcessor.new.process(ast)
end


def evaluate_attributes(context)
@attributes = context[attributes_var_name] || {} if attributes_var_name.present?

attributes.inject({}) do |memo, (key, value)|
# _slug instead of _permalink
_key = key.to_s == '_permalink' ? '_slug' : key.to_s

memo.merge({ _key => evaluate_attribute(context, value) })
end
end

def evaluate_attribute(context, value)
case value
when Array
value.map { |v| evaluate_attribute(context, v) }
when Hash
Hash[value.map { |k, v| [k.to_s, evaluate_attribute(context, v)] }]
when StrictRegexpFragment
create_regexp($1, $2)
when ::Liquid::VariableLookup
evaluated_value = context.evaluate(value)
evaluated_value.respond_to?(:_id) ? evaluated_value.send(:_source) : evaluate_attribute(context, evaluated_value)
else
value
end
end

def create_regexp(value, unparsed_options)
options = unparsed_options.blank? ? nil : unparsed_options.split('').uniq.inject(0) do |_options, letter|
_options |= REGEX_OPTIONS[letter]
end
Regexp.new(value, options)
end


class_methods do
def current_parser
(@current_parser ||= build_parser).tap do |parser|
Expand Down
42 changes: 0 additions & 42 deletions lib/locomotive/steam/liquid/tags/with_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,8 @@ class WithScope < ::Liquid::Block
include Concerns::AttributesParser

# Regexps are allowed as strings
RegexpFragment = /\/([^\/]+)\/([imx]+)?/o.freeze
StrictRegexpFragment = /\A#{RegexpFragment}\z/o.freeze

SingleVariable = /\A\s*([a-zA-Z_0-9]+)\s*\z/om.freeze

REGEX_OPTIONS = {
'i' => Regexp::IGNORECASE,
'm' => Regexp::MULTILINE,
'x' => Regexp::EXTENDED
}.freeze

OPERATORS = %w(all exists gt gte in lt lte ne nin size near within)

SYMBOL_OPERATORS_REGEXP = /(\w+\.(#{OPERATORS.join('|')})){1}\s*\:/o
Expand Down Expand Up @@ -68,39 +59,6 @@ def render(context)

protected

def evaluate_attributes(context)
@attributes = context[attributes_var_name] || {} if attributes_var_name.present?

attributes.inject({}) do |memo, (key, value)|
# _slug instead of _permalink
_key = key.to_s == '_permalink' ? '_slug' : key.to_s

memo.merge({ _key => evaluate_attribute(context, value) })
end
end

def evaluate_attribute(context, value)
case value
when Array
value.map { |v| evaluate_attribute(context, v) }
when Hash
Hash[value.map { |k, v| [k.to_s, evaluate_attribute(context, v)] }]
when StrictRegexpFragment
create_regexp($1, $2)
when ::Liquid::VariableLookup
evaluated_value = context.evaluate(value)
evaluated_value.respond_to?(:_id) ? evaluated_value.send(:_source) : evaluate_attribute(context, evaluated_value)
else
value
end
end

def create_regexp(value, unparsed_options)
options = unparsed_options.blank? ? nil : unparsed_options.split('').uniq.inject(0) do |_options, letter|
_options |= REGEX_OPTIONS[letter]
end
Regexp.new(value, options)
end
end

::Liquid::Template.register_tag('with_scope'.freeze, WithScope)
Expand Down