Skip to content

Commit

Permalink
Explore HyphenRange#to_a
Browse files Browse the repository at this point in the history
  • Loading branch information
jmettraux committed Mar 19, 2024
1 parent 409873f commit e9bab7f
Showing 1 changed file with 38 additions and 4 deletions.
42 changes: 38 additions & 4 deletions lib/fugit/cron.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ class Cron
:original, :zone)
attr_reader(
:seconds, :minutes, :hours, :monthdays, :months, :weekdays, :timezone)
#attr_reader :original, :zone
#attr_reader :weekdays, :timezone
#def seconds; @seconds ? @seconds.map(&:to_a).flatten : nil; end
#def minutes; @minutes ? @minutes.map(&:to_a).flatten : nil; end
#def hours; @hours ? @hours.map(&:to_a).flatten : nil; end
#def monthdays; @monthdays ? @monthdays.map(&:to_a).flatten : nil; end
#def months; @months ? @months.map(&:to_a).flatten : nil; end

class << self

Expand Down Expand Up @@ -53,7 +60,8 @@ def do_parse(s)
def to_cron_s

@cron_s ||= [
@seconds == [ 0 ] ? nil : (@seconds || [ '*' ]).join(','),
#@seconds == [ 0 ] ? nil : (@seconds || [ '*' ]).join(','),
@seconds.all?(&:zero?) ? nil : (@seconds || [ '*' ]).join(','),
(@minutes || [ '*' ]).join(','),
(@hours || [ '*' ]).join(','),
(@monthdays || [ '*' ]).join(','),
Expand Down Expand Up @@ -630,21 +638,30 @@ class FieldRange

attr_reader :key, :type
attr_reader :sta, :edn, :sla
attr_reader :min, :max

def initialize(key, type, a)

@key = key
@type = type
@a = a
min, max = case key

@min, @max = case key
when :hours then [ 0, 23 ]
when :monthdays then [ 1, 31 ]
when :months then [ 1, 12 ]
else [ 0, 59 ]; end

@sta = a[0]
@edn = a[2] || a[0]
@sla = a[3] || 1
@sta = min if @sta == nil || @sta < min
@edn = max if @edn == nil || @edn > max

@sta = @min if @sta == nil || @sta < @min
@edn = @max if @edn == nil || @edn > @max
end

def zero?
@sta == 0 && @edn == 0 && sla == 1
end

def inspect
Expand All @@ -665,6 +682,20 @@ def next_after(i)
def first
expanded.first
end
def to_a
expanded
end
def to_s
if sta == edn
sta.to_s
elsif sta == 1 && edn < 0
edn.to_s
elsif sta == min && edn == max && sla == 1
'*'
else
"#{sta}-#{edn}/#{sla}"
end
end
protected
def expanded
@expanded ||= (@sta..@edn).step(@sla).to_a
Expand All @@ -675,6 +706,9 @@ class TildeRange < FieldRange
def initialize(key, a)
super(key, :tilde, a)
end
def to_s
"#{sta}~#{edn}/#{sla}"
end
end

def make_range(key, a)
Expand Down

0 comments on commit e9bab7f

Please sign in to comment.