Class: Synco::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/synco/controller.rb

Overview

Basic event handling and delegation.

Direct Known Subclasses

Directory, Method, Script, Server

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeController

Returns a new instance of Controller.



21
22
23
24
# File 'lib/synco/controller.rb', line 21

def initialize
	@events = Hash.new{|hash,key| hash[key] = Array.new}
	@aborted = false
end

Instance Attribute Details

#eventsObject (readonly)

Returns the value of attribute events.



32
33
34
# File 'lib/synco/controller.rb', line 32

def events
  @events
end

Class Method Details

.build(*arguments, **options, &block) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/synco/controller.rb', line 9

def self.build(*arguments, **options, &block)
	controller = self.new(*arguments, **options)
	
	if block_given?
		yield(controller)
	end
	
	controller.freeze
	
	return controller
end

Instance Method Details

#abort!(persistent = false) ⇒ Object

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.



91
92
93
94
95
# File 'lib/synco/controller.rb', line 91

def abort!(persistent = false)
	@aborted = true if persistent
	
	throw abort_name
end

#fire(event, *args) ⇒ Object

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.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/synco/controller.rb', line 41

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

#freezeObject



26
27
28
29
30
# File 'lib/synco/controller.rb', line 26

def freeze
	@events.freeze
	
	super
end

#on(event, &block) ⇒ Object

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



35
36
37
# File 'lib/synco/controller.rb', line 35

def on(event, &block)
	@events[event] << block
end

#try(*arguments) ⇒ Object

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.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/synco/controller.rb', line 70

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