Class: Command::Results::Presenter

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

Overview

Gets item and list events from Collectors, and emits two kinds of events to Formatters:

saw events

occur in chronological order, with no guarantee regarding timing.

closed events

occur in tree order.

In general, saw events are good for immediate feedback to the user, not so good in terms of making sense of things. They’re generated as soon as the relevant output element enters the system.

On the other hand, closed events will be generated in the natural order you’d expect the output to appear in. Most Formatter subclasses use closed events.

A list which has not received a “list_end” event from upstream will block lists later in tree order until it closes. A Formatter that listens only to closed events can present them to the user in a way that should be reasonable, although output might be halting for any process that takes noticeable time.

Defined Under Namespace

Classes: Exception

Instance Method Summary collapse

Constructor Details

#initializePresenter

Returns a new instance of Presenter.



286
287
288
289
290
# File 'lib/command-set/results.rb', line 286

def initialize
  @results = List.new("")
  @leading_edge = @results
  @formatters = []
end

Instance Method Details

#begin_list(list_path, options = {}) ⇒ Object



316
317
318
319
320
321
322
323
324
325
# File 'lib/command-set/results.rb', line 316

def begin_list( list_path, options={} )
  list = list_path.pop
  home = get_collection(list_path)
  list = List.new(list)
  list.options = home.options.merge(options)
  list.depth = list_path.length
  notify(:saw_begin, list)
  home.add(list)
  advance_leading_edge
end

#create_collectorObject



292
293
294
# File 'lib/command-set/results.rb', line 292

def create_collector
  return Collector.new(self)
end

#doneObject



343
344
345
346
347
# File 'lib/command-set/results.rb', line 343

def done
  @results.close
  advance_leading_edge
  notify(:done, nil)
end

#end_list(list_path) ⇒ Object



336
337
338
339
340
341
# File 'lib/command-set/results.rb', line 336

def end_list( list_path )
  list = get_collection( list_path )
  notify(:saw_end, list)
  list.close
  advance_leading_edge
end

#item(item_path, options = {}) ⇒ Object



296
297
298
299
300
301
302
303
304
# File 'lib/command-set/results.rb', line 296

def item( item_path, options={} )
  item = item_path.pop
  home = get_collection(item_path)
  item = home.add item
  item.options = home.options.merge(options)
  item.depth = item_path.length
  notify(:saw, item)
  advance_leading_edge
end

#leading_edge?(list) ⇒ Boolean

Returns:

  • (Boolean)


306
307
308
# File 'lib/command-set/results.rb', line 306

def leading_edge?(list)
  return list == @leading_edge
end

#list_open?(list_path) ⇒ Boolean

Returns:

  • (Boolean)


327
328
329
330
331
332
333
334
# File 'lib/command-set/results.rb', line 327

def list_open?(list_path)
  begin 
    get_collection(list_path)
    return true
  rescue Exception
    return false
  end
end

#outputObject

Returns the current list of results. A particularly advanced Formatter might treat saw_* events like notifications, and then use the List#filter functionality to discover the specifics about the item or list just closed.



352
353
354
# File 'lib/command-set/results.rb', line 352

def output
  @results
end

#register_formatter(formatter) ⇒ Object



310
311
312
313
314
# File 'lib/command-set/results.rb', line 310

def register_formatter(formatter)
  @formatters << formatter

  formatter.notify(:start, nil)
end