Sha256: 0c6a0cf79aca018a3943d2d07eb55e68aea1bc13dae101ce48805fd4e0dc3ede

Contents?: true

Size: 1.27 KB

Versions: 84

Compression:

Stored size: 1.27 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
#
# 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

84 entries across 69 versions & 2 rubygems

Version Path
sequel-5.60.1 lib/sequel/extensions/looser_typecasting.rb
sequel-5.60.0 lib/sequel/extensions/looser_typecasting.rb
tdiary-5.2.3 vendor/bundle/ruby/3.1.0/gems/sequel-5.59.0/lib/sequel/extensions/looser_typecasting.rb
sequel-5.59.0 lib/sequel/extensions/looser_typecasting.rb
sequel-5.58.0 lib/sequel/extensions/looser_typecasting.rb
sequel-5.57.0 lib/sequel/extensions/looser_typecasting.rb
tdiary-5.2.2 vendor/bundle/ruby/3.1.0/gems/sequel-5.56.0/lib/sequel/extensions/looser_typecasting.rb
sequel-5.56.0 lib/sequel/extensions/looser_typecasting.rb
sequel-5.55.0 lib/sequel/extensions/looser_typecasting.rb
sequel-5.54.0 lib/sequel/extensions/looser_typecasting.rb
tdiary-5.2.1 vendor/bundle/ruby/3.1.0/gems/sequel-5.53.0/lib/sequel/extensions/looser_typecasting.rb
sequel-5.53.0 lib/sequel/extensions/looser_typecasting.rb
sequel-5.52.0 lib/sequel/extensions/looser_typecasting.rb
sequel-5.51.0 lib/sequel/extensions/looser_typecasting.rb
tdiary-5.2.0 vendor/bundle/ruby/3.0.0/gems/sequel-5.50.0/lib/sequel/extensions/looser_typecasting.rb
tdiary-5.2.0 vendor/bundle/ruby/2.7.0/gems/sequel-5.44.0/lib/sequel/extensions/looser_typecasting.rb
sequel-5.50.0 lib/sequel/extensions/looser_typecasting.rb
sequel-5.49.0 lib/sequel/extensions/looser_typecasting.rb
sequel-5.48.0 lib/sequel/extensions/looser_typecasting.rb
tdiary-5.1.7 vendor/bundle/ruby/3.0.0/gems/sequel-5.47.0/lib/sequel/extensions/looser_typecasting.rb