when-cron 1.0.0 → 1.0.1

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: c6cb41b52b68db876230191f75c10dae240f4d40
4
- data.tar.gz: e8c00085e9e93d136787e99e7ae85909c14e14da
3
+ metadata.gz: 7be791179d6810b1a235984131c4c440b13c945f
4
+ data.tar.gz: 56b1bf74f137172458f068778a8ec123fa43f38a
5
5
  SHA512:
6
- metadata.gz: b9f877e0558fa5c2689ec41704db1accc2808bf4f23d2dcde1cc37eba7a4c3895be94ed5be7c0702757fff12c6d47973de604e9b3dd795c8f375a290c88ba7eb
7
- data.tar.gz: 8f3c9b5015c529772246b44886256bfc89e72f65f41f32f849e5d6fd501e5e2ee8f306a33bf6780cae6d0bf3150d9eb3985f069402821a1365608d1dbf56b955
6
+ metadata.gz: c2cf26e262f12206f73ed74be0c4c8cf5a0179ea74f28a82b116c19e5cdb95983b2735396aab3cc5dd989719ea6eef69794be9e28d393f6908cbb733a9121c3e
7
+ data.tar.gz: 616f6a08f514dfd476880bce095335bcc4362ae6edfc82c6cf33aaa1b76c23a0a685f71709cbb15fcae15e3ba2c6fc1a80f0cca72ac239315062e1f15cb7abcf
data/README.md CHANGED
@@ -1,12 +1,12 @@
1
- # When
1
+ # When-Cron
2
2
 
3
- TODO: Write a gem description
3
+ A basic cron implementation in Ruby.
4
4
 
5
5
  ## Installation
6
6
 
7
7
  Add this line to your application's Gemfile:
8
8
 
9
- gem 'when'
9
+ gem 'when-cron'
10
10
 
11
11
  And then execute:
12
12
 
@@ -14,11 +14,25 @@ And then execute:
14
14
 
15
15
  Or install it yourself as:
16
16
 
17
- $ gem install when
17
+ $ gem install when-cron
18
18
 
19
19
  ## Usage
20
20
 
21
- TODO: Write usage instructions here
21
+ Compare to anything that has an interface like Ruby's Time class with ==.
22
+
23
+ ```When::Cron.new('* * * * *') == Time.now # => true```
24
+
25
+ Only numbers or * are supported for values, only - , / are supported as operators.
26
+
27
+ ```When::Cron.valid?('a b c d e') # => false```
28
+
29
+ When::Cron will allow nonsensical values (exa. 100 in the minute spot), but because the minute will never match, == will always return false.
30
+
31
+ ```When::Cron.new('100 * * * *') == Time.now # => false```
32
+
33
+ When::Cron will raise an error for extremely nonsensical cron strings.
34
+
35
+ ```When::Cron.new('this is getting ridiculous') # => raises When::Cron::InvalidString```
22
36
 
23
37
  ## Contributing
24
38
 
@@ -1,11 +1,19 @@
1
1
  module When
2
2
  class Cron
3
+ def self.valid?(cron)
4
+ begin
5
+ new(cron)
6
+ rescue When::CronPart::InvalidString
7
+ return false
8
+ end
9
+ true
10
+ end
11
+
3
12
  def initialize(cron)
4
- @cron = cron
13
+ parse(cron)
5
14
  end
6
15
 
7
16
  def ==(time)
8
- parsed?
9
17
  matches = []
10
18
  matches << (@minute == time.min)
11
19
  matches << (@hour == time.hour)
@@ -15,12 +23,6 @@ module When
15
23
  matches.all?
16
24
  end
17
25
 
18
- private
19
-
20
- def parsed?
21
- @parsed ||= parse(@cron)
22
- end
23
-
24
26
  def parse(string)
25
27
  strings = string.split(' ')
26
28
  @minute = CronPart.new(strings[0])
@@ -1,31 +1,39 @@
1
1
  module When
2
2
  class CronPart
3
+ class InvalidString < StandardError; end
4
+
3
5
  def initialize(cron_part)
4
- @cron_part = cron_part
6
+ @part = parse(cron_part)
5
7
  end
6
8
 
7
9
  def ==(int)
8
- part == int
10
+ @part == int
9
11
  end
10
12
 
11
13
  private
12
14
 
13
- def part
14
- @part ||= parse(@cron_part)
15
- end
16
-
17
15
  def parse(cron_part)
18
16
  if cron_part =~ /,/
19
17
  CronArray.new(cron_part.split(',').map { |s| parse(s) })
20
- elsif cron_part =~ /^((\d+|\*)-(\d+|\*)|\*)\/\d+$/
18
+ elsif cron_part =~ /\//
21
19
  CronInterval.new(*cron_part.split('/').map { |s| parse(s) })
22
- elsif cron_part =~ /^(\d+|\*)-(\d+|\*)$/
20
+ elsif cron_part =~ /-/
23
21
  CronRange.new(*cron_part.split('-').map { |s| parse(s) })
24
22
  elsif cron_part == '*'
25
23
  Wildcard.new
26
24
  else
27
- cron_part.to_i
25
+ to_valid_int(cron_part)
26
+ end
27
+ end
28
+
29
+ def to_valid_int(cron_part)
30
+ int = cron_part.to_i
31
+ if int.to_s == cron_part
32
+ int
33
+ else
34
+ raise When::CronPart::InvalidString, "found #{cron_part.inspect}: only * or integer values and / , - operators are supported"
28
35
  end
29
36
  end
30
37
  end
38
+
31
39
  end
@@ -2,6 +2,12 @@ require 'spec_helper'
2
2
  include When
3
3
 
4
4
  describe CronPart do
5
+ context 'invalid value' do
6
+ it 'raises an error' do
7
+ expect { CronPart.new('a') }.to raise_error
8
+ end
9
+ end
10
+
5
11
  context 'basic number' do
6
12
  let(:cron_part) { CronPart.new('5') }
7
13
 
@@ -4,6 +4,16 @@ include When
4
4
  describe Cron do
5
5
  let(:now) { Time.new(2013, 6, 15, 12, 30, 30) }
6
6
 
7
+ describe '.valid?' do
8
+ it 'returns false for strings it cannot interpret' do
9
+ expect(Cron.valid?('* * * * a,b')).to be false
10
+ end
11
+
12
+ it 'returns true for cron string it can interpret' do
13
+ expect(Cron.valid?('* * * * 1')).to be true
14
+ end
15
+ end
16
+
7
17
  describe '#==' do
8
18
  context 'simple cron' do
9
19
  let(:cron) { Cron.new('30 12 15 6 6') }
data/when-cron.gemspec CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "when-cron"
7
- spec.version = '1.0.0'
7
+ spec.version = '1.0.1'
8
8
  spec.authors = ["TH"]
9
9
  spec.email = ["tylerhartland7@gmail.com"]
10
10
  spec.description = %q{A basic cron implementation.}
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: when-cron
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - TH
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-10 00:00:00.000000000 Z
11
+ date: 2014-03-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler