Module: SurveyorParserAnswerMethods

Defined in:
lib/surveyor/parser.rb

Overview

Answer model

Instance Method Summary collapse

Instance Method Details

#hash_from(arg) ⇒ Object



382
383
384
# File 'lib/surveyor/parser.rb', line 382

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

#parse_and_build(context, args, original_method, reference_identifier) ⇒ Object



345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/surveyor/parser.rb', line 345

def parse_and_build(context, args, original_method, reference_identifier)
  # clear context
  [ :answer,
    :validation,
    :validation_condition ].each{|k| context.delete k}
  attrs = { :reference_identifier => reference_identifier }.merge(parse_args(args))

  # add answers to grid
  if context[:question_group] && context[:question_group].display_type == "grid"
    self.attributes = PermittedParams.new({:display_order => context[:grid_answers].size}.merge(attrs)).answer
    context[:grid_answers] << context[:answer] = self
  else
    self.attributes = PermittedParams.new({:display_order => context[:question].answers.size}.merge(attrs)).answer
    context[:question].answers << context[:answer] = self
    # keep reference for dependencies
    unless context[:question].reference_identifier.blank? or self.reference_identifier.blank?
      context[:answer_references][context[:question].reference_identifier] ||= {}
      context[:duplicate_references].push "q_#{context[:question].reference_identifier}, a_#{self.reference_identifier}" if context[:answer_references][context[:question].reference_identifier].has_key?(self.reference_identifier)
      context[:answer_references][context[:question].reference_identifier][self.reference_identifier] = context[:answer]
    end
  end
end

#parse_args(args) ⇒ Object



367
368
369
370
371
372
373
374
375
376
377
378
# File 'lib/surveyor/parser.rb', line 367

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

#symbol_args(arg) ⇒ Object



385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/surveyor/parser.rb', line 385

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

#text_args(text = "Answer") ⇒ Object



379
380
381
# File 'lib/surveyor/parser.rb', line 379

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