chrono 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6a6bc0e19633446a8b89579f6cbd93c75bfde77d
4
- data.tar.gz: e4b5d84609564d00fc9f7a052a36a9911780c494
3
+ metadata.gz: 31bceadc7e5050d3837628b9388d98b748367baa
4
+ data.tar.gz: c026bae471b3daacadd4ae5d328238f5ccdf51b9
5
5
  SHA512:
6
- metadata.gz: c942eeb72f2817153a52a33b25d4a4eee2e51bf6d94f937366bfa319f0075f23b9787c2a6a1f74a9e128a7f2b71b9fc22b2a4fe70a840e01234645cfe5634706
7
- data.tar.gz: af90e4f872ad91502de4e0aea4c62702437d720b22afd4350e2088dd5da36923f543f028db948e636659c3fafd8da96e33c5f2f2adb80343338845cb257fd140
6
+ metadata.gz: b161f9fcd7187abea95639062772b8979d343b714fce640a828029839951290f5e70e7ab3e035294f6ad8d9ecd654a7e17a1dcad128a6065a854cd3f4a89a362
7
+ data.tar.gz: a9797e3245d9a7700be75855526e963e723245d002a1954dc93c1e6a2e473b110b5dcb07c4949c6f8b12eca92c907cda767da264caa8da4de2d19d59ef3dff28
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ rvm:
2
+ - 1.9.3
3
+ - 2.0.0
4
+ - 2.1.0
5
+ branches:
6
+ only:
7
+ - master
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## 0.0.2
2
+ * Chrono::Trigger was added
3
+
4
+ ## 0.0.1
5
+ * Chrono::Iterator was added
data/README.md CHANGED
@@ -1,20 +1,33 @@
1
- # Chrono
1
+ # Chrono [![Build Status](https://travis-ci.org/r7kamura/chrono.png)](https://travis-ci.org/r7kamura/chrono) [![Code Climate](https://codeclimate.com/github/r7kamura/chrono.png)](https://codeclimate.com/github/r7kamura/chrono) [![Coverage Status](https://coveralls.io/repos/r7kamura/chrono/badge.png?branch=master&cache-version=1)](https://coveralls.io/r/r7kamura/chrono?branch=master)
2
+
2
3
  Provides a chain of logics about chronology.
3
4
 
4
- ## Chrono::Iterator
5
- Parse cron syntax and determine next scheduled run.
5
+ ## Trigger
6
+ Waits till scheduled time and then triggers a given job. `#run` is a periodic version of `#once`.
6
7
 
7
8
  ```ruby
8
- require "chrono"
9
+ # Trigger
10
+ trigger = Chrono::Trigger.new("30 * * * *") { Time.now }
11
+ trigger.once #=> 2000-01-01 00:30:00
12
+ trigger.run #=> 2000-01-01 01:30:00
13
+ #=> 2000-01-01 02:30:00
14
+ #=> 2000-01-01 03:30:00
15
+ #=> ...
16
+ ```
9
17
 
18
+ ## Iterator
19
+ Parses cron syntax and determines next scheduled run.
20
+
21
+ ```ruby
10
22
  iterator = Chrono::Iterator.new("30 * * * *")
11
23
  iterator.next #=> 2000-01-01 00:30:00
12
- iterator.next #=> 2000-01-02 00:30:00
13
- iterator.next #=> 2000-01-03 00:30:00
24
+ iterator.next #=> 2000-01-01 01:30:00
25
+ iterator.next #=> 2000-01-01 02:30:00
14
26
  ```
15
27
 
28
+ ## Syntax
16
29
  The following syntax is supported.
