Class: Cronscription::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/cronscription/entry.rb

Constant Summary collapse

ORDERED_KEYS =
[:min, :hour, :day, :month, :wday]
FULL_RANGE =
{
  :min   => (0..59).to_a,
  :hour  => (0..23).to_a,
  :day   => (1..31).to_a,
  :month => (1..12).to_a,
  :wday  => (0..6).to_a,
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line) ⇒ Entry

Returns a new instance of Entry.



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/cronscription/entry.rb', line 14

def initialize(line)
  @line = line
  @times = {}

  raw = {}
  raw[:min], raw[:hour], raw[:day], raw[:month], raw[:wday], @command = line.split(nil, 6)
  @command.gsub!(/#.*/, '')

  raw.each do |key, val|
    @times[key] = parse_column(val, FULL_RANGE[key])
  end
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



12
13
14
# File 'lib/cronscription/entry.rb', line 12

def command
  @command
end

#timesObject (readonly)

Returns the value of attribute times.



12
13
14
# File 'lib/cronscription/entry.rb', line 12

def times
  @times
end

Class Method Details

.parsable?(str) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/cronscription/entry.rb', line 35

def self.parsable?(str)
  !!(str =~ /([*\d,-]+\s+){5}.*/)
end

Instance Method Details

#==(other) ⇒ Object



27
28
29
# File 'lib/cronscription/entry.rb', line 27

def ==(other)
  @line == other.instance_variable_get(:@line)
end

#match_command?(regex) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/cronscription/entry.rb', line 49

def match_command?(regex)
  regex === @command
end

#parse_column(column, default = []) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/cronscription/entry.rb', line 39

def parse_column(column, default=[])
  case column
    when /\*\/(\d+)/ then default.select { |val| val % $1.to_i == 0 }
    when /\*/        then default
    when /,/         then column.split(',').map{|c| parse_column(c)}.flatten.uniq
    when /-/         then Range.new(*column.split('-').map{|c| c.to_i}).to_a
    else                  [column.to_i]
  end
end

#times_to_execute(start, finish) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/cronscription/entry.rb', line 53

def times_to_execute(start, finish)
  ret = []

  incr_min  = 60
  incr_hour = incr_min*60
  incr_day  = incr_hour*24
  incr      = incr_min

  current = nearest_minute(start)
  while current <= finish
    if ORDERED_KEYS.map{|k| @times[k].include?(current.send k)}.all?
      ret << current
      # If only I could goto into the middle of the loop, this wouldn't run every time.
      # Optimizations to reduce execution time.  No need to run minutely if there is only one minute.
      if @times[:min].size == 1
        if @times[:hour].size == 1
          incr = incr_day
        else
          incr = incr_hour
        end
      end
    end
    current += incr
  end

  ret
end

#to_sObject



31
32
33
# File 'lib/cronscription/entry.rb', line 31

def to_s
  @line
end