Sha256: 326e73ab49381808eb98b69525a718588550d10436a1a60e3f4e98e56f7adb7b

Contents?: true

Size: 1.28 KB

Versions: 26

Compression:

Stored size: 1.28 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.new(value)
      rescue
        BigDecimal.new('0.0')
      end
    else
      # :nocov:
      def _typecast_value_string_to_decimal(value)
        BigDecimal.new(value)
      end
      # :nocov:
    end
  end

  Database.register_extension(:looser_typecasting, LooserTypecasting)
end

Version data entries

26 entries across 22 versions & 2 rubygems

Version Path
sequel-5.8.0 lib/sequel/extensions/looser_typecasting.rb
sequel-5.7.1 lib/sequel/extensions/looser_typecasting.rb
sequel-5.7.0 lib/sequel/extensions/looser_typecasting.rb
tdiary-5.0.8 vendor/bundle/gems/tdiary-5.0.7/vendor/bundle/gems/sequel-5.3.0/lib/sequel/extensions/looser_typecasting.rb
tdiary-5.0.8 vendor/bundle/ruby/2.5.0/gems/sequel-5.6.0/lib/sequel/extensions/looser_typecasting.rb
tdiary-5.0.8 vendor/bundle/gems/sequel-5.6.0/lib/sequel/extensions/looser_typecasting.rb
sequel-5.6.0 lib/sequel/extensions/looser_typecasting.rb
sequel-5.5.0 lib/sequel/extensions/looser_typecasting.rb
sequel-5.4.0 lib/sequel/extensions/looser_typecasting.rb
tdiary-5.0.7 vendor/bundle/gems/sequel-5.3.0/lib/sequel/extensions/looser_typecasting.rb
sequel-5.3.0 lib/sequel/extensions/looser_typecasting.rb
sequel-5.2.0 lib/sequel/extensions/looser_typecasting.rb
sequel-5.1.0 lib/sequel/extensions/looser_typecasting.rb
tdiary-5.0.6 vendor/bundle/gems/sequel-5.0.0/lib/sequel/extensions/looser_typecasting.rb
sequel-5.0.0 lib/sequel/extensions/looser_typecasting.rb
sequel-4.49.0 lib/sequel/extensions/looser_typecasting.rb
sequel-4.48.0 lib/sequel/extensions/looser_typecasting.rb
tdiary-5.0.5 vendor/bundle/gems/sequel-4.44.0/lib/sequel/extensions/looser_typecasting.rb
tdiary-5.0.5 vendor/bundle/gems/sequel-4.47.0/lib/sequel/extensions/looser_typecasting.rb
tdiary-5.0.5 vendor/bundle/gems/tdiary-5.0.4/vendor/bundle/gems/sequel-4.44.0/lib/sequel/extensions/looser_typecasting.rb