17
- See [examples](https://github.com/r7kamura/chrono/blob/master/spec/chrono/iterator_spec.rb)
30
+ See the [examples](https://github.com/r7kamura/chrono/blob/master/spec/chrono/iterator_spec.rb)
18
31
  for more details.
19
32
 
20
33
  * (*) Asterisk
data/Rakefile CHANGED
@@ -1,2 +1,5 @@
1
1
  require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
2
3
 
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task default: :spec
data/chrono.gemspec CHANGED
@@ -17,7 +17,9 @@ Gem::Specification.new do |spec|
17
17
  spec.require_paths = ["lib"]
18
18
 
19
19
  spec.add_dependency "activesupport"
20
- spec.add_development_dependency "bundler", "~> 1.6"
20
+ spec.add_development_dependency "bundler"
21
+ spec.add_development_dependency "coveralls"
21
22
  spec.add_development_dependency "rake"
22
23
  spec.add_development_dependency "rspec", "2.14.1"
24
+ spec.add_development_dependency "simplecov"
23
25
  end
@@ -17,10 +17,6 @@ module Chrono
17
17
 
18
18
  private
19
19
 
20
- def range
21
- raise NotImplementedError
22
- end
23
-
24
20
  def interpolated
25
21
  source.gsub("*", "#{range.first}-#{range.last}")
26
22
  end
@@ -38,10 +38,6 @@ module Chrono
38
38
  @time ||= now + 1.minute
39
39
  end
40
40
 
41
- def fields
42
- @fields ||= source.split(" ")
43
- end
44
-
45
41
  def schedule
46
42
  @schedule ||= Schedule.new(source)
47
43
  end
@@ -74,10 +70,6 @@ module Chrono
74
70
  self.time = time.tomorrow.at_beginning_of_day
75
71
  end
76
72
 
77
- def carry_wday
78
- self.time = time.next_week.at_beginning_of_week
79
- end
80
-
81
73
  def carry_hour
82
74
  self.time = (time + 1.hour).at_beginning_of_hour
83
75
  end
@@ -0,0 +1,37 @@
1
+ module Chrono
2
+ class Trigger
3
+ attr_reader :block, :source
4
+
5
+ def initialize(source, &block)
6
+ @source = source
7
+ @block = block || -> {}
8
+ end
9
+
10
+ def once
11
+ wait
12
+ call
13
+ end
14
+
15
+ def run
16
+ loop { once }
17
+ end
18
+
19
+ private
20
+
21
+ def call
22
+ block.call
23
+ end
24
+
25
+ def iterator
26
+ @iterator ||= Iterator.new(source)
27
+ end
28
+
29
+ def wait
30
+ sleep(period)
31
+ end
32
+
33
+ def period
34
+ iterator.next - Time.now
35
+ end
36
+ end
37
+ end
@@ -1,3 +1,3 @@
1
1
  module Chrono
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/chrono.rb CHANGED
@@ -7,4 +7,5 @@ require "chrono/fields/wday"
7
7
  require "chrono/iterator"
8
8
  require "chrono/next_time"
9
9
  require "chrono/schedule"
10
+ require "chrono/trigger"
10
11
  require "chrono/version"
@@ -8,6 +8,8 @@ describe Chrono::Iterator do
8
8
  "2000-01-01 23:59:00", "2000-01-02 00:00:00", "* * * * *",
9
9
  "2000-01-31 23:59:00", "2000-02-01 00:00:00", "* * * * *",
10
10
  "2000-12-31 23:59:00", "2001-01-01 00:00:00", "* * * * *",
11
+ "2000-01-01 00:00:00", "2000-01-02 00:00:00", "* * 2 * *",
12
+ "2000-01-01 00:00:00", "2000-02-01 00:00:00", "* * * 2 *",
11
13
  "2000-01-01 00:00:00", "2000-01-01 00:15:00", "*/15 * * * *",
12
14
  "2000-01-01 00:01:00", "2000-01-01 00:15:00", "*/15 * * * *",
13
15
  "2000-01-01 00:00:00", "2000-01-01 00:31:00", "*/31 * * * *",
@@ -0,0 +1,40 @@
1
+ require "spec_helper"
2
+
3
+ describe Chrono::Trigger do
4
+ let(:trigger) do
5
+ described_class.new(source, &block)
6
+ end
7
+
8
+ let(:source) do
9
+ "* * * * *"
10
+ end
11
+
12
+ let(:block) do
13
+ -> {}
14
+ end
15
+
16
+ describe "#once" do
17
+ it "waits till scheduled time and then triggers a given job only once" do
18
+ block.should_receive(:call)
19
+ trigger.should_receive(:sleep)
20
+ trigger.once
21
+ end
22
+ end
23
+
24
+ # Stub Trigger#loop behavior to avoid blocking main process.
25
+ describe "#run" do
26
+ before do
27
+ trigger.stub(:loop) do |&block|
28
+ 2.times do
29
+ block.call
30
+ end
31
+ end
32
+ end
33
+
34
+ it "waits till scheduled time and then triggers a given job periodically" do
35
+ block.should_receive(:call).twice
36
+ trigger.should_receive(:sleep).twice
37
+ trigger.run
38
+ end
39
+ end
40
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,12 @@
1
+ require "simplecov"
2
+ require "coveralls"
3
+
4
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
5
+ SimpleCov::Formatter::HTMLFormatter,
6
+ Coveralls::SimpleCov::Formatter
7
+ ]
8
+ SimpleCov.start
9
+
1
10
  $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
2
11
  require "chrono"
3
12
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chrono
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-18 00:00:00.000000000 Z
11
+ date: 2014-04-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -28,16 +28,30 @@ dependencies:
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
32
39
  - !ruby/object:Gem::Version
33
- version: '1.6'
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: coveralls
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
34
48
  type: :development
35
49
  prerelease: false
36
50
  version_requirements: !ruby/object:Gem::Requirement
37
51
  requirements:
38
- - - "~>"
52
+ - - ">="
39
53
  - !ruby/object:Gem::Version
40
- version: '1.6'
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: rake
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -66,6 +80,20 @@ dependencies:
66
80
  - - '='
67
81
  - !ruby/object:Gem::Version
68
82
  version: 2.14.1
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
69
97
  description:
70
98
  email:
71
99
  - r7kamura@gmail.com
@@ -74,6 +102,8 @@ extensions: []
74
102
  extra_rdoc_files: []
75
103
  files:
76
104
  - ".gitignore"
105
+ - ".travis.yml"
106
+ - CHANGELOG.md
77
107
  - Gemfile
78
108
  - LICENSE.txt
79
109
  - README.md
@@ -89,8 +119,10 @@ files:
89
119
  - lib/chrono/iterator.rb
90
120
  - lib/chrono/next_time.rb
91
121
  - lib/chrono/schedule.rb
122
+ - lib/chrono/trigger.rb
92
123
  - lib/chrono/version.rb
93
124
  - spec/chrono/iterator_spec.rb
125
+ - spec/chrono/trigger_spec.rb
94
126
  - spec/spec_helper.rb
95
127
  homepage: https://github.com/r7kamura/chrono
96
128
  licenses:
@@ -118,4 +150,5 @@ specification_version: 4
118
150
  summary: Provides a chain of logics about chronology.
119
151
  test_files:
120
152
  - spec/chrono/iterator_spec.rb
153
+ - spec/chrono/trigger_spec.rb
121
154
  - spec/spec_helper.rb