class Whenever::Job

Attributes

at[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/whenever/job.rb, line 7
def initialize(options = {})
  @options = options
  @at                      = options.delete(:at)
  @template                = options.delete(:template)
  @job_template            = options.delete(:job_template) || ":job"
  @options[:output]        = Whenever::Output::Redirection.new(options[:output]).to_s if options.has_key?(:output)
  @options[:environment] ||= :production
  @options[:path]          = Shellwords.shellescape(@options[:path] || Whenever.path)
end

Public Instance Methods

output() click to toggle source
# File lib/whenever/job.rb, line 17
def output
  job = process_template(@template, @options).strip
  out = process_template(@job_template, { :job => job }).strip
  if out =~ %r\n/
    raise ArgumentError, "Task contains newline"
  end
  out.gsub(%r%/, '\%')
end

Protected Instance Methods

escape_double_quotes(str) click to toggle source
# File lib/whenever/job.rb, line 47
def escape_double_quotes(str)
  str.gsub(%r"/) { '\"' }
end
escape_single_quotes(str) click to toggle source
# File lib/whenever/job.rb, line 43
def escape_single_quotes(str)
  str.gsub(%r'/) { "'\\''" }
end
process_template(template, options) click to toggle source
# File lib/whenever/job.rb, line 28
def process_template(template, options)
  template.gsub(%r:\w+/) do |key|
    before_and_after = [$`[-1..-1], $'[0..0]]
    option = options[key.sub(':', '').to_sym]

    if before_and_after.all? { |c| c == "'" }
      escape_single_quotes(option)
    elsif before_and_after.all? { |c| c == '"' }
      escape_double_quotes(option)
    else
      option
    end
  end
end