Sha256: 638a232f92db3e710ba7307f8818e64801b72a0990dea0b3f40ec99ebf7565cd

Contents?: true

Size: 1.39 KB

Versions: 14

Compression:

Stored size: 1.39 KB

Contents

module Coercible
  class Coercer

    # Base class for all numeric Coercion classes
    class Numeric < Object
      primitive ::Numeric

      # Coerce given value to String
      #
      # @example
      #   coercer[Numeric].to_string(Rational(2, 2))  # => "1.0"
      #
      # @param [Numeric] value
      #
      # @return [String]
      #
      # @api public
      def to_string(value)
        value.to_s
      end

      # Creates an Integer instance from a numeric object
      #
      # @example
      #   coercer[Numeric].to_integer(Rational(2, 2))  # => 1
      #
      # @param [Numeric] value
      #
      # @return [Integer]
      #
      # @api public
      def to_integer(value)
        value.to_i
      end

      # Creates a Float instance from a numeric object
      #
      # @example
      #   coercer[Numeric].to_float(Rational(2, 2))  # => 1.0
      #
      # @param [Numeric] value
      #
      # @return [Float]
      #
      # @api public
      def to_float(value)
        value.to_f
      end

      # Coerce a BigDecimal instance from a numeric object
      #
      # @example
      #   coercer[Numeric].to_decimal(Rational(2, 2))  # => BigDecimal('1.0')
      #
      # @param [Numeric] value
      #
      # @return [BigDecimal]
      #
      # @api public
      def to_decimal(value)
        to_string(value).to_d
      end

    end # class Numeric

  end # class Coercer
end # module Coercible

Version data entries

14 entries across 12 versions & 4 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/coercible-1.0.0/lib/coercible/coercer/numeric.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/coercible-1.0.0/lib/coercible/coercer/numeric.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/coercible-1.0.0/lib/coercible/coercer/numeric.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/coercible-1.0.0/lib/coercible/coercer/numeric.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/coercible-1.0.0/lib/coercible/coercer/numeric.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/coercible-1.0.0/lib/coercible/coercer/numeric.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/coercible-1.0.0/lib/coercible/coercer/numeric.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/coercible-1.0.0/lib/coercible/coercer/numeric.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/coercible-1.0.0/lib/coercible/coercer/numeric.rb
coercible-1.0.0 lib/coercible/coercer/numeric.rb
coercible-0.2.0 lib/coercible/coercer/numeric.rb
coercible-0.1.0 lib/coercible/coercer/numeric.rb
coercible-0.0.2 lib/coercible/coercer/numeric.rb
coercible-0.0.1 lib/coercible/coercer/numeric.rb