Class: PageTemplate::LoopCommand

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

Overview

LoopCommand is a StackableCommand. It requires an opening:

% if variable %

or [% unless variable %].

variable is fetched from the namespace.

On execution, if variable is true, and non-empty, then its own namespace and passed to the loop commands. (a list is defined as an object that responds to :map)

If variable is true, but does not respond to :map, then the list is called once

% else %

may be specified, modifying LoopCommand to print out

Instance Attribute Summary

Attributes inherited from Command

#called_as

Instance Method Summary collapse

Methods inherited from StackableCommand

#end

Constructor Details

#initialize(called_as, value, iterators) ⇒ LoopCommand

% in variable %

or [% loop variable %]

Or [% in variable: name %]


402
403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'lib/PageTemplate/commands.rb', line 402

def initialize(called_as, value, iterators)
  @called_as = called_as
  @value     = value
  if iterators
    @iterators = iterators.strip.gsub(/\s+/, ' ').split
  else
    @iterators = nil
  end

  @switched  = false
  @commands  = BlockCommand.new
  @elseCommands = BlockCommand.new
  @in_else = false
end

Instance Method Details

#add(command) ⇒ Object

adds to @commands for the loop, or @elseCommands if 'else' has been passed.


425
426
427
428
429
430
431
# File 'lib/PageTemplate/commands.rb', line 425

def add(command)
  unless @in_else
    @commands.add command
  else
    @elseCommands.add command
  end
end

#elseObject

An 'else' defines a list of commands to call when the loop is empty.

Raises:

  • (ArgumentError)

418
419
420
421
422
# File 'lib/PageTemplate/commands.rb', line 418

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

#output(namespace) ⇒ Object

If variable is true, and non-empty, then @commands is printed once with each item in the list placed in its own namespace and passed to the loop commands. (a list is defined as an object that responds to :map)

If variable is true, but does not respond to :map, then the list is called once


439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
# File 'lib/PageTemplate/commands.rb', line 439

def output(namespace)

  vals = namespace.get(@value)
  ns = nil
  return @elseCommands.output(namespace) unless vals
  unless vals.respond_to?(:map) && vals.respond_to?(:length)
    vals = [vals]
  end

  return @elseCommands.output(namespace) if vals.empty?

  # Unfortunately, no "map_with_index"
  len = vals.length
  i = 1
  odd = true
  ns = Namespace.new(namespace)

  # Print the output of all the objects joined together.
  case
  when @iterators.nil? || @iterators.empty?
    vals.map { |item|
      ns.clear
      ns.object = item
      ns['__FIRST__'] = (i == 1) ? true : false
      ns['__LAST__'] = (i == len) ? true : false
      ns['__ODD__'] = odd
      ns['__INDEX__'] = i-1
      odd = ! odd
      i += 1
      @commands.output(ns)
    }.join('')
  when @iterators.size == 1
    iterator = @iterators[0]
    vals.map { |item|
      ns.clear_cache
      # We have an explicit iterator - don't set an object for it.
      # ns.object = item
      ns['__FIRST__'] = (i == 1) ? true : false
      ns['__LAST__'] = (i == len) ? true : false
      ns['__ODD__'] = odd
      ns['__INDEX__'] = i-1
      odd = ! odd
      ns[iterator] = item
      i += 1
      @commands.output(ns)
    }.join('')
  else
    vals.map { |list|
      ns.clear_cache
      ns['__FIRST__'] = (i == 1) ? true : false
      ns['__LAST__'] = (i == len) ? true : false
      ns['__ODD__'] = odd
      ns['__INDEX__'] = i-1
      @iterators.each_with_index do |iterator,index|
        ns[iterator] = list[index]
      end
      odd = ! odd
      i += 1
      @commands.output(ns)
    }.join('')
  end
end

#to_sObject


502
503
504
505
506
507
# File 'lib/PageTemplate/commands.rb', line 502

def to_s
  str = "[ Loop: #{@value} " + @commands.to_s
  str << " Else: " + @elseCommands.to_s if @elseCommands.length > 0
  str << ' ]'
  str
end