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, #strict2_mode?, #strict_parse_with_error_mode_fallback

Constructor Details

#initialize(tag_name, markup, options) ⇒ Case

Returns a new instance of Case.



31
32
33
34
35
# File 'lib/liquid/tags/case.rb', line 31

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

Instance Attribute Details

#blocksObject (readonly)

Returns the value of attribute blocks.



29
30
31
# File 'lib/liquid/tags/case.rb', line 29

def blocks
  @blocks
end

#leftObject (readonly)

Returns the value of attribute left.



29
30
31
# File 'lib/liquid/tags/case.rb', line 29

def left
  @left
end

Instance Method Details

#nodelistObject



50
51
52
# File 'lib/liquid/tags/case.rb', line 50

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

#parse(tokens) ⇒ Object



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

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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/liquid/tags/case.rb', line 65

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



54
55
56
57
58
59
60
61
62
63
# File 'lib/liquid/tags/case.rb', line 54

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