class Liquid::Case

Constants

Syntax
WhenSyntax

Public Class Methods

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

  if markup =~ Syntax
    @left = Expression.parse($1)
  else
    raise SyntaxError.new(options[:locale].t("errors.syntax.case".freeze))
  end
end

Public Instance Methods

nodelist() click to toggle source
# File lib/liquid/tags/case.rb, line 24
def nodelist
  @blocks.map(&:attachment)
end
parse(tokens) click to toggle source
# File lib/liquid/tags/case.rb, line 17
def parse(tokens)
  body = BlockBody.new
  while parse_body(body, tokens)
    body = @blocks.last.attachment
  end
end
render(context) click to toggle source
# File lib/liquid/tags/case.rb, line 39
def render(context)
  context.stack do
    execute_else_block = true

    output = ''
    @blocks.each do |block|
      if block.else?
        return block.attachment.render(context) if execute_else_block
      elsif block.evaluate(context)
        execute_else_block = false
        output << block.attachment.render(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 28
def unknown_tag(tag, markup, tokens)
  case tag
  when 'when'.freeze
    record_when_condition(markup)
  when 'else'.freeze
    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 74
def record_else_condition(markup)
  unless markup.strip.empty?
    raise SyntaxError.new(options[:locale].t("errors.syntax.case_invalid_else".freeze))
  end

  block = ElseCondition.new
  block.attach(BlockBody.new)
  @blocks << block
end
record_when_condition(markup) click to toggle source
# File lib/liquid/tags/case.rb, line 58
def record_when_condition(markup)
  body = BlockBody.new

  while markup
    unless markup =~ WhenSyntax
      raise SyntaxError.new(options[:locale].t("errors.syntax.case_invalid_when".freeze))
    end

    markup = $2

    block = Condition.new(@left, '=='.freeze, Expression.parse($1))
    block.attach(body)
    @blocks << block
  end
end