-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: extract the code to evaluate the attributes of tag into a module
- Loading branch information
Showing
3 changed files
with
82 additions
and
58 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
lib/locomotive/steam/liquid/tags/concerns/attributes_evaluator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
module Locomotive | ||
module Steam | ||
module Liquid | ||
module Tags | ||
module Concerns | ||
|
||
# Evaluates the attributes parsed the AttributesParser | ||
module AttributesEvaluator | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
# Regexps are allowed as strings | ||
RegexpFragment = /\/([^\/]+)\/([imx]+)?/o.freeze | ||
StrictRegexpFragment = /\A#{RegexpFragment}\z/o.freeze | ||
|
||
REGEX_OPTIONS = { | ||
'i' => Regexp::IGNORECASE, | ||
'm' => Regexp::MULTILINE, | ||
'x' => Regexp::EXTENDED | ||
}.freeze | ||
end | ||
|
||
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 | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters