Class: Liquid::Case

Inherits:
Block show all
Defined in:
lib/liquid/tags/case.rb

Defined Under Namespace

Classes: ParseTreeVisitor

Constant Summary collapse

Syntax =
/(#{QuotedFragment})/o
WhenSyntax =
/(#{QuotedFragment})(?:(?:\s+or\s+|\s*\,\s*)(#{QuotedFragment}.*))?/om

Constants inherited from Block

Block::MAX_DEPTH

Instance Attribute Summary collapse

Attributes inherited from Tag

#line_number, #parse_context, #tag_name

Instance Method Summary collapse

Methods inherited from Block

#blank?, #block_delimiter, #block_name, #render

Methods inherited from Tag

#blank?, disable_tags, #name, parse, #raw, #render

Methods included from ParserSwitching

#parse_with_selected_parser

Constructor Details

#initialize(tag_name, markup, options) ⇒ Case

Returns a new instance of Case.


10
11
12
13
14
15
16
17
18
19
# File 'lib/liquid/tags/case.rb', line 10

def initialize(tag_name, markup, options)
  super
  @blocks = []

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

Instance Attribute Details

#blocksObject (readonly)

Returns the value of attribute blocks


8
9
10
# File 'lib/liquid/tags/case.rb', line 8

def blocks
  @blocks
end

#leftObject (readonly)

Returns the value of attribute left


8
9
10
# File 'lib/liquid/tags/case.rb', line 8

def left
  @left
end

Instance Method Details

#nodelistObject


26
27
28
# File 'lib/liquid/tags/case.rb', line 26

def nodelist
  @blocks.map(&:attachment)
end

#parse(tokens) ⇒ Object


21
22
23
24
# File 'lib/liquid/tags/case.rb', line 21

def parse(tokens)
  body = BlockBody.new
  body = @blocks.last.attachment while parse_body(body, tokens)
end

#render_to_output_buffer(context, output) ⇒ Object


41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/liquid/tags/case.rb', line 41

def render_to_output_buffer(context, output)
  execute_else_block = true

  @blocks.each do |block|
    if block.else?
      block.attachment.render_to_output_buffer(context, output) if execute_else_block
    elsif block.evaluate(context)
      execute_else_block = false
      block.attachment.render_to_output_buffer(context, output)
    end
  end

  output
end

#unknown_tag(tag, markup, tokens) ⇒ Object


30
31
32
33
34
35
36
37
38
39
# File 'lib/liquid/tags/case.rb', line 30

def unknown_tag(tag, markup, tokens)
  case tag
  when 'when'
    record_when_condition(markup)
  when 'else'
    record_else_condition(markup)
  else
    super
  end
end