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.



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

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.



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



55
56
57
# File 'lib/liquid/tags/case.rb', line 55

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

#parse(tokens) ⇒ Object



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

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



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/liquid/tags/case.rb', line 70

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



59
60
61
62
63
64
65
66
67
68
# File 'lib/liquid/tags/case.rb', line 59

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