Skip to content

Commit

Permalink
added "correct_answer" to parser, so you can specify one correct answ…
Browse files Browse the repository at this point in the history
…er per question
  • Loading branch information
lancejpollard authored and brian-lc committed Apr 27, 2010
1 parent 8a37a0e commit 1cf1313
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 7 deletions.
11 changes: 9 additions & 2 deletions script/surveyor/base.rb
Expand Up @@ -48,18 +48,25 @@ def parse_args(args)
def yml_attrs
instance_variables.sort - self.class.children.map{|model| "@#{model.to_s}"} - %w(@id @parser @dependency @validation @question_reference @answer_reference)
end

def to_yml
out = [ %(#{self.parser.salt}_#{self.class.name.demodulize.underscore}_#{@id}:) ]
yml_attrs.each{|a| out << associate_and_format(a)}
(out << nil ).join("\r\n")
end

def associate_and_format(a)
if a =~ /_id$/ # a foreign key, e.g. survey_id
" #{a[1..-4]}: " + (instance_variable_get(a).nil? ? "" : "#{self.parser.salt}_#{a[1..-4]}_#{instance_variable_get(a)}")
" #{property_name_map(a[1..-4])}: " + (instance_variable_get(a).nil? ? "" : "#{self.parser.salt}_#{a[1..-4]}_#{instance_variable_get(a)}")
else # quote strings
" #{a[1..-1]}: #{instance_variable_get(a).is_a?(String) ? "\"#{instance_variable_get(a)}\"" : instance_variable_get(a) }"
" #{property_name_map(a[1..-1])}: #{instance_variable_get(a).is_a?(String) ? "\"#{instance_variable_get(a)}\"" : instance_variable_get(a) }"
end
end

def property_name_map(property)
return property
end

def to_file
File.open(self.parser.send("#{self.class.name.demodulize.underscore.pluralize}_yml"), File::CREAT|File::APPEND|File::WRONLY) {|f| f << to_yml}
self.class.children.each{|model| self.send(model).compact.map(&:to_file)}
Expand Down
20 changes: 18 additions & 2 deletions script/surveyor/parser.rb
Expand Up @@ -92,6 +92,11 @@ def method_missing(missing_method, *args, &block)
raise "Error: No current question" if self.current_question.nil?
self.current_answer = Answer.new(self.current_question, args, opts.merge(:display_order => current_question.answers.size + 1))
end

when "correct"
drop_the &block
raise "Error: No current question" if self.current_question.nil?
self.current_correct_answer = self.current_question.find_current_answers(args)

when "validation", "v"
drop_the &block
Expand Down Expand Up @@ -135,31 +140,42 @@ def current_survey=(s)
self.surveys << s
@current_survey = s
end

def current_survey_section=(s)
clear_current "survey_section"
self.current_survey.survey_sections << s
@current_survey_section = s
end

def current_question_group=(g)
clear_current "question_group"
self.current_survey_section.question_groups << g
@current_question_group = g
end
end

def current_question=(q)
clear_current "question"
self.current_survey_section.questions << q
@current_question = q
end

def current_dependency=(d)
raise "Error: No question or question group" unless (dependent = self.current_question_group || self.current_question)
dependent.dependency = d
@current_dependency = d
end

def current_answer=(a)
raise "Error: No current question" if self.current_question.nil?
self.current_question.answers << a
@current_answer = a
end
end

def current_correct_answer=(a)
raise "Error: No current question" if self.current_question.nil?
self.current_question.correct_answer = a
end

def current_validation=(v)
clear_current "validation"
self.current_answer.validation = v
Expand Down
29 changes: 26 additions & 3 deletions script/surveyor/question.rb
@@ -1,31 +1,54 @@
module SurveyParser
class Question < SurveyParser::Base
ANSWER_ID = /\s*\w+_/ unless defined?(ANSWER_ID)

# Context, Content, Reference, Display, Children
attr_accessor :id, :parser, :survey_section_id, :question_group_id
attr_accessor :text, :short_text, :help_text, :pick
attr_accessor :id, :parser, :survey_section_id, :question_group_id, :correct_answer_id
attr_accessor :text, :short_text, :help_text, :pick, :answer_id
attr_accessor :reference_identifier, :data_export_identifier, :common_namespace, :common_identifier
attr_accessor :display_order, :display_type, :is_mandatory, :display_width, :custom_class, :custom_renderer
attr_accessor :dependency
has_children :answers

def default_options
{ :pick => :none,
:display_type => :default,
:is_mandatory => true,
:display_order => self.id
}
end

def parse_opts(opts)
(name = opts.delete(:method_name)) =~ /label|image/ ? opts.merge(:display_type => name) : opts
end

def parse_args(args)
text = args[0] || "Question"
{:text => text, :short_text => text, :data_export_identifier => Surveyor.to_normalized_string(text)}.merge(args[1] || {})
end

def correct_answer=(a)
self.answer_id = a.id
end

def find_answer_by_reference(ref_id)
self.answers.detect{|a| a.reference_identifier == ref_id}
end

# currently, only one correct answer is allowed
def find_current_answers(args)
ref_ids = args[0][:answer]
ids = ref_ids.to_s.split(ANSWER_ID).compact
self.answers.select{|a| ids.include?(a.reference_identifier)}.first
end

# so we can build the correct yaml structure
def property_name_map(property)
names = {
"answer" => "correct_answer"
}
return names.has_key?(property) ? names[property] : property
end

def to_file
super
Expand Down

0 comments on commit 1cf1313

Please sign in to comment.