Skip to content

Commit

Permalink
Bring in Fugit.parse_cronish and .do_parse_cronish
Browse files Browse the repository at this point in the history
  • Loading branch information
jmettraux committed Dec 5, 2022
1 parent d3515d5 commit 3702aae
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

## fugit 1.8.0 not yet released

* Introduce Fugit.parse_cronish and .do_parse_cronish, gh-70


## fugit 1.7.2 released 2022-11-03

Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,23 @@ Fugit.parse_nat('every day at noon').class # ==> ::Fugit::Cron

As `Fugit.parse(s)` returns nil when it doesn't grok its input, and `Fugit.do_parse(s)` fails when it doesn't grok, each of the `parse_` methods has its partner `do_parse_` method.

## parse_cronish and do_parse_cronish

Sometimes you know a cron expression or an "every" natural expression will come in and you want to discard the rest.

```
require 'fugit'
Fugit.parse_cronish('0 0 1 jan *').class # ==> ::Fugit::Cron
Fugit.parse_cronish('every saturday at noon').class # ==> ::Fugit::Cron
Fugit.parse_cronish('12y12M') # ==> nil
```

`.parse_cronish(s)` will return a `Fugit::Cron` instance or else nil.

`.do_parse_cronish(s)` will return a `Fugit::Cron` instance or else fail with an `ArgumentError`.

## `Fugit::Cron`

A class `Fugit::Cron` to parse cron strings and then `#next_time` and `#previous_time` to compute the next or the previous occurrence respectively.
Expand Down
13 changes: 13 additions & 0 deletions lib/fugit/parse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ def do_parse(s, opts={})
fail(ArgumentError.new("found no time information in #{s.inspect}"))
end

def parse_cronish(s, opts={})

r = parse_cron(s) || parse_nat(s, opts)

r.is_a?(::Fugit::Cron) ? r : nil
end

def do_parse_cronish(s, opts={})

parse_cronish(s) ||
fail(ArgumentError.new("not cron or 'natural' cron string: #{s.inspect}"))
end

def determine_type(s)

case self.parse(s)
Expand Down
56 changes: 56 additions & 0 deletions spec/parse_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,62 @@
end
end

CRONISHES = {

'* * * * *' => '* * * * *',
'every day' => '0 0 * * *',

'2022-12-5 11:32' => ArgumentError,
'nada' => ArgumentError,
'100 * * * *' => ArgumentError,
}

describe '.parse_cronish' do

CRONISHES.each do |k, v|

if v.is_a?(String)

it "parses #{k.inspect} to #{v.inspect}" do

r = Fugit.parse_cronish(k)

expect(r.class).to eq(Fugit::Cron)
expect(r.original).to eq(v)
end
else

it "returns nil for #{k.inspect}" do

expect(Fugit.parse_cronish(k)).to eq(nil)
end
end
end
end

describe '.do_parse_cronish' do

CRONISHES.each do |k, v|

if v.is_a?(String)

it "parses #{k.inspect} to #{v.inspect}" do

r = Fugit.do_parse_cronish(k)

expect(r.class).to eq(Fugit::Cron)
expect(r.original).to eq(v)
end
else

it "fails on #{k.inspect}" do

expect { Fugit.do_parse_cronish(k) }.to raise_error(v)
end
end
end
end

describe '.determine_type' do

it 'returns nil if it cannot determine' do
Expand Down

0 comments on commit 3702aae

Please sign in to comment.