Class: CrontabParser::TimeParser

Inherits:
Object
  • Object
show all
Defined in:
lib/crontab-parser/time_parser.rb

Constant Summary collapse

CACHE =
{}

Class Method Summary collapse

Class Method Details

.parse(column, first = 0, last = 59) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/crontab-parser/time_parser.rb', line 7

def self.parse(column, first=0, last=59)
  base = column + "_#{first}-#{last}"
  if CACHE[base]
    return CACHE[base]
  end
  column = column.gsub(%r!/1$!,"").gsub('*', "#{first}-#{last}")
  if column.index('/')
    times,filter = *column.split("/")
  else
    times = column
    filter = nil
  end

  result = if times.index(',')
    times.split(',').map{|col| separetor(col)}
  else
    separetor(times)
  end.flatten

  if filter
    result = result.find_all{|n| n % filter.to_i == 0}
  end

  CACHE[base] = result
end

.separetor(col) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/crontab-parser/time_parser.rb', line 33

def self.separetor(col)
  if col.index('-')
    m,n = *col.split("-").map{|n| n.to_i}
    (m..n).to_a
  else
    [col.to_i]
  end
end