Class: Colonel::Builder

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

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Builder

Returns a new instance of Builder.



6
7
8
9
# File 'lib/colonel/builder.rb', line 6

def initialize(opts={})
  Job.clear_amount
  @all_flag = opts[:all_flag].nil? ? true : opts[:all_flag]
end

Instance Method Details

#add_job(opts = {}) ⇒ Object



46
47
48
# File 'lib/colonel/builder.rb', line 46

def add_job(opts={})
  @jobs << Job.new( Parser::Schedule.new(opts[:schedule]), Parser::Command.new(opts[:command]))
end

#crontabObject



15
16
17
# File 'lib/colonel/builder.rb', line 15

def crontab
  @_crontab ||= Crontab.new(user)
end

#destroy_job(id) ⇒ Object



50
51
52
53
# File 'lib/colonel/builder.rb', line 50

def destroy_job(id)
  index = get_job_index(id)
  @jobs.delete_at index
end

#find_job(id) ⇒ Object



31
32
33
# File 'lib/colonel/builder.rb', line 31

def find_job(id)
  @jobs.select { |j| j.id == id.to_i }.first
end

#get_job_index(id) ⇒ Object



35
36
37
# File 'lib/colonel/builder.rb', line 35

def get_job_index(id)
  @jobs.each_with_index { |j,i| return i if j.id == id.to_i }
end

#parseObject



19
20
21
22
23
24
25
# File 'lib/colonel/builder.rb', line 19

def parse
  @jobs ||= []
  crontab.reject_useless.each do |line|
    @jobs << Parser.new(line, :all_flag => @all_flag).execute
  end
  @jobs
end

#update_crontabObject



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

def update_crontab
  crontab.update(@jobs)
end

#update_job(opts = {}) ⇒ Object

TODO: Refactor it! To complicated!



40
41
42
43
44
# File 'lib/colonel/builder.rb', line 40

def update_job(opts={})
  index = get_job_index(opts[:id])
  job = find_job(opts[:id])
  @jobs[index] = job.update(opts)
end

#userObject



11
12
13
# File 'lib/colonel/builder.rb', line 11

def user
  @_user ||= `whoami`.chomp
end