Class: PageTemplate::LoopCommand
- Inherits:
-
StackableCommand
- Object
- Command
- StackableCommand
- PageTemplate::LoopCommand
- 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
Instance Method Summary collapse
-
#add(command) ⇒ Object
adds to @commands for the loop, or @elseCommands if ‘else’ has been passed.
-
#else ⇒ Object
An ‘else’ defines a list of commands to call when the loop is empty.
-
#initialize(called_as, value, iterators) ⇒ LoopCommand
constructor
- % in variable %
-
or [% loop variable %] Or [% in variable: name %].
-
#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. - #to_s ⇒ Object
Methods inherited from StackableCommand
Constructor Details
#initialize(called_as, value, iterators) ⇒ LoopCommand
- % in variable %
-
or [% loop variable %]
Or [% in variable: name %]
358 359 360 361 362 363 364 365 366 |
# File 'lib/PageTemplate/commands.rb', line 358 def initialize(called_as, value, iterators) @called_as = called_as @value = value @iterators = iterators.strip.gsub(/\s+/,' ').split if iterators @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.
376 377 378 379 380 381 382 |
# File 'lib/PageTemplate/commands.rb', line 376 def add(command) unless @in_else @commands.add command else @elseCommands.add command end end |
#else ⇒ Object
An ‘else’ defines a list of commands to call when the loop is empty.
369 370 371 372 373 |
# File 'lib/PageTemplate/commands.rb', line 369 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
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 |
# File 'lib/PageTemplate/commands.rb', line 390 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_s ⇒ Object
453 454 455 456 457 458 |
# File 'lib/PageTemplate/commands.rb', line 453 def to_s str = "[ Loop: #{@value} " + @commands.to_s str << " Else: " + @elseCommands.to_s if @elseCommands.length > 0 str << ' ]' str end |