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.



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

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

Instance Method Details

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



322
323
324
325
326
327
328
329
330
331
# File 'lib/command-set/results.rb', line 322

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



298
299
300
# File 'lib/command-set/results.rb', line 298

def create_collector
  return Collector.new(self)
end

#doneObject



349
350
351
352
353
# File 'lib/command-set/results.rb', line 349

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

#end_list(list_path) ⇒ Object



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

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



302
303
304
305
306
307
308
309
310
# File 'lib/command-set/results.rb', line 302

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)


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

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

#list_open?(list_path) ⇒ Boolean

Returns:

  • (Boolean)


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

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.



358
359
360
# File 'lib/command-set/results.rb', line 358

def output
  @results
end

#register_formatter(formatter) ⇒ Object



316
317
318
319
320
# File 'lib/command-set/results.rb', line 316

def register_formatter(formatter)
  @formatters << formatter

  formatter.notify(:start, nil)
end