Class: Liquid2::CaseTag

Inherits:
Tag show all
Defined in:
lib/liquid2/nodes/tags/case.rb

Overview

The standard case tag.

Constant Summary collapse

END_BLOCK =
Set["endcase", "when", "else"]
WHEN_DELIM =
Set[:token_comma, :token_or]

Instance Attribute Summary

Attributes inherited from Node

#blank, #token

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Tag

#name

Methods inherited from Node

#block_scope, #partial_scope, #render_with_disabled_tag_check, #template_scope

Constructor Details

#initialize(token, expression, whens, default) ⇒ CaseTag

Returns a new instance of CaseTag.



55
56
57
58
59
60
61
# File 'lib/liquid2/nodes/tags/case.rb', line 55

def initialize(token, expression, whens, default)
  super(token)
  @expression = expression
  @whens = whens
  @default = default
  @blank = whens.map(&:blank).all? && (!default || default.blank)
end

Class Method Details

.parse(token, parser) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/liquid2/nodes/tags/case.rb', line 11

def self.parse(token, parser)
  expression = parser.parse_primary
  parser.carry_whitespace_control
  parser.eat(:token_tag_end)
  parser.eat(:token_other) if parser.current_kind == :token_other

  whens = [] # : Array[MultiEqualBlock]
  default = nil # : Block?

  whens << parse_when(parser, expression) while parser.tag?("when")

  if parser.tag?("else")
    parser.eat_empty_tag("else")
    default = parser.parse_block(END_BLOCK)
  end

  parser.eat_empty_tag("endcase")
  new(token, expression, whens, default)
end

.parse_when(parser, expr) ⇒ MultiEqualBlock

Returns:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/liquid2/nodes/tags/case.rb', line 32

def self.parse_when(parser, expr)
  parser.eat(:token_tag_start)
  parser.skip_whitespace_control
  token = parser.eat(:token_tag_name)

  parser.next if parser.current_kind == :token_comma

  args = [] # : Array[Expression]

  loop do
    args << parser.parse_primary(infix: false)
    break unless WHEN_DELIM.member?(parser.current_kind)

    parser.next
  end

  parser.carry_whitespace_control
  parser.eat(:token_tag_end)

  block = parser.parse_block(END_BLOCK)
  MultiEqualBlock.new(token, expr, args, block)
end

Instance Method Details

#children(_static_context, include_partials: true) ⇒ Object



75
76
77
78
79
80
# File 'lib/liquid2/nodes/tags/case.rb', line 75

def children(_static_context, include_partials: true)
  # @type var nodes: Array[Node]
  nodes = @whens.clone
  nodes << (@default || raise) if @default
  nodes
end

#expressionsObject



82
# File 'lib/liquid2/nodes/tags/case.rb', line 82

def expressions = [@expression]

#render(context, buffer) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/liquid2/nodes/tags/case.rb', line 63

def render(context, buffer)
  rendered = false
  index = 0
  while (node = @whens[index])
    rendered_ = node.render(context, buffer)
    rendered ||= rendered_
    index += 1
  end

  (@default || raise).render(context, buffer) if @default && !rendered
end