Class: Virtus::Coercion::Numeric

Inherits:
Object show all
Defined in:
lib/virtus/coercion/numeric.rb

Overview

Base class for all numeric Coercion classes

Direct Known Subclasses

Decimal, Float, Integer

Constant Summary

Constants inherited from Object

Object::COERCION_METHOD_REGEXP

Constants included from TypeLookup

TypeLookup::TYPE_FORMAT

Class Method Summary collapse

Methods inherited from Object

to_array, to_hash

Methods inherited from Virtus::Coercion

[]

Methods included from TypeLookup

#determine_type, extended, #primitive

Methods included from Options

#accept_options, #accepted_options, #options

Class Method Details

.to_decimal(value) ⇒ BigDecimal

Coerce a BigDecimal instance from a numeric object

Examples:

Virtus::Coercion::Numeric.to_decimal(Rational(2, 2))  # => BigDecimal('1.0')

Parameters:

Returns:

  • (BigDecimal)


60
61
62
# File 'lib/virtus/coercion/numeric.rb', line 60

def self.to_decimal(value)
  to_string(value).to_d
end

.to_float(value) ⇒ Float

Creates a Float instance from a numeric object

Examples:

Virtus::Coercion::Numeric.to_float(Rational(2, 2))  # => 1.0

Parameters:

Returns:



46
47
48
# File 'lib/virtus/coercion/numeric.rb', line 46

def self.to_float(value)
  value.to_f
end

.to_integer(value) ⇒ Integer

Creates an Integer instance from a numeric object

Examples:

Virtus::Coercion::Numeric.to_integer(Rational(2, 2))  # => 1

Parameters:

Returns:



32
33
34
# File 'lib/virtus/coercion/numeric.rb', line 32

def self.to_integer(value)
  value.to_i
end

.to_string(value) ⇒ String

Coerce given value to String

Examples:

Virtus::Coercion::Numeric.to_string(Rational(2, 2))  # => "1.0"

Parameters:

Returns:



18
19
20
# File 'lib/virtus/coercion/numeric.rb', line 18

def self.to_string(value)
  value.to_s
end