lps 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3a433af96e982c102cc101b37b9a9c042f19db11
4
+ data.tar.gz: 800b2fbcb1ed6b121d04a44148d38076722416e9
5
+ SHA512:
6
+ metadata.gz: acaef70ad1f4aeb6df6e64f14b0a52a9834da3793274b423cc1139051be6200f1471905c18389ab789e4c88b9eb624c849a66184bc2ed4351a82cb1d4a9444db
7
+ data.tar.gz: bc3f0a35462336e7d7be7ed8daf83e14f1d9d8956e69b5588854e3041f542c9243da88cc6a0ea3e171beb8980f5569f83cd52e069243259e98cba5c0a7052290
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - "1.8.7"
4
+ - "1.9.2"
5
+ - "1.9.3"
6
+ - "2.0.0"
7
+ - jruby-18mode # JRuby in 1.8 mode
8
+ - jruby-19mode # JRuby in 1.9 mode
9
+ - rbx-18mode
10
+ - rbx-19mode
data/README.md CHANGED
@@ -50,6 +50,17 @@ Breaking out of the loop
50
50
  LPS.freq(10).loop { break if rand(10) == 0 }
51
51
  ```
52
52
 
53
+ Changing loop frequency
54
+ -----------------------
55
+
56
+ ```ruby
57
+ i = 0
58
+ LPS.while { i < 100 }.loop do |lps|
59
+ print '.'
60
+ lps.freq = i += 1
61
+ end
62
+ ```
63
+
53
64
  Falling behind
54
65
  --------------
55
66
 
data/Rakefile CHANGED
@@ -6,3 +6,5 @@ Rake::TestTask.new(:test) do |test|
6
6
  test.pattern = 'test/**/test_*.rb'
7
7
  test.verbose = true
8
8
  end
9
+
10
+ task :default => :test
data/lib/lps.rb CHANGED
@@ -1,6 +1,10 @@
1
1
  require "lps/version"
2
2
  require 'option_initializer'
3
3
 
4
+ # @!attribute [r] freq
5
+ # @return [Number] Loop frequency
6
+ # @!attribute [r] interval
7
+ # @return [Number] Loop interval (inverse of frequency)
4
8
  class LPS
5
9
  include OptionInitializer
6
10
  option_initializer! :freq, :interval => Numeric, :while => :&
@@ -13,24 +17,32 @@ class LPS
13
17
  raise ArgumentError, 'interval must be a non-negative number' if v < 0
14
18
  end
15
19
 
16
- # @param [Proc] &block Loop
20
+ attr_reader :freq, :interval
21
+
22
+ # Main loop
23
+ # @param [Proc] block Loop
17
24
  def loop &block
18
25
  ret = nil
19
26
  always = @while.nil?
27
+
20
28
  if @freq.nil?
21
29
  while always || @while.call
22
- ret = block.call
30
+ ret = block.call self
31
+
32
+ # Frequency set!
33
+ return loop(&block) if @freq
23
34
  end
24
35
  else
25
- sleep_interval = 1.0 / @freq
26
-
27
36
  nt = Time.now
28
37
  while always || @while.call
29
- nt += sleep_interval
30
- ret = block.call
38
+ ret = block.call self
39
+
40
+ # Frequency unset!
41
+ return loop(&block) unless @freq
31
42
 
32
- now = Time.now
33
- diff = nt - now
43
+ nt += @interval
44
+ now = Time.now
45
+ diff = nt - now
34
46
 
35
47
  if diff > 0.01
36
48
  sleep diff
@@ -42,9 +54,30 @@ class LPS
42
54
  ret
43
55
  end
44
56
 
57
+ # Changes loop frequency
58
+ # @param [Number|nil] f Loop frequency
59
+ # @return [Number|nil]
60
+ def freq= f
61
+ validate_options(:freq => f)
62
+
63
+ @freq, @interval = [f, f ? 1.0 / f : 0]
64
+ @freq
65
+ end
66
+
67
+ # Changes loop interval
68
+ # @param [Number] intv Loop interval
69
+ # @return [Number]
70
+ def interval= intv
71
+ validate_options(:interval => intv)
72
+
73
+ @freq, @interval = [(intv == 0) ? nil : 1.0 / intv, intv]
74
+ @interval
75
+ end
76
+
45
77
  # @param [Hash] opts Options Hash.
46
78
  # @option opts [Numeric] :freq Frequency of loop execution (loops/sec)
47
79
  # @option opts [#call] :cond Loop condition
80
+ # @private
48
81
  def initialize opts = {}
49
82
  validate_options opts
50
83
 
@@ -60,6 +93,7 @@ class LPS
60
93
  else
61
94
  nil
62
95
  end
96
+ @interval = @freq ? 1.0 / @freq : 0
63
97
  end
64
98
  end
65
99
 
@@ -1,3 +1,3 @@
1
1
  class LPS
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.1'
3
3
  end
@@ -19,4 +19,5 @@ Gem::Specification.new do |gem|
19
19
  gem.add_development_dependency 'test-unit'
20
20
  gem.add_development_dependency 'guard'
21
21
  gem.add_development_dependency 'guard-test'
22
+ gem.add_development_dependency 'rake'
22
23
  end
@@ -90,5 +90,57 @@ class TestLPS < Test::Unit::TestCase
90
90
  puts [ps, cnt].join ' => '
91
91
  end
92
92
  end
93
+
94
+ def test_freq=
95
+ st = Time.now
96
+ i = 0
97
+ LPS.freq(1).while { i < 100 }.loop do |lps|
98
+ print i += 1
99
+ print ' '
100
+
101
+ lps.freq = i
102
+ assert_equal i, lps.freq
103
+ end
104
+ assert ((Time.now - st) - (1...100).map { |e| 1.0 / e }.inject(:+)).abs < 0.1
105
+ end
106
+
107
+ def test_freq_to_nil
108
+ i = 0
109
+ st = Time.now
110
+ LPS.freq(1).while { i < 100 }.loop do |lps|
111
+ print i += 1
112
+ print ' '
113
+
114
+ lps.freq = nil
115
+ end
116
+ assert (Time.now - st) < 0.1
117
+ end
118
+
119
+ def test_interval=
120
+ st = Time.now
121
+ i = 0
122
+ LPS.interval(1).while { i < 100 }.loop do |lps|
123
+ print i += 1
124
+ print ' '
125
+
126
+ lps.interval = 1.0 / i
127
+ assert_equal 1.0 / i, lps.interval
128
+ end
129
+ assert ((Time.now - st) - (1...100).map { |e| 1.0 / e }.inject(:+)).abs < 0.1
130
+ end
131
+
132
+ def test_invalid_freq_interval
133
+ assert_raise(ArgumentError) do
134
+ LPS.freq(10).loop do |lps|
135
+ lps.freq = -1
136
+ end
137
+ end
138
+
139
+ assert_raise(ArgumentError) do
140
+ LPS.freq(10).loop do |lps|
141
+ lps.interval = -1
142
+ end
143
+ end
144
+ end
93
145
  end
94
146
 
metadata CHANGED
@@ -1,20 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lps
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
5
- prerelease:
4
+ version: 0.2.1
6
5
  platform: ruby
7
6
  authors:
8
7
  - Junegunn Choi
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-03-18 00:00:00.000000000 Z
11
+ date: 2013-10-02 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: option_initializer
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ~>
20
18
  - !ruby/object:Gem::Version
@@ -22,7 +20,6 @@ dependencies:
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
24
  - - ~>
28
25
  - !ruby/object:Gem::Version
@@ -30,49 +27,57 @@ dependencies:
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: test-unit
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - '>='
36
32
  - !ruby/object:Gem::Version
37
33
  version: '0'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - '>='
44
39
  - !ruby/object:Gem::Version
45
40
  version: '0'
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: guard
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ! '>='
45
+ - - '>='
52
46
  - !ruby/object:Gem::Version
53
47
  version: '0'
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ! '>='
52
+ - - '>='
60
53
  - !ruby/object:Gem::Version
61
54
  version: '0'
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: guard-test
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
- - - ! '>='
59
+ - - '>='
68
60
  - !ruby/object:Gem::Version
69
61
  version: '0'
70
62
  type: :development
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
- - - ! '>='
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
76
81
  - !ruby/object:Gem::Version
77
82
  version: '0'
78
83
  description: Rate-controlled loop execution
@@ -83,6 +88,7 @@ extensions: []
83
88
  extra_rdoc_files: []
84
89
  files:
85
90
  - .gitignore
91
+ - .travis.yml
86
92
  - Gemfile
87
93
  - Guardfile
88
94
  - LICENSE
@@ -94,27 +100,27 @@ files:
94
100
  - test/test_lps.rb
95
101
  homepage: https://github.com/junegunn/lps
96
102
  licenses: []
103
+ metadata: {}
97
104
  post_install_message:
98
105
  rdoc_options: []
99
106
  require_paths:
100
107
  - lib
101
108
  required_ruby_version: !ruby/object:Gem::Requirement
102
- none: false
103
109
  requirements:
104
- - - ! '>='
110
+ - - '>='
105
111
  - !ruby/object:Gem::Version
106
112
  version: '0'
107
113
  required_rubygems_version: !ruby/object:Gem::Requirement
108
- none: false
109
114
  requirements:
110
- - - ! '>='
115
+ - - '>='
111
116
  - !ruby/object:Gem::Version
112
117
  version: '0'
113
118
  requirements: []
114
119
  rubyforge_project:
115
- rubygems_version: 1.8.25
120
+ rubygems_version: 2.0.3
116
121
  signing_key:
117
- specification_version: 3
122
+ specification_version: 4
118
123
  summary: Rate-controlled loop execution
119
124
  test_files:
120
125
  - test/test_lps.rb
126
+ has_rdoc: