Parent

Picombo::Event

Event Class

Process queuing/execution class. Allows an unlimited number of callbacks to be added to ‘events’. Events can be run multiple times, and can also process event-specific data. By default, Picombo has several system events.

The event class is a powerful system where you can add, modify or remove Picombo functionality, and you can also create your own events to leverage easy plugablity features in your applicetion.

Examples

It’s very easy to use events to alter system behavior.

Using Procs

You can add one-off proc objects to the event queue:

 Picombo::Event.add('system.post_router') do |data|
     data.merge!({:params => ['test', 'test', 'test']})
 end

This event will be called on the system.post_router event, after the router has parsed the URI

Because the system.post_router is called as Picombo::Event.run(‘system.post_router’, uri) it passes the routed uri variable as data in the method above.

Using Strings

You can also add class methods to events:

 Picombo::Event.add('system.shutdown', 'Picombo::Foobar.new.write_access_log')

This might process some data and then write it to an event log on the system.shutdown event right before the output is sent to the browser

String event additions use eval(), so be careful!

Using Arrays

You can also use arrays to pass things to the event queue:

 Picombo::Event.add('system.display', [self, 'display'])

This will run the current class’s “display” method on the system.display event

Public Class Methods

add(name, callback = nil, &block) click to toggle source

Add a callback to an event queue.

    # File lib/classes/event.rb, line 62
62:         def self.add(name, callback = nil, &block)
63:             # Create the event queue if it doesn't exist yet.
64:             unless @@events.include?(name) and ! @@events.empty?
65:                 @@events[name] = []
66:             end
67: 
68:             # Make sure there isn't a callback and a block, two callbacks at the same time doesn't make sense.
69:             raise ArgumentError.new('You cannot add a callback to an event queue and specify a block, at the same time.') if ! callback.nil? and block_given?
70: 
71:             if block_given?
72:                 @@events[name].push(block)
73:             else
74:                 # If the callback is a string/array, make sure it doesn't already exist.
75:                 if ! callback.respond_to?(:call)
76:                     return false if @@events[name].include?(callback)
77:                 end
78: 
79:                 @@events[name].push(callback)
80:             end
81:         end
clear!(name = nil, callback = nil) click to toggle source

Remove one or all of the callbacks from a given event.

     # File lib/classes/event.rb, line 93
 93:         def self.clear!(name = nil, callback = nil)
 94:             @@events = {} if name.nil?
 95: 
 96:             if callback.nil?
 97:                 @@events[name] = []
 98:             else
 99:                 @@events[name].delete(callback)
100:             end
101:         end
data() click to toggle source

Data reader.

    # File lib/classes/event.rb, line 50
50:         def self.data()
51:             @@data
52:         end
data=(data) click to toggle source

Data writer.

    # File lib/classes/event.rb, line 55
55:         def self.data=(data)
56:             @@data = data
57:         end
get(name) click to toggle source

Return all of the callbacks attached to an event, or an empty array if there are none.

    # File lib/classes/event.rb, line 86
86:         def self.get(name)
87:             ( ! @@events.include?(name) or @@events[name].empty?) ? [] : @@events[name]
88:         end
ran?(name) click to toggle source

Determine whether or not an event has ran yet.

     # File lib/classes/event.rb, line 133
133:         def self.ran?(name)
134:             @@ran.include?(name)
135:         end
run(name, data = nil) click to toggle source

Execute all of the callbacks attached to a given event.

     # File lib/classes/event.rb, line 106
106:         def self.run(name, data = nil)
107:             # Make sure the event queue exists and has at least one attached callback.
108:             return false unless @@events.include?(name) and ! @@events[name].empty?
109: 
110:             @@data = data
111: 
112:             @@events[name].each do |callback|
113:                 if callback.is_a?(Array)
114:                     if callback[0].is_a?(String)
115:                         const_get(callback[0]).send(callback[1])
116:                     else
117:                         callback[0].send(callback[1])
118:                     end
119:                 elsif callback.is_a?(String)
120:                     eval(callback)
121:                 elsif callback.is_a?(Proc)
122:                     callback.call(data)
123:                 end
124:             end
125: 
126:             @@ran.push(name)
127:             @@data = nil
128:         end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.