Class: AutoCron

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

Instance Method Summary collapse

Constructor Details

#initialize(template, application) ⇒ AutoCron

Returns a new instance of AutoCron.



2
3
4
5
6
7
8
# File 'lib/auto_cron.rb', line 2

def initialize( template, application )
  require 'erb'

  @auto_cron_dir = File.join( Rails.root, "config", "auto_cron" )
  @template = template
  @application = application
end

Instance Method Details

#updated_crontabObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/auto_cron.rb', line 10

def updated_crontab
  # Check for unopened or unclosed identifier blocks
  if read_crontab.index(comment_open) && !read_crontab.index(comment_close)
    warn "[fail] Unclosed indentifier; Your crontab file contains '#{comment_open}', but no '#{comment_close}'"
    exit(1)
  elsif !read_crontab.index(comment_open) && read_crontab.index(comment_close)
    warn "[fail] Unopened indentifier; Your crontab file contains '#{comment_close}', but no '#{comment_open}'"
    exit(1)
  end
  
  # If an existing identier block is found, replace it with the new cron entries
  if read_crontab.index(comment_open) && read_crontab.index(comment_close)
    read_crontab.gsub(Regexp.new("#{comment_open}.+#{comment_close}", Regexp::MULTILINE), auto_cron_wrapped.chomp)
  else # Otherwise, append the new cron entries after any existing ones
    [read_crontab, auto_cron_wrapped].join("\n\n")
  end
end