module Asyncron::Cron

Constants

HOUR
MINUTE
MONTH
MONTHDAY
POSITION
WEEKDAY
YEAR

Public Instance Methods

parse(expr) click to toggle source
# File lib/asyncron/cron.rb, line 16
def parse(expr)
  expr.split(/[\s\t]+/).map.with_index { |e, i| transform(i, e) }
end
transform(pos, value) click to toggle source
# File lib/asyncron/cron.rb, line 24
def transform(pos, value)
  unless validate(value)
    raise ArgumentError.new("invalid format for #{POSITION[pos]}")
  end
  return divide(value) do |no_divide|
    range = single_num(no_divide) || extend_asterisk(pos, value)
    next range if range
    next filter(expand(pos), value)
  end
end
validate(value) click to toggle source
# File lib/asyncron/cron.rb, line 20
def validate(value)
  value =~ /^((\d+(-\d+)?(,\d+(-\d+)?)*)(\/\d+)?|\*(\/\d+)?)$/
end

Private Instance Methods

divide(value) { |arg| ... } click to toggle source
# File lib/asyncron/cron.rb, line 68
def divide(value)
  arg, _, divider = value.match(/^([^\/]+)(\/(\d+))?$/).captures
  range = yield(arg)
  return range if divider !~ /^\d+$/
  divider = divider.to_i
  return range.each.with_index.reduce([]) do |acc, (r, i)|
    acc << r if i % divider == 0
    next acc
  end
end
expand(pos) click to toggle source
# File lib/asyncron/cron.rb, line 37
def expand(pos)
  const_get(POSITION[pos].upcase)
end
extend_asterisk(pos, value) click to toggle source
# File lib/asyncron/cron.rb, line 46
def extend_asterisk(pos, value)
  return if value != "*"
  return expand(pos)
end
filter(range, value) click to toggle source
# File lib/asyncron/cron.rb, line 51
def filter(range, value)
  values = value.split(",")
  last_value = values.pop
  values.push(last_value.match(/^([^\/]+)/).captures.first)
  return range unless values.all? { |v| v =~ /^\d+(-\d+)?$/ }
  values = values.reduce([]) do |acc, value|
    min, _, max = value.match(/^(\d+)(-(\d+))?$/).captures
    if max.nil?
      acc << min.to_i
    else
      acc += (min.to_i..max.to_i).to_a
    end
    next acc
  end
  return range & values
end
single_num(value) click to toggle source
# File lib/asyncron/cron.rb, line 41
def single_num(value)
  return if value !~ /^\d+$/
  return [value.to_i]
end