Class: SurveySection

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Surveyor::Models::SurveySectionMethods
Defined in:
lib/surveyor/parser.rb,
lib/surveyor/unparser.rb,
app/models/survey_section.rb,
lib/surveyor/redcap_parser.rb

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Surveyor::Models::SurveySectionMethods

#default_args, included, #initialize

Class Method Details

+ (Object) build_or_set(context, r)



59
60
61
62
63
64
65
66
67
68
# File 'lib/surveyor/redcap_parser.rb', line 59

def self.build_or_set(context, r)
  unless context[:survey_section] && context[:survey_section].reference_identifier == r[:form_name]
    if match = context[:survey].sections.detect{|ss| ss.reference_identifier == r[:form_name]}
      context[:current_survey_section] = match
    else
      context[:survey_section] = context[:survey].sections.build({:title => r[:form_name].to_s.humanize, :reference_identifier => r[:form_name]})
      print "survey_section_#{context[:survey_section].reference_identifier} "
    end
  end
end

+ (Object) parse_and_build(context, args, original_method, reference_identifier)

block



94
95
96
97
98
99
100
101
# File 'lib/surveyor/parser.rb', line 94

def self.parse_and_build(context, args, original_method, reference_identifier)
  # clear context
  context.delete_if{|k,v| !%w(survey question_references answer_references).map(&:to_sym).include?(k)}
  
  # build and set context
  title = args[0]
  context[:survey_section] = context[:survey].sections.build({ :title => title }.merge(args[1] || {}))
end

Instance Method Details

- (Object) clear(context)



102
103
104
# File 'lib/surveyor/parser.rb', line 102

def clear(context)
  context.delete_if{|k,v| !%w(survey question_references answer_references).map(&:to_sym).include?(k)}
end

- (Object) unparse(dsl)

block



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/surveyor/unparser.rb', line 27

def unparse(dsl)
  attrs = (self.attributes.diff SurveySection.new(:title => title).attributes).delete_if{|k,v| %w(created_at updated_at id survey_id).include? k}.symbolize_keys!
  group_questions = []
  dsl << "  section \"#{title}\""
  dsl << (attrs.blank? ? " do\n" : ", #{attrs.inspect.gsub(/\{|\}/, "")} do\n")
  questions.each_with_index do |question, index|
    if question.solo?
      question.unparse(dsl)
    else # gather up the group questions
      group_questions << question
      if (index + 1 >= questions.size) or (question.question_group != questions[index + 1].question_group)
        # this is the last question of the section, or the group
        question.question_group.unparse(dsl)
      end
      group_questions = []
    end
  end
  dsl << "  end\n"
end