Module: RunSignup::DataCoercion

Included in:
Race
Defined in:
lib/run_signup/data_coercion.rb

Instance Method Summary collapse

Instance Method Details

#coerce_hash_values(hash) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/run_signup/data_coercion.rb', line 3

def coerce_hash_values hash
  if hash
    hash.each do |k, v|
      hash[k] = coerce_value(v)
    end
  end
  hash
end

#coerce_value(v) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/run_signup/data_coercion.rb', line 12

def coerce_value v
  if v.is_a?(Array)
    v.map { |v1| coerce_value(v1) }
  elsif v.is_a?(Hash)
    coerce_hash_values(v)
  else
    case v
    when 'T'
      true
    when 'F'
      false
    when /^\d{1,2}\/\d{1,2}\/\d{4} \d{2}:\d{2}$/
      DateTime.strptime(v, '%m/%d/%Y %H:%M').to_s
    when /^\d{1,2}\/\d{1,2}\/\d{4}$/
      Date.strptime(v, '%m/%d/%Y').to_s
    else
      v
    end
  end
end