Sha256: 4182a0f45a255238dd90cfe35c4dc5f42bc50b5e642d09dbc4968d427bdb1ae6

Contents?: true

Size: 981 Bytes

Versions: 4

Compression:

Stored size: 981 Bytes

Contents

module Tunable

  module Normalizer

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

    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

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

    # Called from Setting#normalized_value and DeviceActions#toggle_action
    def normalize_and_get(val)
      getter_value(normalize_value(val))
    end

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

    private

    def is_integer?(val)
      Integer(val)
      true
    rescue
      false
    end

  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
tunable-0.0.7 lib/tunable/normalizer.rb
tunable-0.0.6 lib/tunable/normalizer.rb
tunable-0.0.5 lib/tunable/normalizer.rb
tunable-0.0.4 lib/tunable/normalizer.rb