Class: RubyEvents::Events

Inherits:
Object show all
Defined in:
lib/ruby_events.rb

Overview

The Events class. Contains all the methods and functionality that drives the actual processes of adding, listening, calling and receiving events.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent) ⇒ Events

Initialize the events class by instantiating the class methods we’ll be using.



17
18
19
# File 'lib/ruby_events.rb', line 17

def initialize(parent)
  @parent, @events = parent, {}
end

Class Method Details

.versionObject

The current version of RubyEvents.



11
12
13
# File 'lib/ruby_events.rb', line 11

def self.version
  '1.0.0'
end

Instance Method Details

#fire(event_type, *arguments) ⇒ Object

Fire all registered listeners to the passed event, passing them the arguments as provided.



37
38
39
40
41
# File 'lib/ruby_events.rb', line 37

def fire(event_type, *arguments)
  @events[event_type].each do |event|
    event.call(*arguments)
  end if event_is_defined(event_type)
end

#fire_on_method(method, event_type, &block) ⇒ Object

Set an event to fire when passed method is called. This is useful for adding callbacks or events to built-in methods.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ruby_events.rb', line 45

def fire_on_method(method, event_type, &block)
  # We alias @parent to parent here, because class_eval can't see outside
  # this scope otherwise.
  parent, old_method = @parent, ('ruby_events_' + method.to_s + '_event_old').to_sym
  if parent && parent.respond_to?(method)
    parent.class.class_eval do
      # If the parent is already responding to the alias method, it means
      # the fire_on_method event was already triggered. Remove the other
      # event and continue if this happens.
      if parent.respond_to?(old_method, true)
        remove_method method
        alias_method method, old_method
        remove_method old_method
      end
    
      alias_method old_method, method
      private old_method
      
      # Make sure the self.send is at the end, or we won't return what we
      # are supposed to.
      define_method method do |*args|
        events.fire(event_type, *args)
        block.call(*args) if block # Calling the block we've been passed
        __send__(old_method, *args)
      end
    end
  else
    raise RuntimeError.new('The given object does not respond to method you are trying to intercept calls to.')
  end
end

#listen(event_type, procs = nil, &block) ⇒ Object

Add a listener to the passed event type. You can pass a Proc, an array of Proc’s, or a block.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ruby_events.rb', line 23

def listen(event_type, procs = nil, &block)
  @events[event_type] = [] unless event_is_defined(event_type)
  procs_collected = []
  if procs.respond_to?(:each)
    procs_collected += procs.to_a
  elsif procs
    procs_collected << procs
  end
  procs_collected << block if block
  @events[event_type] += procs_collected
end

#remove(event_type, event) ⇒ Object

Remove a method from the listening queue.



77
78
79
# File 'lib/ruby_events.rb', line 77

def remove(event_type, event)
  @events[event_type].delete_if {|stored_event| stored_event == event} if event_is_defined(event_type)
end