Permalink
Browse files

let Rufus::Scheduler.parse use Chronic if present

for gh-118
  • Loading branch information...
1 parent 136e744 commit 8881722ef1de864825de800569ff1d8145927777 @jmettraux committed Mar 21, 2014
Showing with 59 additions and 4 deletions.
  1. +5 −0 CHANGELOG.txt
  2. +4 −0 TODO.txt
  3. +8 −2 lib/rufus/scheduler/util.rb
  4. +1 −0 rufus-scheduler.gemspec
  5. +41 −2 spec/parse_spec.rb
View
@@ -2,6 +2,11 @@
= rufus-scheduler CHANGELOG.txt
+== rufus-scheduler - 3.0.8 not yet released
+
+- let Rufus::Scheduler.parse use Chronic if present
+
+
== rufus-scheduler - 3.0.7 released 2014/03/18
- implement Scheduler #occurrences and #timeline, inspired by kreynolds
View
@@ -145,3 +145,7 @@
end
end
+~~~
+
+[ ] scheduler.at('chronic string', chronic_options...)
+
@@ -31,13 +31,14 @@ class Scheduler
# time and string methods
#++
- def self.parse(o)
+ def self.parse(o, opts={})
- opts = { :no_error => true }
+ opts[:no_error] = true
parse_cron(o, opts) ||
parse_in(o, opts) || # covers 'every' schedule strings
parse_at(o, opts) ||
+ parse_chronic(o, opts) ||
raise(ArgumentError.new("couldn't parse \"#{o}\""))
end
@@ -78,6 +79,11 @@ def self.parse_at(o, opts={})
raise se
end
+ def self.parse_chronic(o, opts)
+
+ defined?(Chronic) ? Chronic.parse(o, opts) : nil
+ end
+
def self.parse_cron(o, opts)
CronLine.new(o)
@@ -30,6 +30,7 @@ job scheduler for Ruby (at, cron, in and every jobs).
s.add_development_dependency 'rake'
s.add_development_dependency 'rspec', '>= 2.13.0'
+ s.add_development_dependency 'chronic'
s.require_path = 'lib'
View
@@ -12,8 +12,8 @@
describe '.parse' do
- def parse(s)
- Rufus::Scheduler.parse(s)
+ def parse(s, opts={})
+ Rufus::Scheduler.parse(s, opts)
end
it 'parses duration strings' do
@@ -77,6 +77,45 @@ def parse(s)
parse('nada')
}.should raise_error(ArgumentError, 'couldn\'t parse "nada"')
end
+
+ def with_chronic(&block)
+ require 'chronic'
+ Object.const_set(:Khronic, Chronic) unless defined?(Khronic)
+ Object.const_set(:Chronic, Khronic) unless defined?(Chronic)
+ block.call
+ ensure
+ Object.send(:remove_const, :Chronic)
+ end
+
+ def without_chronic(&block) # for quick counter-tests ;-)
+ block.call
+ end
+
+ it 'does not use Chronic if not present' do
+
+ lambda { parse('03/27/2014 07:52:47') }.should raise_error(ArgumentError)
+ end
+
+ it 'uses Chronic if present' do
+
+ with_chronic do
+
+ t = parse('03/27/2014 07:52:47 PM')
+
+ t.strftime('%Y-%m-%d %H:%M:%S').should == '2014-03-27 19:52:47'
+ end
+ end
+
+ it 'passes options to Chronic' do
+
+ with_chronic do
+
+ t = parse('monday', :context => :past)
+
+ t.wday.should == 1
+ t.should < Time.now
+ end
+ end
end
describe '.parse_duration' do

0 comments on commit 8881722

Please sign in to comment.