enat 0.1.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/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ Gemfile.lock
2
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
data/README.markdown ADDED
@@ -0,0 +1,27 @@
1
+ Every Now And Then
2
+ ==================
3
+
4
+ It's quite simple, really - it lets you do something every specified interval of time. When supplied with a block, it
5
+ yields the iteration count (starts at zero).
6
+
7
+ If the operation inside the block takes longer than the specified interval, then the next iteration will begin
8
+ immediately after the last.
9
+
10
+ To exit, simply break out of the loop.
11
+
12
+ Examples
13
+ ========
14
+
15
+ require 'enat'
16
+
17
+ every 4.2.minutes do |i|
18
+ puts "wheee! i've done this #{i} times!"
19
+ break if i > 5
20
+ end
21
+
22
+ Installation
23
+ ------------
24
+
25
+ $ bundle install
26
+ $ gem build enat.gemspec
27
+ $ gem install enat
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ RSpec::Core::RakeTask.new(:spec)
4
+
5
+ task :default => :spec
data/enat.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ lib = File.expand_path('../lib/', __FILE__)
2
+ $:.unshift lib unless $:.include?(lib)
3
+
4
+ require 'enat/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "enat"
8
+ s.version = Enat::VERSION
9
+ s.platform = Gem::Platform::RUBY
10
+
11
+ s.authors = ["Hari Gopal"]
12
+ s.email = "harigopal1@gmail.com"
13
+ s.summary = "Does something every specified interval."
14
+ s.description = "Executes a block of code at intervals of time, without guarantee of timing."
15
+ s.homepage = "http://github.com/harigopal/enat"
16
+
17
+ s.required_ruby_version = '>= 1.9.2'
18
+
19
+ s.add_development_dependency "rake"
20
+ s.add_development_dependency "rspec"
21
+
22
+ s.add_dependency "activesupport"
23
+
24
+ s.files = `git ls-files`.split("\n") - ["Gemfile.lock", ".rvmrc"]
25
+ s.test_files = `git ls-files -- spec/*`.split("\n")
26
+ s.require_paths = ["lib"]
27
+ end
data/examples/test.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'active_support/time'
2
+ require 'enat/import'
3
+
4
+ every(2.seconds) do |iteration|
5
+ puts "Hello no. #{iteration}!"
6
+ end
data/lib/enat/enat.rb ADDED
@@ -0,0 +1,29 @@
1
+ # Every Now And Then
2
+ module Enat
3
+ # Yields to block every supplied interval
4
+ #
5
+ # @param [Fixnum, Float] interval_in_seconds minimum time interval between yields
6
+ def every(interval_in_seconds)
7
+ interval_in_seconds = interval_in_seconds.to_f
8
+
9
+ unless interval_in_seconds > 0
10
+ raise EnatError, "Interval must be non-zero"
11
+ end
12
+
13
+ iteration = 0
14
+
15
+ loop do
16
+ start_time = Time.now.to_f
17
+
18
+ yield iteration
19
+ iteration += 1
20
+
21
+ time_elapsed = Time.now.to_f - start_time
22
+ if time_elapsed >= interval_in_seconds
23
+ next
24
+ else
25
+ sleep(interval_in_seconds - time_elapsed)
26
+ end
27
+ end
28
+ end
29
+ end
data/lib/enat/error.rb ADDED
@@ -0,0 +1,3 @@
1
+ module Enat
2
+ class EnatError < StandardError; end
3
+ end
@@ -0,0 +1,3 @@
1
+ module Enat
2
+ VERSION = "0.1.0"
3
+ end
data/lib/enat.rb ADDED
@@ -0,0 +1,9 @@
1
+ # Gems
2
+ require "active_support/time"
3
+
4
+ # Local
5
+ require_relative "enat/version"
6
+ require_relative "enat/error"
7
+ require_relative "enat/enat"
8
+
9
+ include Enat
data/spec/enat_spec.rb ADDED
@@ -0,0 +1,19 @@
1
+ require "rspec"
2
+
3
+ require_relative '../lib/enat'
4
+
5
+ class DummyClass
6
+ include Enat
7
+ end
8
+
9
+ describe DummyClass do
10
+ it { should respond_to(:every).with(1).argument }
11
+
12
+ context "when supplied interval is zero" do
13
+ it "should raise error" do
14
+ expect {
15
+ subject.every("foo") { }
16
+ }.to raise_error(Enat::EnatError, "Interval must be non-zero")
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ require 'simplecov'
2
+
3
+ SimpleCov.start { add_filter 'vendor' }
data/test.rb ADDED
@@ -0,0 +1,8 @@
1
+ require_relative 'lib/enat'
2
+
3
+ every 5.seconds do |iteration|
4
+ sleep_time = rand(5)
5
+ puts "#{Time.now.to_f}: Sleeping for #{sleep_time} seconds... (Iteration #{iteration})"
6
+ sleep sleep_time
7
+ puts "#{Time.now.to_f}: Done sleeping."
8
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: enat
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Hari Gopal
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: activesupport
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Executes a block of code at intervals of time, without guarantee of timing.
63
+ email: harigopal1@gmail.com
64
+ executables: []
65
+ extensions: []
66
+ extra_rdoc_files: []
67
+ files:
68
+ - .gitignore
69
+ - Gemfile
70
+ - README.markdown
71
+ - Rakefile
72
+ - enat.gemspec
73
+ - examples/test.rb
74
+ - lib/enat.rb
75
+ - lib/enat/enat.rb
76
+ - lib/enat/error.rb
77
+ - lib/enat/version.rb
78
+ - spec/enat_spec.rb
79
+ - spec/spec_helper.rb
80
+ - test.rb
81
+ homepage: http://github.com/harigopal/enat
82
+ licenses: []
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: 1.9.2
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 1.8.24
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: Does something every specified interval.
105
+ test_files:
106
+ - spec/enat_spec.rb
107
+ - spec/spec_helper.rb
108
+ has_rdoc: