Sha256: 28a620586e814f38e8e42f041dfcf3180309a03cf6997df4afeb2b0088b9b7a1

Contents?: true

Size: 1.37 KB

Versions: 32

Compression:

Stored size: 1.37 KB

Contents

# frozen-string-literal: true
#
# The LooserTypecasting extension loosens the default database typecasting
# for the following types:
#
# :float :: use to_f instead of Float()
# :integer :: use to_i instead of Integer()
# :decimal :: use 0.0 for unsupported strings
# :string :: silently allow hash and array conversion to string
#
# This also removes bytesize checks for string inputs for float, integer
# and decimal conversions.
#
# To load the extension into the database:
#
#   DB.extension :looser_typecasting
#
# Related module: Sequel::LooserTypecasting

#
module Sequel
  module LooserTypecasting
    private

    # Typecast the value to a Float using to_f instead of Kernel.Float
    def typecast_value_float(value)
      value.to_f
    end

    # Typecast the value to an Integer using to_i instead of Kernel.Integer
    def typecast_value_integer(value)
      value.to_i
    end

    # Typecast the value to an String using to_s instead of Kernel.String
    def typecast_value_string(value)
      value.to_s
    end

    if RUBY_VERSION >= '2.4'
      def _typecast_value_string_to_decimal(value)
        BigDecimal(value)
      rescue
        BigDecimal('0.0')
      end
    else
      # :nocov:
      def _typecast_value_string_to_decimal(value)
        BigDecimal(value)
      end
      # :nocov:
    end
  end

  Database.register_extension(:looser_typecasting, LooserTypecasting)
end

Version data entries

32 entries across 32 versions & 2 rubygems

Version Path
sequel-5.90.0 lib/sequel/extensions/looser_typecasting.rb
sequel-5.89.0 lib/sequel/extensions/looser_typecasting.rb
sequel-5.88.0 lib/sequel/extensions/looser_typecasting.rb
sequel-5.87.0 lib/sequel/extensions/looser_typecasting.rb
sequel-5.86.0 lib/sequel/extensions/looser_typecasting.rb
sequel-5.85.0 lib/sequel/extensions/looser_typecasting.rb
sequel-5.84.0 lib/sequel/extensions/looser_typecasting.rb
sequel-5.83.1 lib/sequel/extensions/looser_typecasting.rb
sequel-5.83.0 lib/sequel/extensions/looser_typecasting.rb
sequel-5.82.0 lib/sequel/extensions/looser_typecasting.rb
sequel-5.81.0 lib/sequel/extensions/looser_typecasting.rb
sequel-5.80.0 lib/sequel/extensions/looser_typecasting.rb
sequel-5.79.0 lib/sequel/extensions/looser_typecasting.rb
sequel-5.78.0 lib/sequel/extensions/looser_typecasting.rb
sequel-5.77.0 lib/sequel/extensions/looser_typecasting.rb
sequel-5.76.0 lib/sequel/extensions/looser_typecasting.rb
sequel-5.75.0 lib/sequel/extensions/looser_typecasting.rb
sequel-5.74.0 lib/sequel/extensions/looser_typecasting.rb
sequel-5.73.0 lib/sequel/extensions/looser_typecasting.rb
sequel-5.72.0 lib/sequel/extensions/looser_typecasting.rb