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

Modules: Styler Classes: FormatAdvisor

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(out = nil, err = nil) ⇒ Formatter

Returns a new instance of Formatter.



505
506
507
508
509
510
# File 'lib/command-set/results.rb', line 505

def initialize(out = nil, err = nil)
  @out_to = out || ::Command::raw_stdout
  @err_to = err || ::Command::raw_stderr
  @advisor = FormatAdvisor.new(self)
  @advice = {:list => [], :item => [], :output => []}
end

Instance Attribute Details

#adviceObject (readonly)

Returns the value of attribute advice.



527
528
529
# File 'lib/command-set/results.rb', line 527

def advice
  @advice
end

Class Method Details

.inherited(sub) ⇒ Object



539
540
541
542
543
544
# File 'lib/command-set/results.rb', line 539

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



512
513
514
515
516
517
518
519
520
521
522
523
524
525
# File 'lib/command-set/results.rb', line 512

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
    if Hash === result
      advice.merge(result)
    else
      advice
    end
    end
end

#closed_begin_list(list) ⇒ Object

Presenter callback: a list opened, tree order



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

def closed_begin_list(list); end

#closed_end_list(list) ⇒ Object

Presenter callback: an list closed, tree order



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

def closed_end_list(list); end

#closed_item(item) ⇒ Object

Presenter callback: an item added, tree order



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

def closed_item(item); end

#default_advice(type) ⇒ Object



533
534
535
# File 'lib/command-set/results.rb', line 533

def default_advice(type)
  {}
end

#finishObject

Presenter callback: output is done



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

def finish; end

#notify(msg, item) ⇒ Object



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
501
502
503
# File 'lib/command-set/results.rb', line 472

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



529
530
531
# File 'lib/command-set/results.rb', line 529

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

#saw_begin_list(list) ⇒ Object

Presenter callback: a list has just started



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

def saw_begin_list(list); end

#saw_end_list(list) ⇒ Object

Presenter callback: a list has just ended



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

def saw_end_list(list); end

#saw_item(item) ⇒ Object

Presenter callback: an item has just been added



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

def saw_item(item); end

#startObject

Presenter callback: output is beginning



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

def start; end