Class: Macros4Cuke::FormattingService

Inherits:
Object
  • Object
show all
Defined in:
lib/macros4cuke/formatting-service.rb

Overview

A worker class that drives the rendering of macro-steps in any registered format.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFormattingService

Constructor.



22
23
24
25
# File 'lib/macros4cuke/formatting-service.rb', line 22

def initialize()
  @formatters = []
  @walker_factory = CollWalkerFactory.new
end

Instance Attribute Details

#formattersObject (readonly)

The list of registered formatters



16
17
18
# File 'lib/macros4cuke/formatting-service.rb', line 16

def formatters
  @formatters
end

#walker_factoryObject (readonly)

Link to a factory of walker objects that visit macro collections



19
20
21
# File 'lib/macros4cuke/formatting-service.rb', line 19

def walker_factory
  @walker_factory
end

Instance Method Details

#register(aFormatter) ⇒ Object

Register a formatter. Raises an exception when the formatter implements an unknown formatting event.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/macros4cuke/formatting-service.rb', line 30

def register(aFormatter)
  # Get the list of formatted events supported by the formatter
  supported_events = aFormatter.implements

  # Complain if list is empty or nil
  if supported_events.nil? || supported_events.empty?
    raise(NoFormattingEventForFormatter.new(aFormatter))
  end

  # Check that each event from the event list the formatter is known.
  supported_events.each do |event|
    next if Formatter::AllNotifications.include? event
    
    raise(UnknownFormattingEvent.new(aFormatter, event))
  end

  formatters << aFormatter
end

#start!(aMacroCollection) ⇒ Object

Launch the rendering(s) of the given macro collection.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/macros4cuke/formatting-service.rb', line 50

def start!(aMacroCollection)
  # Create a walker (that will visit the collection)
  walker = walker_factory.build_walker(aMacroCollection)

  walker.each do |visit_event|
    (msg, nesting_level) = visit_event[0..1]

    # Notify each formatter of the visit event.
    formatters.each do |fmt|
      accepted_notifications = fmt.implements
      next unless accepted_notifications.include? msg
      
      # Assumption: all nil argument(s) are at the end
      arg_vector = visit_event[2..-1].reject(&:nil?)
      fmt.send(msg, nesting_level, *arg_vector)
    end
  end
end