ruby_events 0.0.6 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.markdown +9 -0
- data/Rakefile +1 -1
- data/lib/ruby_events.rb +40 -19
- metadata +3 -3
data/README.markdown
CHANGED
@@ -58,6 +58,15 @@ a little bit of sugar:
|
|
58
58
|
end
|
59
59
|
|
60
60
|
a << 'this is a test'
|
61
|
+
|
62
|
+
You can supply the `listen` method with a Proc, an array of Procs, or a block.
|
63
|
+
You can also give it a mixute of Procs and a block if you really want:
|
64
|
+
|
65
|
+
a = []
|
66
|
+
a.events.fire_on_method('<<'.to_sym, :item_injected)
|
67
|
+
a.events.listen(:injected, [Proc.new { |event_data| puts event_data; }, Proc.new { puts 'Hello, I was called!'; }])
|
68
|
+
|
69
|
+
a << 'this is a test'
|
61
70
|
|
62
71
|
These method events will automatically be passed the arguments that were passed
|
63
72
|
to that method when it was called. Don't let your imagination stop there. You
|
data/Rakefile
CHANGED
@@ -12,7 +12,7 @@ require 'rake/testtask'
|
|
12
12
|
|
13
13
|
spec = Gem::Specification.new do |s|
|
14
14
|
s.name = 'ruby_events'
|
15
|
-
s.version = '0.0.
|
15
|
+
s.version = '0.0.7'
|
16
16
|
s.has_rdoc = true
|
17
17
|
s.extra_rdoc_files = ['README.markdown']
|
18
18
|
s.summary = 'A really simple event implementation that hooks into the Object class. Now all your objects can join in the fun of firing events!'
|
data/lib/ruby_events.rb
CHANGED
@@ -7,17 +7,29 @@ module RubyEvents
|
|
7
7
|
# The Events class. Contains all the methods and functionality that drives
|
8
8
|
# the actual processes of adding, listening, calling and receiving events.
|
9
9
|
class Events
|
10
|
+
# The current version of RubyEvents.
|
11
|
+
def self.version
|
12
|
+
'0.0.7'
|
13
|
+
end
|
14
|
+
|
10
15
|
# Initialize the events class by instantiating the class methods we'll be
|
11
16
|
# using.
|
12
17
|
def initialize(parent)
|
13
18
|
@parent, @events = parent, {}
|
14
19
|
end
|
15
20
|
|
16
|
-
# Add a listener to the passed event type.
|
17
|
-
|
21
|
+
# Add a listener to the passed event type. You can pass a Proc, an array of
|
22
|
+
# Proc's, or a block.
|
23
|
+
def listen(event_type, procs = nil, &block)
|
18
24
|
@events[event_type] = [] unless event_is_defined(event_type)
|
19
|
-
|
20
|
-
|
25
|
+
procs_collected = []
|
26
|
+
if procs.respond_to?(:each)
|
27
|
+
procs_collected += procs.to_a
|
28
|
+
elsif procs
|
29
|
+
procs_collected << procs
|
30
|
+
end
|
31
|
+
procs_collected << block if block
|
32
|
+
@events[event_type] += procs_collected
|
21
33
|
end
|
22
34
|
|
23
35
|
# Fire all registered listeners to the passed event, passing them the
|
@@ -31,23 +43,32 @@ module RubyEvents
|
|
31
43
|
# Set an event to fire when passed method is called. This is useful for
|
32
44
|
# adding callbacks or events to built-in methods.
|
33
45
|
def fire_on_method(method, event_type, &block)
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
46
|
+
parent = @parent
|
47
|
+
method_s = method.to_s
|
48
|
+
old_method = ('ruby_events_' + method_s + '_event_old').to_sym
|
49
|
+
old_method_s = old_method.to_s
|
50
|
+
if parent && parent.respond_to?(method)
|
51
|
+
parent.class.class_eval do
|
52
|
+
# If the parent is already responding to the alias method, it means
|
53
|
+
# the fire_on_method event was already triggered. Remove the other
|
54
|
+
# event and continue if this happens.
|
55
|
+
if parent.respond_to?(old_method, true)
|
56
|
+
undef_method method
|
57
|
+
alias_method method, old_method
|
58
|
+
undef_method old_method
|
59
|
+
end
|
60
|
+
|
39
61
|
alias_method old_method, method
|
62
|
+
private old_method
|
63
|
+
|
64
|
+
# Make sure the self.send is at the end, or we won't return what we
|
65
|
+
# are supposed to.
|
66
|
+
define_method "#{method_s}" do |*args|
|
67
|
+
events.fire(event_type, *args)
|
68
|
+
block.call(*args) if(block) # Calling the block we've been passed
|
69
|
+
__send__(old_method, *args)
|
70
|
+
end
|
40
71
|
end
|
41
|
-
# FIXME: We need to find a way to call the block that's been passed
|
42
|
-
# so they can define the arguments they want to pass to the event.
|
43
|
-
#
|
44
|
-
# We are using self.send instead of calling the method directory because
|
45
|
-
# methods like << can not be resolved as a method properly once they've
|
46
|
-
# had the _event_old suffix tacked on the end.
|
47
|
-
#
|
48
|
-
# Make sure the self.send is at the end, or we won't return what we
|
49
|
-
# are supposed to.
|
50
|
-
@parent.class_eval('def ' + method_s + '(*args); events.fire(:' + event_type.to_s + ', *args); self.send("' + old_method_s + '".to_sym, *args); end')
|
51
72
|
else
|
52
73
|
# TODO: Need to raise exception here.
|
53
74
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 7
|
9
|
+
version: 0.0.7
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Nathan Kleyn
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-05-
|
17
|
+
date: 2010-05-16 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|