Class: Locomotive::Steam::Liquid::Tags::WithScope

Inherits:
Liquid::Block
  • Object
show all
Includes:
Concerns::Attributes
Defined in:
lib/locomotive/steam/liquid/tags/with_scope.rb

Overview

Filter a collection

Usage:

with_scope main_developer: ‘John Doe’, providers.in: [‘acme’], started_at.le: today, active: true %

{% for project in contents.projects %}
  {{ project.name }}
{% endfor %}

endwith_scope %

Constant Summary collapse

ArrayFragment =

Regexps and Arrays are allowed

/\[(\s*(#{::Liquid::QuotedFragment},\s*)*#{::Liquid::QuotedFragment}\s*)\]/o.freeze
RegexpFragment =
/\/([^\/]+)\/([imx]+)?/o.freeze
TagAttributes =

a slight different from the Shopify implementation because we allow stuff like ‘started_at.le`

/([a-zA-Z_0-9\.]+)\s*\:\s*(#{ArrayFragment}|#{RegexpFragment}|#{::Liquid::QuotedFragment})/o.freeze
SingleVariable =
/(#{::Liquid::VariableSignature}+)/om.freeze
REGEX_OPTIONS =
{
  'i' => Regexp::IGNORECASE,
  'm' => Regexp::MULTILINE,
  'x' => Regexp::EXTENDED
}.freeze

Instance Attribute Summary collapse

Attributes included from Concerns::Attributes

#raw_attributes

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, options) ⇒ WithScope

Returns a new instance of WithScope.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/locomotive/steam/liquid/tags/with_scope.rb', line 37

def initialize(tag_name, markup, options)
  super

  # simple hash?
  parse_attributes(markup) { |value| parse_attribute(value) }

  if attributes.empty? && markup =~ SingleVariable
    # alright, maybe we'vot got a single variable built
    # with the Action liquid tag instead?
    @attributes_var_name = Regexp.last_match(1)
  end

  if attributes.empty? && attributes_var_name.blank?
    raise ::Liquid::SyntaxError.new("Syntax Error in 'with_scope' - Valid syntax: with_scope <name_1>: <value_1>, ..., <name_n>: <value_n>")
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



35
36
37
# File 'lib/locomotive/steam/liquid/tags/with_scope.rb', line 35

def attributes
  @attributes
end

#attributes_var_nameObject (readonly)

Returns the value of attribute attributes_var_name.



35
36
37
# File 'lib/locomotive/steam/liquid/tags/with_scope.rb', line 35

def attributes_var_name
  @attributes_var_name
end

Instance Method Details

#render(context) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/locomotive/steam/liquid/tags/with_scope.rb', line 54

def render(context)
  context.stack do
    context['with_scope'] = self.evaluate_attributes(context)

    # for now, no content type is assigned to this with_scope
    context['with_scope_content_type'] = false

    super
  end
end