Class: CrontabRb::Cron

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Cron



10
11
12
13
14
15
16
17
# File 'lib/crontab_rb/cron.rb', line 10

def initialize(attributes={})
  @id          = attributes[:id]
  @name        = attributes[:name]
  @command     = attributes[:command]
  @time        = attributes[:time]
  @at          = attributes[:at]
  @updated_at  = attributes[:updated_at]
end

Instance Attribute Details

#atObject

Returns the value of attribute at.



7
8
9
# File 'lib/crontab_rb/cron.rb', line 7

def at
  @at
end

#commandObject

Returns the value of attribute command.



5
6
7
# File 'lib/crontab_rb/cron.rb', line 5

def command
  @command
end

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/crontab_rb/cron.rb', line 3

def id
  @id
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/crontab_rb/cron.rb', line 4

def name
  @name
end

#timeObject

Returns the value of attribute time.



6
7
8
# File 'lib/crontab_rb/cron.rb', line 6

def time
  @time
end

#updated_atObject

Returns the value of attribute updated_at.



8
9
10
# File 'lib/crontab_rb/cron.rb', line 8

def updated_at
  @updated_at
end

Class Method Details

.allObject



30
31
32
33
34
35
# File 'lib/crontab_rb/cron.rb', line 30

def self.all
  records = Database.all
  records.map {|record| new(record)}
rescue
  []
end

.convert_to_symbol(hash) ⇒ Object



69
70
71
# File 'lib/crontab_rb/cron.rb', line 69

def self.convert_to_symbol(hash)
  Hash[hash.map{|k, v| [k.to_sym, v]}]
end

.create(options = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/crontab_rb/cron.rb', line 19

def self.create(options={})
  options = convert_to_symbol(options)
  cron = new(options)
  cron.validate
  record          = Database.create(options)
  cron.id         = record[:id]
  cron.updated_at = record[:updated_at]
  cron.write_crontab
  cron
end

.destroy(id) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/crontab_rb/cron.rb', line 56

def self.destroy(id)
  record = Database.find(id)
  return nil if record.nil?
  cron = new(record)
  Database.delete(id)
  cron.write_crontab
  cron
end

.find(id) ⇒ Object



37
38
39
40
# File 'lib/crontab_rb/cron.rb', line 37

def self.find(id)
  record = Database.find(id)
  record.nil? ? nil : new(record)
end

.update(id, options = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/crontab_rb/cron.rb', line 42

def self.update(id, options={})
  record = Database.find(id)
  return nil if record.nil?
  record[:name]    = options[:name]    || record[:name]
  record[:command] = options[:command] || record[:command]
  record[:time]    = options[:time]    || record[:time]
  record[:at]      = options[:at]      || record[:at]
  cron = new(record)
  cron.validate
  Database.new(record).save
  cron.write_crontab
  cron
end

Instance Method Details

#validateObject



65
66
67
# File 'lib/crontab_rb/cron.rb', line 65

def validate
  raise "Time attribute of crontab_rb only accept #{CrontabRb::Template::EVERY.keys.join(",")}" unless CrontabRb::Template::EVERY.keys.include?(time)
end

#write_crontabObject



73
74
75
# File 'lib/crontab_rb/cron.rb', line 73

def write_crontab
  Write.write_crontab
end