Sha256: 44031ba2a829919f9d624282313c491ae4f7dfdfcfa94c6d4fd622e683b187b4

Contents?: true

Size: 1.62 KB

Versions: 9

Compression:

Stored size: 1.62 KB

Contents

module Locomotive
  module Steam
    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*\:/o

          # register the tag
          tag_name :with_scope

          def initialize(name, markup, options)
            # convert symbol operators into valid ruby code
            markup.gsub!(SYMBOL_OPERATORS_REGEXP, ':"\1" =>')

            super(name, markup, options)
          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|
                # _slug instead of _permalink
                _key = key.to_s == '_permalink' ? '_slug' : key.to_s

                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
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
locomotivecms_steam-1.0.0.rc2 lib/locomotive/steam/liquid/tags/with_scope.rb
locomotivecms_steam-1.0.0.rc1 lib/locomotive/steam/liquid/tags/with_scope.rb
locomotivecms_steam-1.0.0.pre.beta.3 lib/locomotive/steam/liquid/tags/with_scope.rb
locomotivecms_steam-1.0.0.pre.beta.2 lib/locomotive/steam/liquid/tags/with_scope.rb
locomotivecms_steam-1.0.0.pre.beta.1 lib/locomotive/steam/liquid/tags/with_scope.rb
locomotivecms_steam-1.0.0.pre.alpha.3 lib/locomotive/steam/liquid/tags/with_scope.rb
locomotivecms_steam-1.0.0.pre.alpha.2 lib/locomotive/steam/liquid/tags/with_scope.rb
locomotivecms_steam-1.0.0.pre.alpha.1 lib/locomotive/steam/liquid/tags/with_scope.rb
locomotivecms_steam-1.0.0.pre.alpha lib/locomotive/steam/liquid/tags/with_scope.rb