Class: Raketab

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

Constant Summary collapse

Month =
enum %w[January February March April May June July August September October November December], 1
Weekday =
enum %w[Sunday Monday Tuesday Wednesday Thursday Friday Saturday]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRaketab

Returns a new instance of Raketab.



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

def initialize
  @tabs = []
end

Class Method Details

.methodize_enum(enum) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/raketab.rb', line 9

def methodize_enum(enum)
  enum.each do |e| 
    p = Proc.new { e }
    define_method(e.to_s.downcase,    p) 
    define_method(e.to_abbr.downcase, p) 
  end
end

.schedule(&block) ⇒ Object



18
19
20
21
22
# File 'lib/raketab.rb', line 18

def schedule(&block)
  tab = Raketab.new
  tab.instance_eval(&block)
  tab
end

Instance Method Details

#run(command, options = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
80
# File 'lib/raketab.rb', line 31

def run(command, options={})
  month, wday, mday, hour, min = options[:month]   || options[:months]   || options[:mon], 
                                 options[:weekday] || options[:weekdays] || options[:wday], 
                                 options[:day]     || options[:days]     || options[:mday],
                                 options[:hour]    || options[:hours],
                                 options[:minute]  || options[:minutes]  || options[:min]

  # make sure we have ints instead of enums, yo
  month, wday = [[month, Month], [wday, Weekday]].map do |element,type| 
    if element.kind_of?(Array) # just arrays for now
     element.each_with_index { |e,i| element[i] = enum_to_i(e,type) } 
    else
     enum_to_i(element,type)
    end
  end

  [:each, :every, :on, :in, :at, :the].each do |type|
    if options[type]
      if(options[type] =~ /:/)
        from = options[type]
      else
        from, ignore, exclusive, to = options[type].to_s.match(/(\w+)(\.\.(\.?)(\w+))?/)[1..4].map { |m| m.gsub(/s$/i, '') if m } 
      end

      parse = Date._parse(from)
      range = to ? Date._parse(to) : {}

      month ||= get_value(parse, range, exclusive == '.', :mon)
      wday  ||= get_value(parse, range, exclusive == '.', :wday)
      mday  ||= get_value(parse, range, exclusive == '.', :mday)
      hour  ||= get_value(parse, range, exclusive == '.', :hour)
      min   ||= get_value(parse, range, exclusive == '.', :min)
    end
  end

  # deal with any arrays and ranges
  min, hour, mday, wday, month = [min, hour, mday, wday, month].map do |type| 
   type.respond_to?(:map) ? type.map.join(',') : type    
  end

  # special cases with hours
  hour ||= options[:at].to_i if options[:at] # :at => "5 o'clock" / "5" / 5

  # fill missing items
  hour, min         = [hour, min].map { |t| t || '0' }
  month, wday, mday = [month, wday, mday].map { |t| t || '*' }    
  
  # put it together
  @tabs << "#{min} #{hour} #{mday} #{month} #{wday} #{command}"
end

#tabsObject



82
83
84
# File 'lib/raketab.rb', line 82

def tabs
  @tabs.join("\n")
end