class Fugit::Nat::SlotGroup

Constants

SLASH_REGEX

Public Class Methods

new(slots) click to toggle source
# File lib/fugit/nat.rb, line 622
      def initialize(slots)

#puts "SlotGroup.new " + slots.inspect
        @slots = {}
        @hms = []

        slots.each do |s|
          if s.key == :hm
            #ls = @hms.last; @hms.pop if ls && ls.key == :hm && ls.weak == true
            @hms << s
          elsif hs = @slots[s.key]
            hs.append(s)
          else
            @slots[s.key] = s
          end
        end

        if @slots[:monthday] || @slots[:weekday]
          @hms << make_slot(:hm, 0, 0) if @hms.empty?
        elsif @slots[:month]
          @hms << make_slot(:hm, 0, 0) if @hms.empty?
          @slots[:monthday] ||= make_slot(:monthday, 1)
        end
      end

Public Instance Methods

to_crons(opts) click to toggle source
# File lib/fugit/nat.rb, line 647
def to_crons(opts)

  multi = opts.has_key?(:multi) ? opts[:multi] : false

  hms = determine_hms

  if multi == :fail && hms.count > 1
    fail(ArgumentError.new(
      "multiple crons in #{opts[:_s].inspect} - #{@slots.inspect}"))
  elsif multi == true
    hms.collect { |hm| parse_cron(hm, opts) }
  else
    parse_cron(hms.first, opts)
  end
end

Protected Instance Methods

determine_hms() click to toggle source
# File lib/fugit/nat.rb, line 670
def determine_hms

  return [ [ [ '*' ], [ '*' ] ] ] if @hms.empty?

  hms = @hms.dup
    #
  while ig = (hms.count > 1 && hms.index { |hm| hm.graded? }) do
    sg = hms[ig]
    so = hms.delete_at(ig == 0 ? 1 : ig - 1)
    sg.append(so)
  end

  hms
    .collect(&:a)
    .inject({}) { |r, hm|
      hm[1].each { |m| (r[m] ||= []).concat(hm[0]) }
      r }
    .inject({}) { |r, (m, hs)|
      (r[hs.sort] ||= []) << m
      r }
    .to_a
end
make_slot(key, data0, data1=nil) click to toggle source
# File lib/fugit/nat.rb, line 665
def make_slot(key, data0, data1=nil)

  Fugit::Nat::Slot.new(key, data0, data1)
end
parse_cron(hm, opts) click to toggle source
# File lib/fugit/nat.rb, line 693
def parse_cron(hm, opts)

  a = [
    slot(:second, '0'),
    hm[1],
    hm[0],
    slot(:monthday, '*'),
    slot(:month, '*'),
    slot(:weekday, '*') ]
  tz = @slots[:tz]
  a << tz.data0 if tz
  a.shift if a.first == [ '0' ]

  letters_last = lambda { |x| x.is_a?(Numeric) ? x : 999_999 }

  s = a
    .collect { |e|
      e.uniq.sort_by(&letters_last).collect(&:to_s).join(',') }
    .join(' ')

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

  if opts[:strict]
    restrict(a, c)
  else
    c
  end
end
restrict(a, cron) click to toggle source

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.

# File lib/fugit/nat.rb, line 732
def restrict(a, cron)

  if m = ((a[0] && a[0] != [ 0 ] && a[0][0]) || '').match(SLASH_REGEX)
    return nil unless (1..60).include?(m[1].to_i)
  end
  if m = ((a[1] && a[1][0]) || '').match(SLASH_REGEX)
    return nil unless [ 1, 2, 3, 4, 5, 6, 8, 12 ].include?(m[1].to_i)
  end

  cron
end
slot(key, default) click to toggle source
# File lib/fugit/nat.rb, line 744
def slot(key, default)

  s = @slots[key]
  s ? s.data0 : [ default ]
end