merge PR #65 (with_scope supports regular expression switches: i, m, x)

did committed Aug 09, 2016
commit b439507dfba750b42462190638273250e8303330
Showing 2 changed files with 27 additions and 1 deletions
locomotive/steam/liquid/tags/with_scope.rb b/lib/locomotive/steam/liquid/tags/with_scope.rb +12 -1
@@ @@ -20,6 +20,12 @@ module Locomotive
SYMBOL_OPERATORS_REGEXP = /(\w+\.(#{OPERATORS.join('|')})){1}\s*\:/o
+ REGEX_OPTIONS = {
+ 'i' => Regexp::IGNORECASE,
+ 'm' => Regexp::MULTILINE,
+ 'x' => Regexp::EXTENDED
+ }
+
# register the tag
tag_name :with_scope
@@ @@ -54,7 +60,12 @@ module Locomotive
def cast_value(value)
case value
when Array then value.map { |_value| cast_value(_value) }
- when /^\/[^\/]*\/$/ then Regexp.new(value[1..-2])
+ when /^\/([^\/]*)\/([imx]+)?$/
+ _value, options_str = $1, $2
+ options = options_str.blank? ? nil : options_str.split('').uniq.inject(0) do |_options, letter|
+ _options |= REGEX_OPTIONS[letter]
+ end
+ Regexp.new(_value, options)
else
value.respond_to?(:_id) ? value.send(:_source) : value
end
spec/unit/liquid/tags/with_scope_spec.rb +15 -0
@@ @@ -34,6 +34,13 @@ describe Locomotive::Steam::Liquid::Tags::WithScope do
end
+ describe 'decode regexps with case-insensitive' do
+
+ let(:source) { "{% with_scope title: /like this/ix %}{% assign conditions = with_scope %}{% endwith_scope %}" }
+ it { expect(conditions['title']).to eq(/like this/ix) }
+
+ end
+
describe 'decode content entry' do
let(:entry) {
@@ @@ -71,6 +78,14 @@ describe Locomotive::Steam::Liquid::Tags::WithScope do
end
+ describe 'decode a regexp stored in a context variable, with case-insensitive' do
+
+ let(:assigns) { { 'my_regexp' => '/^hello world/ix' } }
+ let(:source) { "{% with_scope title: my_regexp %}{% assign conditions = with_scope %}{% endwith_scope %}" }
+ it { expect(conditions['title']).to eq(/^hello world/ix) }
+
+ end
+
describe 'allow order_by option' do
let(:source) { "{% with_scope order_by:\'name DESC\' %}{% assign conditions = with_scope %}{% endwith_scope %}" }