forked from NUBIC/surveyor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
question.rb
59 lines (49 loc) · 1.84 KB
/
question.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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, :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
if self.dependency then self.dependency.to_file end
end
end
end