Class: Liquid::Case

Inherits:
DecisionBlock show all
Defined in:
lib/liquid/standardtags.rb

Constant Summary collapse

Syntax =
/(#{QuotedFragment})/
WhenSyntax =
/(#{QuotedFragment})/

Instance Attribute Summary

Attributes inherited from Tag

#nodelist

Instance Method Summary collapse

Methods inherited from DecisionBlock

#equal_variables, #interpret_condition

Methods inherited from Block

#block_delimiter, #block_name, #create_variable, #parse

Methods inherited from Tag

#name, #parse

Constructor Details

#initialize(markup, tokens) ⇒ Case

Returns a new instance of Case.



223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/liquid/standardtags.rb', line 223

def initialize(markup, tokens)
  @nodelists = []
  @else_nodelist = []
  
  super

  if markup =~ Syntax
    @left = $1
  else
    raise SyntaxError.new("Syntax Error in tag 'case' - Valid syntax: case [condition]")
  end
end

Instance Method Details

#end_tagObject



236
237
238
# File 'lib/liquid/standardtags.rb', line 236

def end_tag
  push_nodelist
end

#push_nodelistObject



259
260
261
262
263
264
265
# File 'lib/liquid/standardtags.rb', line 259

def push_nodelist
  if @right
    # only push the nodelist if there was actually a when condition stated before. 
    # we discard all tokens between the case and the first when condition this way...
    @nodelists << [@right, @nodelist] 
  end
end

#render(context) ⇒ Object



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/liquid/standardtags.rb', line 267

def render(context)
  output = []
  run_else_block = true
  
  @nodelists.each do |right, nodelist|
    if equal_variables(context[@left], context[right])
      run_else_block = false
      context.stack do          
        output += render_all(nodelist, context)
      end
    end
  end            
  
  if run_else_block
    context.stack do          
      output += render_all(@else_nodelist, context)       
    end
  end
  
  output.to_s
end

#unknown_tag(tag, params, tokens) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/liquid/standardtags.rb', line 240

def unknown_tag(tag, params, tokens)
  case tag 
  when 'when'
    if params =~ WhenSyntax          
      push_nodelist
      @right = $1
      @nodelist = [] 
    else
      raise SyntaxError.new("Syntax Error in tag 'case' - Valid when condition: when [condition] ")
    end
  when 'else'
    push_nodelist
    @right = nil
    @else_nodelist = @nodelist = [] 
  else
    super
  end
end