Module: Tunable::Normalizer

Included in:
Tunable
Defined in:
lib/tunable/normalizer.rb

Constant Summary collapse

TRUTHIES =
['true', 't', 'on', 'yes', 'y', '1'].freeze
FALSIES =
['false', 'f', 'off', 'no', 'n', '0'].freeze

Instance Method Summary collapse

Instance Method Details

#getter_value(normalized) ⇒ Object



16
17
18
# File 'lib/tunable/normalizer.rb', line 16

def getter_value(normalized)
  return normalized === 1 ? true : normalized === 0 ? false : normalized
end

#matching_type(a, b) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/tunable/normalizer.rb', line 25

def matching_type(a, b)
  if [true, false].include?(a)
    return [true, false].include?(b)
  else
    a.class == b.class
  end
end

#normalize_and_get(val) ⇒ Object

Called from Setting#normalized_value and DeviceActions#toggle_action



21
22
23
# File 'lib/tunable/normalizer.rb', line 21

def normalize_and_get(val)
  getter_value(normalize_value(val))
end

#normalize_value(val) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/tunable/normalizer.rb', line 8

def normalize_value(val)
  return 1 if TRUTHIES.include?(val.to_s)
  return 0 if FALSIES.include?(val.to_s)
  return Integer(val) if is_integer?(val)
  return if val.blank? # false.blank? returns true so this needs to go after the 0 line
  val
end