Class: ParameterNormalizer::Normalizer

Inherits:
Object
  • Object
show all
Defined in:
lib/parameter_normalizer/normalizer.rb

Instance Method Summary collapse

Instance Method Details

#as_boolean(value) ⇒ Object



25
26
27
28
29
# File 'lib/parameter_normalizer/normalizer.rb', line 25

def as_boolean(value)
  return true if value.in?(['1', 1, true, 'true'])

  false
end

#as_date(value) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/parameter_normalizer/normalizer.rb', line 39

def as_date(value)
  return if value.blank?

  datetime = Time.zone.parse(value)
  datetime.to_date
rescue StandardError
  nil
end

#as_datetime(value) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/parameter_normalizer/normalizer.rb', line 31

def as_datetime(value)
  return if value.blank?

  Time.zone.parse(value)
rescue StandardError
  nil
end

#as_integer(value) ⇒ Object



3
4
5
6
7
# File 'lib/parameter_normalizer/normalizer.rb', line 3

def as_integer(value)
  Integer(value)
rescue StandardError
  nil
end

#as_integer_array(value) ⇒ Object



9
10
11
12
13
# File 'lib/parameter_normalizer/normalizer.rb', line 9

def as_integer_array(value)
  Array(value).filter_map do |record|
    as_integer(record)
  end
end

#as_integer_hash(value) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/parameter_normalizer/normalizer.rb', line 15

def as_integer_hash(value)
  {}.tap do |results|
    next if value.blank?

    value.each do |k, v|
      results[k] = as_integer(v)
    end
  end
end