From e12d3436d6c3e1592f7503c6c712af125c4c1031 Mon Sep 17 00:00:00 2001 From: Javan Makhmali Date: Thu, 1 Jul 2010 17:04:11 -0400 Subject: [PATCH] automatically escape :task in single or double quotes --- lib/whenever/job.rb | 9 ++++++++- test/output_defined_job_test.rb | 31 +++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/lib/whenever/job.rb b/lib/whenever/job.rb index 636bc49..e49eccc 100644 --- a/lib/whenever/job.rb +++ b/lib/whenever/job.rb @@ -13,7 +13,14 @@ module Whenever end def output - @options[:template].gsub(/:\w+/) do |key| + template = @options[:template].dup + + unless @options[:escape_quotes] === false + template.sub!("':task'", %Q('#{@options[:task].gsub(/'/) { "'\''" }}')) + template.sub!('":task"', %Q("#{@options[:task].gsub(/"/) { '\"' }}")) + end + + template.gsub(/:\w+/) do |key| @options[key.sub(':', '').to_sym] end end diff --git a/test/output_defined_job_test.rb b/test/output_defined_job_test.rb index 308e0aa..ddaa220 100644 --- a/test/output_defined_job_test.rb +++ b/test/output_defined_job_test.rb @@ -84,4 +84,35 @@ class OutputDefinedJobTest < Test::Unit::TestCase end end + context "A defined job with a task in single quotes" do + setup do + @output = Whenever.cron \ + <<-file + job_type :some_job, "happy ':task'" + every 2.hours do + some_job "first 'birthday'" + end + file + end + + should "output the defined job with the single quotes escaped" do + assert_match two_hours + %Q( happy 'first '\''birthday'\''), @output + end + end + + context "A defined job with a task in double quotes" do + setup do + @output = Whenever.cron \ + <<-file + job_type :some_job, 'happy ":task"' + every 2.hours do + some_job 'first "birthday"' + end + file + end + + should "output the defined job with the single quotes escaped" do + assert_match two_hours + ' happy "first \"birthday\""', @output + end + end end \ No newline at end of file