Sha256: e13530c16a77a9214151a5ac1596f1c525f1e3fd55988f36b846b7649d827803

Contents?: true

Size: 1.08 KB

Versions: 8

Compression:

Stored size: 1.08 KB

Contents

module Flipper
  module Typecast
    TruthMap = {
      true    => true,
      1       => true,
      "true"  => true,
      "1"     => true,
    }.freeze

    # Internal: Convert value to a boolean.
    #
    # Returns true or false.
    def self.to_boolean(value)
      !!TruthMap[value]
    end

    # Internal: Convert value to an integer.
    #
    # Returns an Integer representation of the value.
    # Raises ArgumentError if conversion is not possible.
    def self.to_integer(value)
      if value.respond_to?(:to_i)
        value.to_i
      else
        raise ArgumentError, "#{value.inspect} cannot be converted to an integer"
      end
    end

    # Internal: Convert value to a set.
    #
    # Returns a Set representation of the value.
    # Raises ArgumentError if conversion is not possible.
    def self.to_set(value)
      return value if value.is_a?(Set)
      return Set.new if value.nil? || value.empty?

      if value.respond_to?(:to_set)
        value.to_set
      else
        raise ArgumentError, "#{value.inspect} cannot be converted to a set"
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
flipper-0.10.2 lib/flipper/typecast.rb
flipper-0.10.1 lib/flipper/typecast.rb
flipper-0.10.0 lib/flipper/typecast.rb
flipper-0.9.2 lib/flipper/typecast.rb
flipper-0.9.1 lib/flipper/typecast.rb
flipper-0.9.0 lib/flipper/typecast.rb
flipper-0.9.0.beta1 lib/flipper/typecast.rb
flipper-0.8.0 lib/flipper/typecast.rb