Sha256: 0857277187a3bef3670eb184e2996076024cc73261261fb33d5484f1d632ee92

Contents?: true

Size: 1.71 KB

Versions: 3

Compression:

Stored size: 1.71 KB

Contents

module Locomotive
  module Liquid
    module Tags

      # 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 %}
      #

      class WithScope < Solid::Block

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

        SYMBOL_OPERATORS_REGEXP = /(\w+\.(#{OPERATORS.join('|')})){1}\s*\:/

        # register the tag
        tag_name :with_scope

        def initialize(tag_name, arguments_string, tokens, context = {})
          # convert symbol operators into valid ruby code
          arguments_string.gsub!(SYMBOL_OPERATORS_REGEXP, ':"\1" =>')

          super(tag_name, arguments_string, tokens, context)
        end

        def display(options = {}, &block)
          current_context.stack do
            current_context['with_scope'] = self.decode(options)
            yield
          end
        end

        protected

        def decode(options)
          HashWithIndifferentAccess.new.tap do |hash|
            options.each do |key, value|
              _key, _operator = key.to_s.split('.')

              # _slug instead of _permalink
              _key = '_slug' if _key == '_permalink'

              # key to h4s symbol
              _key = _key.to_s.to_sym.send(_operator.to_sym) if _operator

              hash[_key] = (case value
                # regexp inside a string
              when /^\/[^\/]*\/$/ then Regexp.new(value[1..-2])
              else
                value
              end)
            end
          end
        end
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
locomotive_cms-2.5.7 lib/locomotive/liquid/tags/with_scope.rb
locomotivecms-3.0.0.pre.alpha.2 lib/locomotive/previous_liquid/tags/with_scope.rb
locomotivecms-3.0.0.pre.alpha lib/locomotive/previous_liquid/tags/with_scope.rb