Skip to content

Instantly share code, notes, and snippets.

@rsutphin
Created October 22, 2012 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rsutphin/3932129 to your computer and use it in GitHub Desktop.
Save rsutphin/3932129 to your computer and use it in GitHub Desktop.
Rake task for mustache syntax checking an application's surveyor surveys
task :mustache_check => :environment do
require 'mustache/parser'
mustache_error = Struct.new(:record_type, :reference_identifier, :error_message)
text_error = lambda do |text|
begin
Mustache::Parser.new.compile(text)
nil
rescue Mustache::Parser::SyntaxError => e
e.to_s
end
end
produce_error = lambda do |text_having_object|
error = text_error[text_having_object.text] if text_having_object.text
if error
mustache_error.new(text_having_object.class, text_having_object.reference_identifier, error)
end
end
surveys = Survey.includes(:sections => { :questions => [ :answers ] })
latest_versions = surveys.inject({}) { |h, s|
h[s.title] = [h[s.title], s.survey_version].compact.max; h
}
surveys.select { |survey| survey.survey_version == latest_versions[survey.title] }.each do |survey|
if Rake.application.options.trace
$stderr.puts "Analyzing #{survey.title} / testbed survey_version #{survey.survey_version} / testbed id #{survey.id}"
end
questions = survey.sections.collect { |sect| sect.questions }.flatten
errors = questions.collect { |q|
([q] + q.answers).collect { |i| produce_error[i] }
}.flatten.compact
unless errors.empty?
$stderr.puts "#{survey.title} / testbed survey_version #{survey.survey_version} / testbed id #{survey.id}" unless Rake.application.options.trace
errors.each do |error|
$stderr.puts "* Error on #{error.record_type.to_s.downcase} #{error.reference_identifier}"
$stderr.puts error.error_message.split("\n").map { |line| " #{line}" }.join("\n")
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment