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.



275
276
277
278
279
280
# File 'lib/command-set/results.rb', line 275

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

Instance Method Details

#begin_list(home, name, options = {}) ⇒ Object



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

def begin_list( home, name, options={} )
  list = List.new(name)

  add_item(home, list, options)

  notify(:saw_begin, list)
  return list
end

#create_collectorObject



282
283
284
# File 'lib/command-set/results.rb', line 282

def create_collector
  return Collector.new(self, @results)
end

#doneObject



323
324
325
326
327
# File 'lib/command-set/results.rb', line 323

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

#end_list(list) ⇒ Object



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

def end_list( list )
  @list_lock.synchronize do
    list.close
    advance_leading_edge
  end

  notify(:saw_end, list)
  return nil
end

#item(home, value, options = {}) ⇒ Object



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

def item( home, value, options={} )
  item = ListItem.new(value)

  add_item(home, item, options)

  notify(:saw, item)
  return nil
end

#leading_edge?(list) ⇒ Boolean

Returns:

  • (Boolean)


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

def leading_edge?(list)
  return list == @leading_edge
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.



333
334
335
# File 'lib/command-set/results.rb', line 333

def output
  @results
end

#register_formatter(formatter) ⇒ Object



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

def register_formatter(formatter)
  @formatters << formatter
  formatter.notify(:start, nil)
end