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.



466
467
468
469
470
# File 'lib/command-set/results.rb', line 466

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.



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

def advice
  @advice
end

Class Method Details

.inherited(sub) ⇒ Object



496
497
498
499
500
501
# File 'lib/command-set/results.rb', line 496

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



472
473
474
475
476
477
478
479
480
481
482
# File 'lib/command-set/results.rb', line 472

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



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

def closed_begin_list(list); end

#closed_end_list(list) ⇒ Object

Presenter callback: an list closed, tree order



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

def closed_end_list(list); end

#closed_item(item) ⇒ Object

Presenter callback: an item added, tree order



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

def closed_item(item); end

#default_advice(type) ⇒ Object



490
491
492
# File 'lib/command-set/results.rb', line 490

def default_advice(type)
  {}
end

#finishObject

Presenter callback: output is done



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

def finish; end

#notify(msg, item) ⇒ Object



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
460
461
462
463
464
# File 'lib/command-set/results.rb', line 433

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



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

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

#saw_begin_list(list) ⇒ Object

Presenter callback: a list has just started



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

def saw_begin_list(list); end

#saw_end_list(list) ⇒ Object

Presenter callback: a list has just ended



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

def saw_end_list(list); end

#saw_item(item) ⇒ Object

Presenter callback: an item has just been added



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

def saw_item(item); end

#startObject

Presenter callback: output is beginning



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

def start; end