Class: Luoma::CaseTag

Inherits:
Markup
  • Object
show all
Defined in:
lib/luoma/tags/case.rb,
sig/luoma/tags/case.rbs

Constant Summary collapse

END_CASE_BLOCK =

Returns:

  • (Set[String])
Set["endcase", "when", "else"]

Instance Attribute Summary

Attributes inherited from Markup

#blank, #tag_name, #token

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Markup

#block_scope, #partial, #template_scope

Constructor Details

#initialize(token, tag_name, expression, blocks) ⇒ CaseTag

(t_token, String, Expression, Array, ElseBlock?) -> void

Parameters:



34
35
36
37
38
39
40
41
# File 'lib/luoma/tags/case.rb', line 34

def initialize(token, tag_name, expression, blocks)
  super(token)
  @tag_name = tag_name
  @expression = expression
  @blocks = blocks
  @blank = blocks.all?(&:blank)
  @blocks.each(&:filter_strings) if @blank
end

Class Method Details

.parse(token, tag_name, parser) ⇒ Markup

(t_token, String, Parser) -> Markup

Parameters:

  • token (t_token)
  • tag_name (String)
  • parser (Parser)

Returns:



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/luoma/tags/case.rb', line 8

def self.parse(token, tag_name, parser)
  parser.expect_expression
  expression = parser.parse_expression
  parser.carry_whitespace_control
  parser.eat(:token_tag_end)

  # Junk between `{% case %}` and first `{% when %}`.
  parser.eat(:token_text) if parser.kind == :token_text

  blocks = [] #: Array[WhenBlock|ElseBlock]
  blocks << WhenBlock.parse("when", parser) while parser.tag?("when")

  if parser.tag?("else")
    else_token = parser.eat_empty_tag("else")
    blocks << ElseBlock.new(
      else_token,
      "else",
      parser.parse_block(stop: END_CASE_BLOCK)
    )
  end

  parser.eat_empty_tag("endcase")
  new(token, tag_name, expression, blocks)
end

Instance Method Details

#children(static_context) ⇒ Object

Signature:

  • (RenderContext) -> Array[Markup]



63
64
65
# File 'lib/luoma/tags/case.rb', line 63

def children(static_context)
  @blocks
end

#expressionsObject

Signature:

  • () -> Array[Expression]



68
69
70
# File 'lib/luoma/tags/case.rb', line 68

def expressions
  [@expression]
end

#render(context, buffer) ⇒ Object

Signature:

  • (RenderContext, String) -> void



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/luoma/tags/case.rb', line 44

def render(context, buffer)
  left = @expression.evaluate(context)

  @blocks.each do |block|
    if block.is_a?(ElseBlock)
      return Luoma.render_block(block.block, context, buffer)
    else
      block.right.each do |expr|
        right = expr.evaluate(context)
        if (right.is_a?(PredicateFunction) && context.env.truthy?(right.call(context, left), context)) ||
           context.env.eq?(left, right, context, expr.span)
          return Luoma.render_block(block.block, context, buffer)
        end
      end
    end
  end
end