runtime 1.0.0
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/library/runtime/event.rb +15 -0
- data/library/runtime/extensions.rb +13 -0
- data/library/runtime/manager.rb +33 -0
- data/library/runtime/pool.rb +16 -0
- data/library/runtime.rb +62 -0
- metadata +70 -0
@@ -0,0 +1,13 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
class Fixnum
|
4
|
+
def seconds; self end
|
5
|
+
def minutes; seconds * 60 end
|
6
|
+
def hours; minutes * 60 end
|
7
|
+
def days; days * 24 end
|
8
|
+
|
9
|
+
alias_method :second, :seconds
|
10
|
+
alias_method :minute, :minutes
|
11
|
+
alias_method :hour, :hours
|
12
|
+
alias_method :day, :days
|
13
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
Thread.abort_on_exception = true
|
4
|
+
|
5
|
+
module Runtime
|
6
|
+
class Manager
|
7
|
+
def initialize
|
8
|
+
@pool = Pool.new
|
9
|
+
@thread = spawn_thread
|
10
|
+
end
|
11
|
+
|
12
|
+
def manage
|
13
|
+
@pool.each_event_at current_time do |event|
|
14
|
+
@pool.drop event
|
15
|
+
event.pull
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def add_event event
|
20
|
+
@pool.push event if event.time >= current_time
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def spawn_thread
|
26
|
+
Thread.new do
|
27
|
+
loop { manage; sleep 1 }
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def current_time; Time.now.to_i end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Runtime
|
4
|
+
class Pool
|
5
|
+
def initialize
|
6
|
+
@events = []
|
7
|
+
end
|
8
|
+
|
9
|
+
def each_event_at time
|
10
|
+
@events.select { |event| event.time == time }.each { |event| yield event }
|
11
|
+
end
|
12
|
+
|
13
|
+
def push event; @events << event end
|
14
|
+
def drop event; @events.delete event end
|
15
|
+
end
|
16
|
+
end
|
data/library/runtime.rb
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'date'
|
4
|
+
require 'runtime/pool'
|
5
|
+
require 'runtime/event'
|
6
|
+
require 'runtime/manager'
|
7
|
+
require 'runtime/extensions'
|
8
|
+
|
9
|
+
module Runtime
|
10
|
+
class DuplicateInstance < StandardError; end
|
11
|
+
|
12
|
+
class << Version = [1,0]
|
13
|
+
def to_s; join '.' end
|
14
|
+
end
|
15
|
+
|
16
|
+
module_function
|
17
|
+
|
18
|
+
def enabled?; not $_runtime.nil? end
|
19
|
+
|
20
|
+
def enable
|
21
|
+
if $_runtime.nil?
|
22
|
+
$_runtime = Manager.new
|
23
|
+
else
|
24
|
+
raise DuplicateInstance, 'Runtime is already running'
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def in time, *args, &block
|
29
|
+
raise ArgumentError, 'The _in method demands a block!' and return unless block_given?
|
30
|
+
|
31
|
+
event = case time
|
32
|
+
when Time
|
33
|
+
Event.new time.to_i, *args, &block
|
34
|
+
when Fixnum
|
35
|
+
Event.new Time.now.to_i + time, *args, &block
|
36
|
+
end
|
37
|
+
|
38
|
+
$_runtime.add_event event
|
39
|
+
end
|
40
|
+
|
41
|
+
def at time, *args, &block
|
42
|
+
raise ArgumentError, 'The at method demands a block!' and return unless block_given?
|
43
|
+
|
44
|
+
event = case time
|
45
|
+
when String # FIXME
|
46
|
+
Event.new DateTime.parse(time).to_time.to_i, *args, &block
|
47
|
+
when Time
|
48
|
+
Event.new time.to_i, *args, &block
|
49
|
+
end
|
50
|
+
|
51
|
+
$_runtime.add_event event
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def _in *args, █ Runtime.in *args, &block end
|
56
|
+
def at *args, █ Runtime.at *args, &block end
|
57
|
+
|
58
|
+
# WHY, RUBY.. WHY ;_;!?
|
59
|
+
alias_method :in_, :_in
|
60
|
+
alias_method :_in_, :_in
|
61
|
+
|
62
|
+
Runtime.enable unless $NO_RUNTIME and $NO_RUNTIME == true
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: runtime
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 1
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
version: 1.0.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Mikkel Kroman
|
13
|
+
autorequire:
|
14
|
+
bindir: executables
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-11-27 00:00:00 +01:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description:
|
22
|
+
email: mk@maero.dk
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files: []
|
28
|
+
|
29
|
+
files:
|
30
|
+
- library/runtime.rb
|
31
|
+
- library/runtime/manager.rb
|
32
|
+
- library/runtime/pool.rb
|
33
|
+
- library/runtime/event.rb
|
34
|
+
- library/runtime/extensions.rb
|
35
|
+
has_rdoc: true
|
36
|
+
homepage:
|
37
|
+
licenses: []
|
38
|
+
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
|
42
|
+
require_paths:
|
43
|
+
- library
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
segments:
|
50
|
+
- 1
|
51
|
+
- 9
|
52
|
+
- 1
|
53
|
+
version: 1.9.1
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
segments:
|
60
|
+
- 0
|
61
|
+
version: "0"
|
62
|
+
requirements: []
|
63
|
+
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 1.3.7
|
66
|
+
signing_key:
|
67
|
+
specification_version: 3
|
68
|
+
summary: Runtime is an event library running in the background of your process, checking every second if there is an event ready to be run at the given time.
|
69
|
+
test_files: []
|
70
|
+
|