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, #raise_tag_never_closed, raise_unknown_tag, #render

Methods inherited from Tag

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

Methods included from ParserSwitching

#parse_with_selected_parser, #strict_parse_with_error_mode_fallback

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 = parse_expression(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



34
35
36
# File 'lib/liquid/tags/case.rb', line 34

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

#parse(tokens) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/liquid/tags/case.rb', line 21

def parse(tokens)
  body = case_body = new_body
  body = @blocks.last.attachment while parse_body(body, tokens)
  @blocks.reverse_each do |condition|
    body = condition.attachment
    unless body.frozen?
      body.remove_blank_strings if blank?
      body.freeze
    end
  end
  case_body.freeze
end

#render_to_output_buffer(context, output) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/liquid/tags/case.rb', line 49

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
      next
    end

    result = Liquid::Utils.to_liquid_value(
      block.evaluate(context)
    )

    if result
      execute_else_block = false
      block.attachment.render_to_output_buffer(context, output)
    end
  end

  output
end

#unknown_tag(tag, markup, tokens) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/liquid/tags/case.rb', line 38

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