holywarez-mpt 0.1.3.11 → 0.1.3.12
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/CHANGELOG +1 -0
- data/examples/unordered_subscribers.rb +13 -0
- data/install_gem.bat +1 -1
- data/lib/event.rb +41 -0
- data/mpt.gemspec +1 -1
- metadata +1 -1
data/CHANGELOG
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
module UnorderedSubscribersExperiment
|
|
2
2
|
include ::MPT
|
|
3
3
|
|
|
4
|
+
class Stub
|
|
5
|
+
def some_method(argument)
|
|
6
|
+
puts "Subscribe class instance for event: #{argument}"
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
subscribe :some_method, :for => "/some/method"
|
|
10
|
+
end
|
|
11
|
+
|
|
4
12
|
experiment "Unordered subscribers with void event object" do
|
|
5
13
|
Event.subscribe '/system/special-channel' do |parameter|
|
|
6
14
|
self.event_object = parameter
|
|
@@ -13,4 +21,9 @@ module UnorderedSubscribersExperiment
|
|
|
13
21
|
result = Event.trigger '/system/special-channel', "XXX"
|
|
14
22
|
puts "Result is: #{result}"
|
|
15
23
|
end
|
|
24
|
+
|
|
25
|
+
experiment "Subscribed instance methods" do
|
|
26
|
+
stub_instance = Stub.new
|
|
27
|
+
Event.trigger '/some/method', "[SUCCESS]"
|
|
28
|
+
end
|
|
16
29
|
end
|
data/install_gem.bat
CHANGED
data/lib/event.rb
CHANGED
|
@@ -77,3 +77,44 @@ module MPT
|
|
|
77
77
|
end # end of class Event
|
|
78
78
|
|
|
79
79
|
end
|
|
80
|
+
|
|
81
|
+
class Class
|
|
82
|
+
def subscribe( target, options )
|
|
83
|
+
return if options[:for].blank?
|
|
84
|
+
|
|
85
|
+
unless self.respond_to?( :new_with_subscribe_support )
|
|
86
|
+
code_to_eval = <<-EOC
|
|
87
|
+
class << self
|
|
88
|
+
def register_subscribe(target, options)
|
|
89
|
+
es = @@event_subscribes ||= {}
|
|
90
|
+
est = es[options[:for]] ||= []
|
|
91
|
+
est << target
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def new_with_subscribe_support(*constructor_arguments)
|
|
95
|
+
instance = new_without_subscribe_support(*constructor_arguments)
|
|
96
|
+
::MPT::Event.subscribe( "#{options[:for]}", :owner => instance ) do |*args|
|
|
97
|
+
instance.send( "trigger_event", "#{options[:for]}", *args )
|
|
98
|
+
end
|
|
99
|
+
instance
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
alias_method_chain :new, :subscribe_support
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
private
|
|
106
|
+
def trigger_event(name, *args)
|
|
107
|
+
@@event_subscribes[name].each do |method_name|
|
|
108
|
+
self.send( method_name, *args )
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
EOC
|
|
112
|
+
|
|
113
|
+
puts code_to_eval
|
|
114
|
+
self.class_eval( code_to_eval, __FILE__, __LINE__ )
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
self.register_subscribe( target, options )
|
|
118
|
+
|
|
119
|
+
end
|
|
120
|
+
end
|
data/mpt.gemspec
CHANGED