Class: MPT::Event

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

Constant Summary collapse

@@mpt_subscribers =
{}

Class Method Summary collapse

Class Method Details

.clear_event_chain(event_name) ⇒ Object



66
67
68
# File 'lib/event.rb', line 66

def clear_event_chain(event_name)
  @@mpt_subscribers.delete(event_name)
end

.clear_owner_subscribers(owner) ⇒ Object



70
71
72
73
74
# File 'lib/event.rb', line 70

def clear_owner_subscribers(owner)
  @@mpt_subscribers.each_pair do |event_name, subscribers|
    @@mpt_subscribers[event_name] = subscribers.select { |s| owner != s[:options][:owner]  }
  end
end

.method_missing(name, *args) ⇒ Object



76
77
78
# File 'lib/event.rb', line 76

def method_missing(name, *args)
  self.trigger(name.to_sym, *args)
end

.subscribe(event_name, options = {}, &block) ⇒ Object



44
45
46
47
48
# File 'lib/event.rb', line 44

def subscribe(event_name, options = {}, &block)
  channel = @@mpt_subscribers[event_name] ||= []
  channel << { :options => options, :proc => block }
  channel.mpt_filterchain_reorganize
end

.trigger(event_name, *args) ⇒ Object



62
63
64
# File 'lib/event.rb', line 62

def trigger(event_name, *args)
  trigger_with_object(event_name, nil, *args)
end

.trigger_with_object(event_name, object, *args) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/event.rb', line 50

def trigger_with_object(event_name, object, *args)
  container = MPT::EventContainer.new(object)
  channel = @@mpt_subscribers[event_name]
  if !channel.nil? && channel.size > 0
    channel.each do |subscriber|
      container.instance_eval_with_args( *args, &subscriber[:proc] )
    end
  end

  container.event_object
end