Skip to content

Commit

Permalink
Explore nat strict: 1 and "every 17 hours", gh-86
Browse files Browse the repository at this point in the history
  • Loading branch information
jmettraux committed Aug 26, 2023
1 parent e346926 commit ec91e88
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
33 changes: 29 additions & 4 deletions lib/fugit/nat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -642,9 +642,9 @@ def to_crons(opts)
fail(ArgumentError.new(
"multiple crons in #{opts[:_s].inspect} - #{@slots.inspect}"))
elsif multi == true
hms.collect { |hm| parse_cron(hm) }
hms.collect { |hm| parse_cron(hm, opts) }
else
parse_cron(hms.first)
parse_cron(hms.first, opts)
end
end

Expand Down Expand Up @@ -678,7 +678,7 @@ def determine_hms
.to_a
end

def parse_cron(hm)
def parse_cron(hm, opts)

a = [
slot(:second, '0'),
Expand All @@ -695,7 +695,32 @@ def parse_cron(hm)
.collect { |e| e.uniq.sort.collect(&:to_s).join(',') }
.join(' ')

Fugit::Cron.parse(s)
c = Fugit::Cron.parse(s)

if opts[:strict]
restrict(a, c)
else
c
end
end

# Return nil if the cron is "not strict"
#
# For example, "0 0/17 * * *" (gh-86) is a perfectly valid
# cron string, but makes not much sense when derived via `.parse_nat`
# from "every 17 hours".
#
# It happens here because it's nat being strict, not cron.
#
def restrict(a, cron)

if m = ((a[1] && a[1][0]) || '').match(/^(\d+|\*)\/(\d+)$/)
#p m
sla = m[1].to_i
return nil unless [ 1, 2, 3, 4, 5, 6, 8, 12 ].include?(sla)
end

cron
end

def slot(key, default)
Expand Down
19 changes: 15 additions & 4 deletions spec/nat_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -302,15 +302,26 @@

].each do |input|

it "rejects (returns nil) for #{input.inspect}" do
it "rejects (returns nil) for #{input.inspect}" do

expect(Fugit::Nat.parse(input)).to eq(nil)
end
end

#context 'strict:' do
# TODO
#end
context 'strict: true' do

[

'every 17 hours',

].each do |input|

it "rejects (returns nil) for #{input.inspect}" do

expect(Fugit::Nat.parse(input, strict: true)).to eq(nil)
end
end
end
end

describe '.do_parse' do
Expand Down

0 comments on commit ec91e88

Please sign in to comment.