Class: Coercible::Coercer::Numeric

Inherits:
Object
  • Object
show all
Defined in:
lib/project/coercer/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 Options

Options::Undefined

Constants included from TypeLookup

TypeLookup::TYPE_FORMAT

Instance Attribute Summary

Attributes inherited from Object

#coercers

Instance Method Summary collapse

Methods inherited from Object

#coerced?, #initialize, #inspect, #to_array, #to_hash

Methods included from Options

#accept_options, #accepted_options, extended, #options

Methods included from TypeLookup

#determine_type, extended

Constructor Details

This class inherits a constructor from Coercible::Coercer::Object

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Coercible::Coercer::Object

Instance Method Details

#to_decimal(value) ⇒ BigDecimal

Coerce a BigDecimal instance from a numeric object

Examples:

coercer[Numeric].to_decimal(Rational(2, 2))  # => BigDecimal('1.0')

Parameters:

Returns:



62
63
64
# File 'lib/project/coercer/numeric.rb', line 62

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

#to_float(value) ⇒ Float

Creates a Float instance from a numeric object

Examples:

coercer[Numeric].to_float(Rational(2, 2))  # => 1.0

Parameters:

Returns:



48
49
50
# File 'lib/project/coercer/numeric.rb', line 48

def to_float(value)
  value.to_f
end

#to_integer(value) ⇒ Integer

Creates an Integer instance from a numeric object

Examples:

coercer[Numeric].to_integer(Rational(2, 2))  # => 1

Parameters:

Returns:



34
35
36
# File 'lib/project/coercer/numeric.rb', line 34

def to_integer(value)
  value.to_i
end

#to_string(value) ⇒ String

Coerce given value to String

Examples:

coercer[Numeric].to_string(Rational(2, 2))  # => "1.0"

Parameters:

Returns:



20
21
22
# File 'lib/project/coercer/numeric.rb', line 20

def to_string(value)
  value.to_s
end