Class: Command::Results::Formatter

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/command-set/results.rb

Overview

The end of the Results train. Formatter objects are supposed to output to the user events that they receive from their presenters. To simplify this process, a number of common IO functions are delegated to an IO object - usually Command::raw_stdout.

This class in particular is pretty quiet - probably not helpful for everyday use. Of course, for some purposes, singleton methods might be very useful

Direct Known Subclasses

StrategyFormatter, TextFormatter, XMLFormatter

Defined Under Namespace

Classes: FormatAdvisor

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ Formatter

Returns a new instance of Formatter.



461
462
463
464
465
# File 'lib/command-set/results.rb', line 461

def initialize(io)
  @out_to = io
  @advisor = FormatAdvisor.new(self)
  @advice = {:list => [], :item => [], :output => []}
end

Instance Attribute Details

#adviceObject (readonly)

Returns the value of attribute advice.



479
480
481
# File 'lib/command-set/results.rb', line 479

def advice
  @advice
end

Class Method Details

.inherited(sub) ⇒ Object



491
492
493
494
495
496
# File 'lib/command-set/results.rb', line 491

def self.inherited(sub)
  sub.extend Forwardable
  sub.class_eval do
    def_delegators :@out_to, :p, :puts, :print, :printf, :putc, :write, :write_nonblock, :flush
  end
end

Instance Method Details

#apply_advice(item) ⇒ Object



467
468
469
470
471
472
473
474
475
476
477
# File 'lib/command-set/results.rb', line 467

def apply_advice(item)
  type = List === item ? :list : :item

  item.options[:format_advice] = 
    @advice[type].inject(default_advice(type)) do |advice, advisor|
    result = advisor[item]
    break if result == :DONE
    advice.merge!(result) if Hash === result
    advice
    end
end

#closed_begin_list(list) ⇒ Object

Presenter callback: a list opened, tree order



511
# File 'lib/command-set/results.rb', line 511

def closed_begin_list(list); end

#closed_end_list(list) ⇒ Object

Presenter callback: an list closed, tree order



517
# File 'lib/command-set/results.rb', line 517

def closed_end_list(list); end

#closed_item(item) ⇒ Object

Presenter callback: an item added, tree order



514
# File 'lib/command-set/results.rb', line 514

def closed_item(item); end

#default_advice(type) ⇒ Object



485
486
487
# File 'lib/command-set/results.rb', line 485

def default_advice(type)
  {}
end

#finishObject

Presenter callback: output is done



520
# File 'lib/command-set/results.rb', line 520

def finish; end

#notify(msg, item) ⇒ Object



428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# File 'lib/command-set/results.rb', line 428

def notify(msg, item)
  if msg == :start
    start
    return
  end
  if msg == :done
    finish
    return
  end

  apply_advice(item)

  if List === item
    case msg
    when :saw_begin
      saw_begin_list(item)
    when :saw_end
      saw_end_list(item)
    when :arrive
      closed_begin_list(item)
    when :leave
      closed_end_list(item)
    end
  else
    case msg
    when :arrive
      closed_item(item)
    when :saw
      saw_item(item)
    end
  end
end

#receive_advice(&block) ⇒ Object



481
482
483
# File 'lib/command-set/results.rb', line 481

def receive_advice(&block)
  @advisor.instance_eval(&block)
end

#saw_begin_list(list) ⇒ Object

Presenter callback: a list has just started



502
# File 'lib/command-set/results.rb', line 502

def saw_begin_list(list); end

#saw_end_list(list) ⇒ Object

Presenter callback: a list has just ended



508
# File 'lib/command-set/results.rb', line 508

def saw_end_list(list); end

#saw_item(item) ⇒ Object

Presenter callback: an item has just been added



505
# File 'lib/command-set/results.rb', line 505

def saw_item(item); end

#startObject

Presenter callback: output is beginning



499
# File 'lib/command-set/results.rb', line 499

def start; end