Class: Helena::Answer

Inherits:
Object
  • Object
show all
Includes:
Concerns::ApplicationModel
Defined in:
app/models/helena/answer.rb

Direct Known Subclasses

BooleanAnswer, IntegerAnswer, StringAnswer

Class Method Summary collapse

Class Method Details

.answer_class_for(value) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/models/helena/answer.rb', line 18

def self.answer_class_for(value)
  case value
  when Integer
    Helena::IntegerAnswer
  when TrueClass
    Helena::BooleanAnswer
  when FalseClass
    Helena::BooleanAnswer
  when String
    Helena::StringAnswer
  end
end

.build_generic(code, value, ip_address) ⇒ Object



13
14
15
16
# File 'app/models/helena/answer.rb', line 13

def self.build_generic(code, value, ip_address)
  value = cast_value(value)
  answer_class_for(value).new(code: code, value: value, ip_address: ip_address)
end

.cast_value(value) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'app/models/helena/answer.rb', line 31

def self.cast_value(value)
  if value == 'true'
    true
  elsif value == 'false'
    false
  elsif integer?(value)
    value.to_i
  else
    value.to_s
  end
end

.integer?(str) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
# File 'app/models/helena/answer.rb', line 43

def self.integer?(str)
  Integer(str)
rescue StandardError
  false
end