class Synco::Controller

Basic event handling and delegation.

Attributes

events[R]

Public Class Methods

build(*arguments, **options) { |controller| ... } click to toggle source
# File lib/synco/controller.rb, line 24
def self.build(*arguments, **options, &block)
        controller = self.new(*arguments, **options)
        
        if block_given?
                yield(controller)
        end
        
        controller.freeze
        
        return controller
end
new() click to toggle source
# File lib/synco/controller.rb, line 36
def initialize
        @events = Hash.new{|hash,key| hash[key] = Array.new}
        @aborted = false
end

Public Instance Methods

abort!(persistent = false) click to toggle source

Abort the current event handler. Aborting an event handler persistently implies that in the future it will still be aborted; thus calling try will have no effect.

# File lib/synco/controller.rb, line 106
def abort!(persistent = false)
        @aborted = true if persistent
        
        throw abort_name
end
fire(event, *args) click to toggle source

Fire an event which calls all registered event handlers in the order they were defined. The first argument is used to instance_eval any handlers.

# File lib/synco/controller.rb, line 56
def fire(event, *args)
        return false unless @events.key?(event)
        
        handled = false
        
        scope = args.shift
        
        @events[event].each do |handler|
                handled = true

                if scope
                        scope.instance_exec(*args, &handler)
                else
                        handler.call
                end
        end
        
        return handled
end
freeze() click to toggle source
Calls superclass method
# File lib/synco/controller.rb, line 41
def freeze
        @events.freeze
        
        super
end
on(event, &block) click to toggle source

Register an event handler which may be triggered when an event is fired.

# File lib/synco/controller.rb, line 50
def on(event, &block)
        @events[event] << block
end
try(*arguments) { || ... } click to toggle source

Try executing a given block of code and fire appropriate events.

The sequence of events (registered via on) are as follows:

:prepare

Fired before the block is executed. May call abort! to cancel execution.

:success

Fired after the block of code has executed without raising an exception.

:failure

Fired if an exception is thrown during normal execution.

:finish

Fired at the end of execution regardless of failure.

If abort! has been called in the past, this function returns immediately.

# File lib/synco/controller.rb, line 85
def try(*arguments)
        return if @aborted
        
        begin
                catch(abort_name) do
                        fire(:prepare, *arguments)
                        
                        yield
                        
                        fire(:success, *arguments)
                end
        rescue Exception => exception
                # Propagage the exception unless it was handled in some specific way.
                raise unless fire(:failure, *arguments, exception)
        ensure
                fire(:finish, *arguments)
        end
end

Private Instance Methods

abort_name() click to toggle source

The name used for throwing abortions.

# File lib/synco/controller.rb, line 115
def abort_name
        ("abort_" + self.class.name).downcase.to_sym
end