lps 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -29,11 +29,18 @@ LPS.freq(10).while { Time.now - now < 10 }.loop do
29
29
  # do something
30
30
  end
31
31
 
32
+
32
33
  # - Loops 10 times per second
33
34
  # - Loops indefinitely
34
35
  LPS.freq(10).loop do
35
36
  # do something
36
37
  end
38
+
39
+
40
+ # Every 0.1 second
41
+ LPS.interval(0.1).loop do
42
+ # do something
43
+ end
37
44
  ```
38
45
 
39
46
  Breaking out of the loop
data/lib/lps.rb CHANGED
@@ -1,22 +1,43 @@
1
1
  require "lps/version"
2
2
 
3
3
  class LPS
4
+ # @param [Numeric] freq Frequency of loop execution (loops/sec)
5
+ # @return [LPS]
4
6
  def self.freq freq
5
- LPS.new :freq => freq
7
+ LPS.new.freq freq
6
8
  end
7
9
 
10
+ # @param [Numeric] intv Loop execution interval
11
+ # @return [LPS]
12
+ def self.interval intv
13
+ LPS.new.interval intv
14
+ end
15
+
16
+ # @param [Proc] &cond Loop condition
17
+ # @return [LPS]
8
18
  def self.while &cond
9
- LPS.new :cond => cond
19
+ LPS.new.while &cond
10
20
  end
11
21
 
22
+ # @param [Numeric] freq Frequency of loop execution (loops/sec)
23
+ # @return [LPS]
12
24
  def freq freq
13
25
  LPS.new(:freq => freq, :cond => @cond)
14
26
  end
15
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]
16
36
  def while &cond
17
37
  LPS.new(:freq => @freq, :cond => cond)
18
38
  end
19
39
 
40
+ # @param [Proc] &block Loop
20
41
  def loop &block
21
42
  ret = nil
22
43
  if @freq.nil?
@@ -44,17 +65,20 @@ class LPS
44
65
  ret
45
66
  end
46
67
 
68
+ # @param [Hash] opts Options Hash.
69
+ # @option opts [Numeric] :freq Frequency of loop execution (loops/sec)
70
+ # @option opts [#call] :cond Loop condition
47
71
  def initialize opts = {}
48
- raise ArgumentError.new("Not a Hash") unless opts.is_a?(Hash)
72
+ raise ArgumentError, 'Not a Hash' unless opts.is_a?(Hash)
49
73
 
50
74
  @freq = opts[:freq]
51
- raise ArgumentError.new(
52
- "Frequency must be a positive number (or nil)") unless
75
+ raise ArgumentError,
76
+ 'Frequency must be a positive number (or nil)' unless
53
77
  @freq.nil? || (@freq.is_a?(Numeric) && @freq > 0)
54
78
 
55
79
  @cond = opts[:cond] || proc { true }
56
- raise ArgumentError.new("Invalid condition block") unless
57
- @cond.is_a?(Proc)
80
+ raise ArgumentError, 'Invalid condition block' unless
81
+ @cond.respond_to?(:call)
58
82
  end
59
83
  end
60
84
 
@@ -1,3 +1,3 @@
1
1
  class LPS
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
@@ -66,5 +66,16 @@ class TestLPS < Test::Unit::TestCase
66
66
  puts [ps, cnt].join ' => '
67
67
  end
68
68
  end
69
+
70
+ def test_lps_interval
71
+ 20.times.map { |i| 1 << i }.each do |ps|
72
+ cnt = 0
73
+
74
+ now = Time.now
75
+ LPS.interval(1.0 / ps).while { Time.now - now <= 1 }.loop { cnt += 1 }
76
+
77
+ puts [ps, cnt].join ' => '
78
+ end
79
+ end
69
80
  end
70
81
 
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.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-23 00:00:00.000000000 Z
12
+ date: 2013-02-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: test-unit
16
- requirement: &2155956340 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,15 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *2155956340
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: guard
27
- requirement: &2155951960 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ! '>='
@@ -32,10 +37,15 @@ dependencies:
32
37
  version: '0'
33
38
  type: :development
34
39
  prerelease: false
35
- version_requirements: *2155951960
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
36
46
  - !ruby/object:Gem::Dependency
37
47
  name: guard-test
38
- requirement: &2155949980 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
39
49
  none: false
40
50
  requirements:
41
51
  - - ! '>='
@@ -43,7 +53,12 @@ dependencies:
43
53
  version: '0'
44
54
  type: :development
45
55
  prerelease: false
46
- version_requirements: *2155949980
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
47
62
  description: Rate-controlled loop execution
48
63
  email:
49
64
  - junegunn.c@gmail.com
@@ -81,9 +96,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
81
96
  version: '0'
82
97
  requirements: []
83
98
  rubyforge_project:
84
- rubygems_version: 1.8.11
99
+ rubygems_version: 1.8.24
85
100
  signing_key:
86
101
  specification_version: 3
87
102
  summary: Rate-controlled loop execution
88
103
  test_files:
89
104
  - test/test_lps.rb
105
+ has_rdoc: