Class: Dugway::Tags::Get

Inherits:
Liquid::Block
  • Object
show all
Defined in:
lib/dugway/liquid/tags/get.rb

Constant Summary collapse

Syntax =
/((#{ Liquid::QuotedFragment })\s+)?(\w+)\s+from\s+(#{ Liquid::QuotedFragment }+)/

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ Get

Returns a new instance of Get.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/dugway/liquid/tags/get.rb', line 6

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

Instance Method Details

#render(context) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/dugway/liquid/tags/get.rb', line 23

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