Sha256: 2bbb1532fc3c5491eb1a6b69b6cbcd1817dd270af2602dec50da261b8b66bfea

Contents?: true

Size: 1.65 KB

Versions: 54

Compression:

Stored size: 1.65 KB

Contents

module Dugway
  module Tags
    class Get < ::Liquid::Block
      Syntax = /((#{ Liquid::QuotedFragment })\s+)?(\w+)\s+from\s+(#{ Liquid::QuotedFragment }+)/

      def initialize(tag_name, markup, tokens)
        if markup =~ Syntax
          @number_to_get = $1.present? ? $2 : nil
          @variable_name = $3
          @collection_name = $4

          @attributes = {}
          markup.scan(Liquid::TagAttributes) { |key, value|
            @attributes[key] = value
          }
        else
          raise SyntaxError.new("Syntax Error in tag 'get' - Valid syntax: get [number] [items] from [collection] order:[order]")
        end

        super
      end

      def render(context)
        @context = context

        @number_to_get = if context[@number_to_get].present?
          context[@number_to_get]
        elsif @number_to_get.present?
          @number_to_get.to_i
        elsif @attributes['limit'].present?
          if context[@attributes['limit']].present?
            context[@attributes['limit']]
          else
            @attributes['limit'].to_i
          end
        else
          nil
        end

        @order = context[@attributes['order']].present? ? context[@attributes['order']] : @attributes['order']

        context.stack do
          context['internal'] = {
            'per_page' => @number_to_get,
            'order' => @order,
            'page' => nil
          }

          context[@variable_name] = context[@collection_name]

          raise ArgumentError.new("Cannot get array '#{ @collection_name }'. Not found.")  if context[@variable_name].total_entries.nil?

          render_all @nodelist, context
        end
      end
    end
  end
end

Version data entries

54 entries across 54 versions & 1 rubygems

Version Path
dugway-1.0.14 lib/dugway/liquid/tags/get.rb
dugway-1.0.13 lib/dugway/liquid/tags/get.rb
dugway-1.0.12 lib/dugway/liquid/tags/get.rb
dugway-1.0.10 lib/dugway/liquid/tags/get.rb
dugway-1.0.9 lib/dugway/liquid/tags/get.rb
dugway-1.0.8 lib/dugway/liquid/tags/get.rb
dugway-1.0.7 lib/dugway/liquid/tags/get.rb
dugway-1.0.6 lib/dugway/liquid/tags/get.rb
dugway-1.0.5 lib/dugway/liquid/tags/get.rb
dugway-1.0.4 lib/dugway/liquid/tags/get.rb
dugway-1.0.3 lib/dugway/liquid/tags/get.rb
dugway-1.0.2 lib/dugway/liquid/tags/get.rb
dugway-1.0.1 lib/dugway/liquid/tags/get.rb
dugway-1.0.0 lib/dugway/liquid/tags/get.rb
dugway-0.12.3 lib/dugway/liquid/tags/get.rb
dugway-0.12.2 lib/dugway/liquid/tags/get.rb
dugway-0.12.1 lib/dugway/liquid/tags/get.rb
dugway-0.12.0 lib/dugway/liquid/tags/get.rb
dugway-0.11.4 lib/dugway/liquid/tags/get.rb
dugway-0.11.3 lib/dugway/liquid/tags/get.rb