ruby_events 0.0.3 → 0.0.4
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/Rakefile +3 -3
- data/lib/ruby_events.rb +24 -13
- metadata +3 -3
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.4'
|
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!'
|
@@ -32,9 +32,9 @@ Rake::GemPackageTask.new(spec) do |p|
|
|
32
32
|
end
|
33
33
|
|
34
34
|
Rake::RDocTask.new do |rdoc|
|
35
|
-
files =['README', '
|
35
|
+
files =['README.markdown', 'lib/**/*.rb']
|
36
36
|
rdoc.rdoc_files.add(files)
|
37
|
-
rdoc.main = "README" # page to start on
|
37
|
+
rdoc.main = "README.markdown" # page to start on
|
38
38
|
rdoc.title = "ruby_events Docs"
|
39
39
|
rdoc.rdoc_dir = 'doc/rdoc' # rdoc output folder
|
40
40
|
rdoc.options << '--line-numbers'
|
data/lib/ruby_events.rb
CHANGED
@@ -1,20 +1,26 @@
|
|
1
|
+
# The RubyEvents module contains the core logic used by the events firing class.
|
2
|
+
# All Object's are extended by default with an events accessor that is set to
|
3
|
+
# an instance of the RubyEvents::Events class. By calling the methods on this
|
4
|
+
# accessor, you can set up and listen to events or callbacks as fired by this
|
5
|
+
# class.
|
1
6
|
module RubyEvents
|
2
7
|
class Events
|
8
|
+
# Initialize the events class by instantiating the class methods we'll be
|
9
|
+
# using.
|
3
10
|
def initialize(parent)
|
4
11
|
@parent = parent
|
5
12
|
@events = {}
|
6
13
|
end
|
7
14
|
|
15
|
+
# Add a listener to the passed event type.
|
8
16
|
def listen(event_type, &event)
|
9
17
|
@events[event_type] = [] unless @events.has_key? event_type
|
10
18
|
# TODO: Can we allow both block and an array of Proc's here?
|
11
|
-
# events = [events] unless events.respond_to? :each
|
12
|
-
# events.each do |event|
|
13
|
-
# @events[event_type] << event
|
14
|
-
# end
|
15
19
|
@events[event_type] << event
|
16
20
|
end
|
17
21
|
|
22
|
+
# Fire all registered listeners to the passed event, passing them the
|
23
|
+
# arguments as provided.
|
18
24
|
def fire(event_type, *arguments)
|
19
25
|
if @events.has_key?(event_type)
|
20
26
|
@events[event_type].each do |event|
|
@@ -23,16 +29,19 @@ module RubyEvents
|
|
23
29
|
end
|
24
30
|
end
|
25
31
|
|
32
|
+
# Set an event to fire when passed method is called. This is useful for
|
33
|
+
# adding callbacks or events to built-in methods.
|
26
34
|
def fire_on_method(method, event_type, &block)
|
27
35
|
old_method = (method.to_s + '_event_old').to_sym
|
28
36
|
if @parent && @parent.respond_to?(method) && !@parent.respond_to?(old_method)
|
29
|
-
@parent.class
|
37
|
+
@parent = @parent.class
|
38
|
+
@parent.class_eval do
|
30
39
|
alias_method old_method, method
|
31
40
|
end
|
32
41
|
|
33
|
-
|
34
|
-
|
35
|
-
|
42
|
+
method = method.to_s
|
43
|
+
old_method = old_method.to_s
|
44
|
+
|
36
45
|
# FIXME: We need to find a way to call the block that's been passed
|
37
46
|
# so they can define the arguments they want to pass to the event.
|
38
47
|
#
|
@@ -42,12 +51,13 @@ module RubyEvents
|
|
42
51
|
#
|
43
52
|
# Make sure the self.send is at the end, or we won't return what we
|
44
53
|
# are supposed to.
|
45
|
-
@parent.
|
54
|
+
@parent.class_eval('def ' + method + '(*args); events.fire(:' + event_type + ', *args); self.send("' + old_method + '".to_sym, *args); end')
|
46
55
|
else
|
47
56
|
# TODO: Need to raise exception here.
|
48
57
|
end
|
49
58
|
end
|
50
59
|
|
60
|
+
# Remove a method from the listening queue.
|
51
61
|
def remove(event_type, event)
|
52
62
|
if @events.has_key?(event_type)
|
53
63
|
@events[event_type].delete_if {|stored_event| stored_event == event}
|
@@ -56,12 +66,13 @@ module RubyEvents
|
|
56
66
|
end
|
57
67
|
end
|
58
68
|
|
69
|
+
# Extending the Object class with the events accessor.
|
59
70
|
class Object
|
71
|
+
attr_writer :events
|
72
|
+
|
73
|
+
# Attribute reader for the events accessor. Returns a new instance of the
|
74
|
+
# events class if not defined, or the already defined class otherwise.
|
60
75
|
def events
|
61
76
|
@events || @events = RubyEvents::Events.new(self)
|
62
77
|
end
|
63
|
-
|
64
|
-
def events=(events)
|
65
|
-
@events = events
|
66
|
-
end
|
67
78
|
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
|
+
- 4
|
9
|
+
version: 0.0.4
|
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-04-
|
17
|
+
date: 2010-04-29 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|