Class: Khronotab::CronTab

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

Constant Summary collapse

VERSION =
'1.3.2'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opt = {}) ⇒ CronTab

Returns a new instance of CronTab.



14
15
16
17
18
19
# File 'lib/khronotab.rb', line 14

def initialize(opt={})
  @variables ||= []
  @jobs ||= []
  @comments ||= []
  self.read_from_file(opt[:file]) if opt[:file] 
end

Instance Attribute Details

#commentsObject

Returns the value of attribute comments.



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

def comments
  @comments
end

#jobsObject

Returns the value of attribute jobs.



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

def jobs
  @jobs
end

#variablesObject

Returns the value of attribute variables.



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

def variables
  @variables
end

Class Method Details

.read_from_file(*args) ⇒ Object



21
22
23
# File 'lib/khronotab.rb', line 21

def self.read_from_file(*args)
  self.new.read_from_file(*args)
end

Instance Method Details

#read_from_file(filename) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/khronotab.rb', line 25

def read_from_file(filename)
  File.open(filename, 'r').readlines.each do |line|
    if CronVariable.matches?(line)
      @variables << CronVariable.add_new(line)
    elsif CronJob.matches?(line)
      @jobs << CronJob.add_new(line)
    elsif CronComment.matches?(line)
      @comments << line
    else
      STDERR.puts("Warning: Line is not valid!(#{line.chomp})")
    end
  end
self
end

#write_to_file(filename) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/khronotab.rb', line 40

def write_to_file(filename)
  #TODO: Have this validate a file or append to the file if there
  # is new data. Just overwriting the damn thing isn't very
  # safe or wise.. -kmwhite 2010.04.19
  crontab = File.open(filename,'w')
  @variables.each do |variable|
    crontab.puts definition.to_line
  end
  @jobs.each do |job|
    crontab.puts job.to_line
  end
  crontab.flush
  crontab.close
end