lps 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/lps.rb +32 -43
- data/lib/lps/version.rb +1 -1
- data/lps.gemspec +1 -0
- data/test/test_lps.rb +12 -5
- metadata +19 -4
data/lib/lps.rb
CHANGED
@@ -1,54 +1,39 @@
|
|
1
1
|
require "lps/version"
|
2
|
+
require 'option_initializer'
|
2
3
|
|
3
4
|
class LPS
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
# @param [Numeric] freq Frequency of loop execution (loops/sec)
|
23
|
-
# @return [LPS]
|
24
|
-
def freq freq
|
25
|
-
LPS.new(:freq => freq, :cond => @cond)
|
26
|
-
end
|
27
|
-
|
28
|
-
# @param [Numeric] intv Loop execution interval
|
29
|
-
# @return [LPS]
|
30
|
-
def interval intv
|
31
|
-
freq(intv == 0 ? nil : (1.0 / intv))
|
32
|
-
end
|
33
|
-
|
34
|
-
# @param [Proc] &cond Loop condition
|
35
|
-
# @return [LPS]
|
36
|
-
def while &cond
|
37
|
-
LPS.new(:freq => @freq, :cond => cond)
|
5
|
+
include OptionInitializer
|
6
|
+
option_initializer :freq, :interval, :while
|
7
|
+
option_validator do |k, v|
|
8
|
+
case k
|
9
|
+
when :freq
|
10
|
+
raise ArgumentError,
|
11
|
+
'frequency must be a positive number (or nil)' unless
|
12
|
+
v.nil? || (v.is_a?(Numeric) && v > 0)
|
13
|
+
when :interval
|
14
|
+
raise ArgumentError,
|
15
|
+
'interval must be a positive number (or zero)' unless
|
16
|
+
v.is_a?(Numeric) && v >= 0
|
17
|
+
when :while
|
18
|
+
raise ArgumentError,
|
19
|
+
'loop condition must respond to call' unless
|
20
|
+
v.respond_to?(:call)
|
21
|
+
end
|
38
22
|
end
|
39
23
|
|
40
24
|
# @param [Proc] &block Loop
|
41
25
|
def loop &block
|
42
26
|
ret = nil
|
27
|
+
always = @while.nil?
|
43
28
|
if @freq.nil?
|
44
|
-
while @
|
29
|
+
while always || @while.call
|
45
30
|
ret = block.call
|
46
31
|
end
|
47
32
|
else
|
48
33
|
sleep_interval = 1.0 / @freq
|
49
34
|
|
50
35
|
nt = Time.now
|
51
|
-
while @
|
36
|
+
while always || @while.call
|
52
37
|
nt += sleep_interval
|
53
38
|
ret = block.call
|
54
39
|
|
@@ -69,16 +54,20 @@ class LPS
|
|
69
54
|
# @option opts [Numeric] :freq Frequency of loop execution (loops/sec)
|
70
55
|
# @option opts [#call] :cond Loop condition
|
71
56
|
def initialize opts = {}
|
72
|
-
|
57
|
+
validate_options opts
|
73
58
|
|
74
|
-
|
59
|
+
freq, intv, @while = opts.values_at :freq, :interval, :while
|
75
60
|
raise ArgumentError,
|
76
|
-
'
|
77
|
-
@freq.nil? || (@freq.is_a?(Numeric) && @freq > 0)
|
61
|
+
"can't have both frequency and interval" if freq && intv
|
78
62
|
|
79
|
-
@
|
80
|
-
|
81
|
-
|
63
|
+
@freq =
|
64
|
+
if freq
|
65
|
+
freq
|
66
|
+
elsif intv
|
67
|
+
(intv == 0) ? nil : 1.0 / intv
|
68
|
+
else
|
69
|
+
nil
|
70
|
+
end
|
82
71
|
end
|
83
72
|
end
|
84
73
|
|
data/lib/lps/version.rb
CHANGED
data/lps.gemspec
CHANGED
@@ -15,6 +15,7 @@ Gem::Specification.new do |gem|
|
|
15
15
|
gem.require_paths = ["lib"]
|
16
16
|
gem.version = LPS::VERSION
|
17
17
|
|
18
|
+
gem.add_runtime_dependency 'option_initializer', '~> 1.1.3'
|
18
19
|
gem.add_development_dependency 'test-unit'
|
19
20
|
gem.add_development_dependency 'guard'
|
20
21
|
gem.add_development_dependency 'guard-test'
|
data/test/test_lps.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
+
$VERBOSE = true
|
3
4
|
require 'rubygems'
|
4
|
-
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '../lib')
|
5
5
|
require 'lps'
|
6
6
|
require 'test-unit'
|
7
7
|
|
@@ -34,12 +34,19 @@ class TestLPS < Test::Unit::TestCase
|
|
34
34
|
assert_raise(ArgumentError) { LPS.freq(-1) }
|
35
35
|
end
|
36
36
|
|
37
|
-
def
|
38
|
-
assert_raise(ArgumentError) { LPS.
|
37
|
+
def test_invalid_interval
|
38
|
+
assert_raise(ArgumentError) { LPS.interval(-1) }
|
39
39
|
end
|
40
40
|
|
41
|
-
def
|
42
|
-
|
41
|
+
def test_both_freq_and_interval
|
42
|
+
LPS.interval(1).freq(1)
|
43
|
+
assert_raise(ArgumentError) { LPS.interval(1).freq(1).new }
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_non_number_params
|
47
|
+
assert_raise(ArgumentError) { LPS.freq('bad') }
|
48
|
+
assert_raise(ArgumentError) { LPS.interval('bad') }
|
49
|
+
assert_raise(ArgumentError) { LPS.while('bad') }
|
43
50
|
end
|
44
51
|
|
45
52
|
def test_return_value
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lps
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,8 +9,24 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-03-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: option_initializer
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.1.3
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 1.1.3
|
14
30
|
- !ruby/object:Gem::Dependency
|
15
31
|
name: test-unit
|
16
32
|
requirement: !ruby/object:Gem::Requirement
|
@@ -96,10 +112,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
96
112
|
version: '0'
|
97
113
|
requirements: []
|
98
114
|
rubyforge_project:
|
99
|
-
rubygems_version: 1.8.
|
115
|
+
rubygems_version: 1.8.25
|
100
116
|
signing_key:
|
101
117
|
specification_version: 3
|
102
118
|
summary: Rate-controlled loop execution
|
103
119
|
test_files:
|
104
120
|
- test/test_lps.rb
|
105
|
-
has_rdoc:
|