class Liquid::Case

Constants

Syntax
WhenSyntax

Public Class Methods

new(tag_name, markup, tokens) click to toggle source
Calls superclass method
# File lib/liquid/tags/case.rb, line 6
def initialize(tag_name, markup, tokens)
  @blocks = []

  if markup =~ Syntax
    @left = $1
  else
    raise SyntaxError.new("Syntax Error in tag 'case' - Valid syntax: case [condition]")
  end

  super
end

Public Instance Methods

render(context) click to toggle source
# File lib/liquid/tags/case.rb, line 30
def render(context)
  context.stack do
    execute_else_block = true

    output = ''
    @blocks.each do |block|
      if block.else?
        return render_all(block.attachment, context) if execute_else_block
      elsif block.evaluate(context)
        execute_else_block = false
        output << render_all(block.attachment, context)
      end
    end
    output
  end
end
unknown_tag(tag, markup, tokens) click to toggle source
Calls superclass method Liquid::Block#unknown_tag
# File lib/liquid/tags/case.rb, line 18
def unknown_tag(tag, markup, tokens)
  @nodelist = []
  case tag
  when 'when'
    record_when_condition(markup)
  when 'else'
    record_else_condition(markup)
  else
    super
  end
end

Private Instance Methods

record_else_condition(markup) click to toggle source
# File lib/liquid/tags/case.rb, line 64
def record_else_condition(markup)

  if not markup.strip.empty?
    raise SyntaxError.new("Syntax Error in tag 'case' - Valid else condition: {% else %} (no parameters) ")
  end

  block = ElseCondition.new
  block.attach(@nodelist)
  @blocks << block
end
record_when_condition(markup) click to toggle source
# File lib/liquid/tags/case.rb, line 49
def record_when_condition(markup)
  while markup
    # Create a new nodelist and assign it to the new block
    if not markup =~ WhenSyntax
      raise SyntaxError.new("Syntax Error in tag 'case' - Valid when condition: {% when [condition] [or condition2...] %} ")
    end

    markup = $2

    block = Condition.new(@left, '==', $1)
    block.attach(@nodelist)
    @blocks.push(block)
  end
end