Sha256: f189e4a4d415474851b769763393a1c94f439436b913561969f67c926bc789f6

Contents?: true

Size: 1.05 KB

Versions: 4

Compression:

Stored size: 1.05 KB

Contents

class RestModel
  module Serialization
    class Boolean
      MAPPINGS  = {
        true    => true,
        "true"  => true,
        "TRUE"  => true,
        "1"     => true,
        1       => true,
        1.0     => true,
        "x"     => true,
        "X"     => true,
        "t"     => true,
        "T"     => true,
        false   => false,
        "false" => false,
        "FALSE" => false,
        "0"     => false,
        0       => false,
        0.0     => false,
        ""      => false,
        " "     => false,
        "f"     => false,
        "F"     => false,
        nil     => false
      }.freeze

      def self.serialize(value)
        MAPPINGS[value].tap do |bool|
          fail "value not serializable: #{value}" if bool.nil?
        end
      end

      def self.desserialize(value)
        fail unless boolean?(value)

        value ? RestModel::Configuration.true_value
              : RestModel::Configuration.false_value
      end

      private

      def self.boolean?(value)
        !!value == value
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rest_model-0.1.6 lib/rest_model/serialization/boolean.rb
rest_model-0.1.5 lib/rest_model/serialization/boolean.rb
rest_model-0.1.4 lib/rest_model/serialization/boolean.rb
rest_model-0.1.3 lib/rest_model/serialization/boolean.rb