Module: SurveyorParserAnswerMethods

Defined in:
lib/surveyor/parser.rb

Overview

Answer model

Instance Method Summary collapse

Instance Method Details

#hash_from(arg) ⇒ Object



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

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



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

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 = ({:display_order => context[:grid_answers].size}.merge(attrs))
    context[:grid_answers] << context[:answer] = self
  else
    self.attributes = ({:display_order => context[:question].answers.size}.merge(attrs))
    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



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

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



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

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



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

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