Class: Herpes
- Inherits:
-
Object
show all
- Extended by:
- Forwardable
- Defined in:
- lib/herpes/event.rb,
lib/herpes.rb,
lib/herpes/module.rb,
lib/herpes/version.rb
Overview
–
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
++
Defined Under Namespace
Classes: Callback, Event, Module
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize ⇒ Herpes
Returns a new instance of Herpes.
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/herpes.rb', line 86
def initialize
@pool = ThreadPool.new(5)
@modules = []
@before = Hash.new { |h, k| h[k] = [] }
@matchers = Hash.new { |h, k| h[k] = [] }
@after = Hash.new { |h, k| h[k] = [] }
@callbacks = []
end
|
Instance Attribute Details
Returns the value of attribute modules.
83
84
85
|
# File 'lib/herpes.rb', line 83
def modules
@modules
end
|
Class Method Details
.load(*path) ⇒ Object
77
78
79
|
# File 'lib/herpes.rb', line 77
def self.load (*path)
new.load(*path)
end
|
12
13
14
|
# File 'lib/herpes/version.rb', line 12
def self.version
'0.0.2.8'
end
|
Instance Method Details
#after(&block) ⇒ Object
177
178
179
180
181
182
183
184
|
# File 'lib/herpes.rb', line 177
def after (&block)
return unless block
@after[@current] << block
@after[@current].uniq!
self
end
|
#before(&block) ⇒ Object
159
160
161
162
163
164
165
166
|
# File 'lib/herpes.rb', line 159
def before (&block)
return unless block
@before[@current] << block
@before[@current].uniq!
self
end
|
#cancel(&block) ⇒ Object
233
234
235
236
|
# File 'lib/herpes.rb', line 233
def cancel (&block)
@callbacks.reject!(&block)
wake_up
end
|
#dispatch(event = nil, &block) ⇒ Object
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
# File 'lib/herpes.rb', line 186
def dispatch (event = nil, &block)
if block && !event
event = Event.new(&block)
end
raise ArgumentError, 'you did not pass an Event' unless event.is_a?(Event)
@before.each {|name, blocks|
next unless name.nil? || (event.generated_by && event.generated_by =~ name)
blocks.each { |b| b.call(event) }
}
@matchers.each {|name, matchers|
next unless name.nil? || (event.generated_by && event.generated_by =~ name)
dispatched = false
matchers.each {|m|
begin
dispatched = true
m.block.call(event)
end if
(m.matcher == :anything) ||
(m.matcher == :anything_else && !dispatched) ||
(m.matcher.respond_to?(:call) && m.matcher.call(event))
}
}
@after.each {|name, blocks|
next unless name.nil? || (event.generated_by && event.generated_by =~ name)
blocks.each { |b| b.call(event) }
}
end
|
#every(time, discriminator = nil, &block) ⇒ Object
223
224
225
226
|
# File 'lib/herpes.rb', line 223
def every (time, discriminator = nil, &block)
@callbacks << Callback.new(time, discriminator, &block)
wake_up
end
|
#from(name, &block) ⇒ Object
150
151
152
153
154
155
156
157
|
# File 'lib/herpes.rb', line 150
def from (name, &block)
return unless block
@current, tmp = name, @current
result = instance_eval &block
@current = tmp
result
end
|
#load(*paths) ⇒ Object
130
131
132
133
134
135
136
|
# File 'lib/herpes.rb', line 130
def load (*paths)
paths.each {|path|
instance_eval File.read(path), path, 1
}
self
end
|
#log_at(path = nil) ⇒ Object
101
102
103
|
# File 'lib/herpes.rb', line 101
def log_at (path = nil)
path ? @log_at = File.expand_path(path) : @log_at
end
|
#on(matcher, &block) ⇒ Object
168
169
170
171
172
173
174
175
|
# File 'lib/herpes.rb', line 168
def on (matcher, &block)
return unless block
@matchers[@current] << Struct.new(:matcher, :block).new(matcher, block)
@matchers[@current].uniq!
self
end
|
#once(time, discriminator = nil, &block) ⇒ Object
Also known as:
once_after
228
229
230
231
|
# File 'lib/herpes.rb', line 228
def once (time, discriminator = nil, &block)
@callbacks << Callback.new(time, discriminator, true, &block)
wake_up
end
|
#running? ⇒ Boolean
250
|
# File 'lib/herpes.rb', line 250
def running?; @running; end
|
120
121
122
123
124
125
126
127
128
|
# File 'lib/herpes.rb', line 120
def save
return unless @state && @path
Marshal.dump(@state).tap {|dump|
File.open(@path, 'wb') { |f| f.write(dump) }
}
self
end
|
#save_every(time) ⇒ Object
114
115
116
117
118
|
# File 'lib/herpes.rb', line 114
def save_every (time)
cancel { |c| c.discriminator == self }
every time, self do save end
end
|
#sleep(time = nil) ⇒ Object
303
304
305
306
307
308
309
310
311
|
# File 'lib/herpes.rb', line 303
def sleep (time = nil)
@awakenable ||= IO.pipe
begin
@awakenable.first.read_nonblock 2048
rescue Errno::EAGAIN; end
IO.select([@awakenable.first], nil, nil, time)
end
|
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
# File 'lib/herpes.rb', line 253
def start
@running = true
while running?
sleep until_next
break unless running?
@callbacks.select {|callback|
callback.next_in <= 0 && !(callback.gonna_call? || callback.calling?)
}.each {|callback|
@callbacks.delete(callback) if callback.one_shot?
callback.gonna_call!
process {
begin
callback.call(self)
rescue Exception => e
(log_at ? File.open(log_at, ?a) : STDOUT).tap {|f|
f.write "[#{Time.now}] "
f.write "From: #{caller[0, 1].join "\n"}\n"
f.write "#{e.class}: #{e.message}\n"
f.write e.backtrace.to_a.join "\n"
f.write "\n\n"
}
end
}
}
end
ensure
save
@stopped = true
end
|
#state(path = nil) ⇒ Object
105
106
107
108
109
110
111
112
|
# File 'lib/herpes.rb', line 105
def state (path = nil)
if path && path != @path
@path = File.expand_path(path)
@state = Marshal.load(File.read(@path)) rescue nil
else
@state ||= {}
end
end
|
299
300
301
|
# File 'lib/herpes.rb', line 299
def stop
stop!
end
|
289
290
291
292
293
294
295
296
297
|
# File 'lib/herpes.rb', line 289
def stop!
return unless running?
@running = false
wake_up
@pool.shutdown
end
|
#stopped? ⇒ Boolean
251
|
# File 'lib/herpes.rb', line 251
def stopped?; @stopped; end
|
#until_next ⇒ Object
238
239
240
241
242
243
244
245
246
247
248
|
# File 'lib/herpes.rb', line 238
def until_next
return 0 unless running?
callbacks = @callbacks.reject(&:calling?).reject(&:gonna_call?)
return if callbacks.empty?
next_in = callbacks.min_by(&:next_in).next_in
next_in > 0 ? next_in : 0
end
|
#use(name, &block) ⇒ Object
144
145
146
147
148
|
# File 'lib/herpes.rb', line 144
def use (name, &block)
raise ArgumentError, "#{name} not found" unless Module[name]
@modules << Module[name].use(self, &block)
end
|
313
314
315
316
317
|
# File 'lib/herpes.rb', line 313
def wake_up
@awakenable ||= IO.pipe
@awakenable.last.write 'x'
end
|
#with(name, &block) ⇒ Object
138
139
140
141
142
|
# File 'lib/herpes.rb', line 138
def with (name, &block)
raise ArgumentError, "#{name} not found" unless Module[name]
Module[name].with(&block)
end
|
#workers(number) ⇒ Object
97
98
99
|
# File 'lib/herpes.rb', line 97
def workers (number)
@pool.resize(number)
end
|