Class: Answer

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

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Surveyor::Models::AnswerMethods

#css_class, #default_args, included, #initialize, #split_or_hidden_text

Class Method Details

+ (Object) build_and_set(context, r)



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/surveyor/redcap_parser.rb', line 175

def self.build_and_set(context, r)
  case r[:field_type]
  when "text"
    context[:answer] = context[:question].answers.build(:response_class => "string", :text => "Text")
  when "notes"
    context[:answer] = context[:question].answers.build(:response_class => "text", :text => "Notes")
  when "file"
    puts "\n!!! skipping answer: file"
  end
  r[:choices_or_calculations].to_s.split("|").each do |pair|
    aref, atext = pair.strip.split(", ")
    if aref.blank? or atext.blank?
      puts "\n!!! skipping answer #{pair}"
    else
      context[:answer] = context[:question].answers.build(:reference_identifier => aref, :text => atext)
      unless context[:question].reference_identifier.blank? or aref.blank? or !context[:answer].valid?
        context[:lookup] ||= []
        context[:lookup] << [context[:question].reference_identifier, aref, context[:answer]]
      end
      puts "#{context[:answer].errors.full_messages}, #{context[:answer].inspect}" unless context[:answer].valid?
      print "answer_#{context[:answer].reference_identifier} "
    end
  end
end

+ (Object) hash_from(arg)



249
250
251
# File 'lib/surveyor/parser.rb', line 249

def self.hash_from(arg)
  arg.is_a?(Symbol) ? {:response_class => arg.to_s} : arg.is_a?(Hash) ? arg : {}
end

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

nonblock



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/surveyor/parser.rb', line 217

def self.parse_and_build(context, args, original_method, reference_identifier)
  # clear context
  context.delete_if{|k,v| %w(answer validation validation_condition reference_identifier).map(&:to_sym).include? k}

  attrs = { :reference_identifier => reference_identifier}.merge(self.parse_args(args))
                            
  # add answers to grid
  if context[:question_group] && context[:question_group].display_type == "grid"
    context[:answer] = new(attrs)
    context[:grid_answers] ||= []
    context[:grid_answers] << context[:answer]
  else
    context[:answer] = context[:question].answers.build(attrs)
    context[:answer_references][context[:question].reference_identifier] ||= {} unless context[:question].reference_identifier.blank?
    context[:answer_references][context[:question].reference_identifier][reference_identifier] = context[:answer] unless reference_identifier.blank? or context[:question].reference_identifier.blank?
  end
end

+ (Object) parse_args(args)



234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/surveyor/parser.rb', line 234

def self.parse_args(args)
  case args[0]
  when Hash # Hash
    self.text_args(args[0][:text]).merge(args[0])
  when String # (String, Hash) or (String, Symbol, Hash)
    self.text_args(args[0]).merge(self.hash_from args[1]).merge(args[2] || {})
  when Symbol # (Symbol, Hash) or (Symbol, Symbol, Hash)
    self.symbol_args(args[0]).merge(self.hash_from args[1]).merge(args[2] || {})
  else
    self.text_args(nil)
  end
end

+ (Object) symbol_args(arg)



252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/surveyor/parser.rb', line 252

def self.symbol_args(arg)
  case arg
  when :other
    self.text_args("Other")
  when :other_and_string
    self.text_args("Other").merge({:response_class => "string"})
  when :none, :omit # is_exclusive erases and disables other checkboxes and input elements
    self.text_args(arg.to_s.humanize).merge({:is_exclusive => true})
  when :integer, :date, :time, :datetime, :text, :datetime, :string
    self.text_args(arg.to_s.humanize).merge({:response_class => arg.to_s, :hide_label => true})
  end
end

+ (Object) text_args(text = "Answer")



246
247
248
# File 'lib/surveyor/parser.rb', line 246

def self.text_args(text = "Answer")
  {:text => text.to_s}
end

Instance Method Details

- (Object) unparse(dsl)

nonblock



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/surveyor/unparser.rb', line 107

def unparse(dsl)
  attrs = (self.attributes.diff Answer.new(:text => text).attributes).delete_if{|k,v| %w(created_at updated_at reference_identifier response_class id question_id api_id).include? k}.symbolize_keys!
  attrs.delete(:is_exclusive) if text == "Omit" && is_exclusive == true
  attrs.merge!({:is_exclusive => false}) if text == "Omit" && is_exclusive == false
  dsl << "  " if question.part_of_group?
  dsl << "    a"
  dsl << "_#{reference_identifier}" unless reference_identifier.blank?
  if response_class.to_s.titlecase == text && attrs == {:hide_label => true}
    dsl << " :#{response_class}"
  else    
    dsl << [ text.blank? ? nil : text == "Other" ? " :other" : text == "Omit" ? " :omit" : " \"#{text}\"",
              (response_class.blank? or response_class == "answer") ? nil : " #{response_class.to_sym.inspect}",
              attrs.blank? ? nil : " #{attrs.inspect.gsub(/\{|\}/, "")}\n"].compact.join(",")
  end
  dsl << "\n"
  validations.each{|validation| validation.unparse(dsl)}
end