Class: Crony::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/crony/parser.rb

Class Method Summary collapse

Class Method Details

.parse(element) ⇒ Object



3
4
5
6
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
32
33
# File 'lib/crony/parser.rb', line 3

def self.parse(element)
  @element = element
  case @element
  when '*' then { :every => true }
  when /^l$/i then { :last => true }
  when /(\d|\w{3})l$/i
    @element = @element.sub(/l$/i, '')
    { :last => true, :collection => parse_collection }
  when /.*#\d$/
    @element, nth_week = @element.scan(/(.*)#(\d)$/)[0]
    { :nth_week => nth_week, :collection => parse_collection }
  when /\d+w$/i
    @element = @element.sub(/w$/i, '')
    { :nearest => true, :collection => parse_collection }
  when /\//
    start, frequency = @element.scan(/(.*)\/(.*)/)[0]
    case start
    when '*'
      { :frequency => frequency }
    when /^\d+$/ #single number
      { :frequency => frequency, :start => start }
    when /^\w+-\w+$/
      range_start, range_end = *start.scan(/(\w+)-(\w+)/)[0]
      { :frequency => frequency, :start => range_start, :stop => range_end }
    end
  when /^\w+-\w+$/
    range_start, range_end = *@element.scan(/(\w+)-(\w+)/)[0]
    { :start => range_start, :stop => range_end }
  else { :collection => parse_collection }
  end
end

.parse_collectionObject



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/crony/parser.rb', line 35

def self.parse_collection
  parts = @element.split(',')
  res = []
  parts.each do |part|
    if part =~ /\-/
      range = part.scan(/(\d+)-(\d+)/)[0]
      res += Array(range[0].to_i..range[1].to_i)
    else
      res << part
    end
  end
  res
end