ruby_events 0.0.4 → 0.0.5

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.
Files changed (3) hide show
  1. data/Rakefile +1 -1
  2. data/lib/ruby_events.rb +17 -17
  3. metadata +2 -2
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.4'
15
+ s.version = '0.0.5'
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
@@ -4,17 +4,18 @@
4
4
  # accessor, you can set up and listen to events or callbacks as fired by this
5
5
  # class.
6
6
  module RubyEvents
7
+ # The Events class. Contains all the methods and functionality that drives
8
+ # the actual processes of adding, listening, calling and receiving events.
7
9
  class Events
8
10
  # Initialize the events class by instantiating the class methods we'll be
9
11
  # using.
10
12
  def initialize(parent)
11
- @parent = parent
12
- @events = {}
13
+ @parent, @events = parent, {}
13
14
  end
14
15
 
15
16
  # Add a listener to the passed event type.
16
17
  def listen(event_type, &event)
17
- @events[event_type] = [] unless @events.has_key? event_type
18
+ @events[event_type] = [] unless event_is_defined
18
19
  # TODO: Can we allow both block and an array of Proc's here?
19
20
  @events[event_type] << event
20
21
  end
@@ -22,26 +23,21 @@ module RubyEvents
22
23
  # Fire all registered listeners to the passed event, passing them the
23
24
  # arguments as provided.
24
25
  def fire(event_type, *arguments)
25
- if @events.has_key?(event_type)
26
- @events[event_type].each do |event|
27
- event.call arguments
28
- end
29
- end
26
+ @events[event_type].each do |event|
27
+ event.call arguments
28
+ end if event_is_defined
30
29
  end
31
30
 
32
31
  # Set an event to fire when passed method is called. This is useful for
33
32
  # adding callbacks or events to built-in methods.
34
33
  def fire_on_method(method, event_type, &block)
35
- old_method = (method.to_s + '_event_old').to_sym
34
+ method_s, old_method_s = method.to_s, old_method.to_s
35
+ old_method = (method_s + '_event_old').to_sym
36
36
  if @parent && @parent.respond_to?(method) && !@parent.respond_to?(old_method)
37
37
  @parent = @parent.class
38
38
  @parent.class_eval do
39
39
  alias_method old_method, method
40
40
  end
41
-
42
- method = method.to_s
43
- old_method = old_method.to_s
44
-
45
41
  # FIXME: We need to find a way to call the block that's been passed
46
42
  # so they can define the arguments they want to pass to the event.
47
43
  #
@@ -51,7 +47,7 @@ module RubyEvents
51
47
  #
52
48
  # Make sure the self.send is at the end, or we won't return what we
53
49
  # are supposed to.
54
- @parent.class_eval('def ' + method + '(*args); events.fire(:' + event_type + ', *args); self.send("' + old_method + '".to_sym, *args); end')
50
+ @parent.class_eval('def ' + method_s + '(*args); events.fire(:' + event_type.to_s + ', *args); self.send("' + old_method_s + '".to_sym, *args); end')
55
51
  else
56
52
  # TODO: Need to raise exception here.
57
53
  end
@@ -59,9 +55,13 @@ module RubyEvents
59
55
 
60
56
  # Remove a method from the listening queue.
61
57
  def remove(event_type, event)
62
- if @events.has_key?(event_type)
63
- @events[event_type].delete_if {|stored_event| stored_event == event}
64
- end
58
+ @events[event_type].delete_if {|stored_event| stored_event == event} if event_is_defined
59
+ end
60
+
61
+ private
62
+ # Checks if an event of event_type is defined in the collection of events.
63
+ def event_is_defined(event_type)
64
+ @events.has_key?(event_type)
65
65
  end
66
66
  end
67
67
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 4
9
- version: 0.0.4
8
+ - 5
9
+ version: 0.0.5
10
10
  platform: ruby
11
11
  authors:
12
12
  - Nathan Kleyn