Class: PageTemplate::IfCommand

Inherits:
StackableCommand show all
Defined in:
lib/PageTemplate/commands.rb

Overview

IfCommand is a StackableCommand. It requires an opening:

% if variable %

or [% unless variable %].

On execution, if variable is true, then the contents of IfCommand’s @commands is printed.

% else %

may be specified for either if or unless, after which

objects are added to @falseCommands, and are printed if variable is false, instead.

Instance Attribute Summary

Attributes inherited from Command

#called_as

Instance Method Summary collapse

Methods inherited from StackableCommand

#end

Constructor Details

#initialize(called_as, value) ⇒ IfCommand

command must match “if <value>” or “unless <value>”



317
318
319
320
321
322
323
324
325
# File 'lib/PageTemplate/commands.rb', line 317

def initialize(called_as, value)
  @value = value
  @called_as = called_as
  @trueCommands = []
  @trueCommands << [value,BlockCommand.new]
  @falseCommands = BlockCommand.new
  @in_else = (called_as == 'unless')
  @switched = false
end

Instance Method Details

#add(command) ⇒ Object

Add the command to the @trueCommands or @falseCommands block, depending on if the command is an ‘if’ or ‘unless’ and if ‘else’ has been called.



329
330
331
332
333
334
335
# File 'lib/PageTemplate/commands.rb', line 329

def add(command)
  unless @in_else
    @trueCommands.last.last.add command
  else
    @falseCommands.add command
  end
end

#elseObject

an ‘else’ will modify the command, switching it from adding to

Raises:

  • (ArgumentError)


343
344
345
346
347
# File 'lib/PageTemplate/commands.rb', line 343

def else
  raise ArgumentError, "More than one 'else' to IfCommand" if @switched
  @in_else = ! @in_else
  @switched = true
end

#elsif(value) ⇒ Object

an elseif will create a new condition.

Raises:

  • (Argumentrror)


337
338
339
340
# File 'lib/PageTemplate/commands.rb', line 337

def elsif(value)
  raise Argumentrror, "'elsif' cannot be passed after 'else' or in an 'unless'" if @switched || @in_else
  @trueCommands << [value,BlockCommand.new]
end

#output(namespace = nil) ⇒ Object

If @value is true within the context of namespace, then print the output of @trueCommands. Otherwise, print the output of



351
352
353
354
355
356
357
# File 'lib/PageTemplate/commands.rb', line 351

def output(namespace=nil)
  val = ''
  @trueCommands.each do |val,commands|
    return commands.output(namespace) if namespace.true?(val)
  end
  @falseCommands.output(namespace)
end

#to_sObject



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# File 'lib/PageTemplate/commands.rb', line 358

def to_s
  str = '['
  if @called_as == 'if'
    str = ' If'
    label = ' If'
    str += @trueCommands.map { |val,commands|
      s = "#{label} (#{val}) #{commands}"
      label = ' Elsif'
    }.join('')
    if @falseCommands.length > 0
      str << " else: #{@falseCommands}"
    end
  else
    str = "[ Unless (#{@value}): #{@falseCommands}"
    if @trueCommands.length > 0
      str << " else: #{@trueCommands}"
    end
  end
  str << ' ]'
  str
end