Sha256: 897271e3f87f18d6f8f91698a299680f7f2a41eaab87e54936cd377157bc7496

Contents?: true

Size: 1.44 KB

Versions: 9

Compression:

Stored size: 1.44 KB

Contents

module Virtus
  class Coercion

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

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

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

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

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

    end # class Numeric
  end # class Coercion
end # module Virtus

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
virtus-0.5.5 lib/virtus/coercion/numeric.rb
virtus-0.5.4 lib/virtus/coercion/numeric.rb
virtus-0.5.3 lib/virtus/coercion/numeric.rb
fragrant-0.0.5 vendor/bundle/ruby/1.9.1/gems/virtus-0.5.2/lib/virtus/coercion/numeric.rb
virtus-0.5.2 lib/virtus/coercion/numeric.rb
virtus-0.5.1 lib/virtus/coercion/numeric.rb
virtus-0.5.0 lib/virtus/coercion/numeric.rb
virtus-0.4.2 lib/virtus/coercion/numeric.rb
virtus-0.4.1 lib/virtus/coercion/numeric.rb