Class: Virtus::Coercion::Integer

Inherits:
Numeric show all
Defined in:
lib/virtus/coercion/integer.rb

Overview

Coerce Fixnum values

Constant Summary

Constants inherited from Object

Object::COERCION_METHOD_REGEXP

Constants included from TypeLookup

TypeLookup::TYPE_FORMAT

Class Method Summary collapse

Methods inherited from Numeric

to_decimal, to_float

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_boolean(value) ⇒ BigDecimal

Coerce given value to a Boolean

Examples:

with a 1

Virtus::Coercion::Fixnum.to_boolean(1)  # => true

with a 0

Virtus::Coercion::Fixnum.to_boolean(0)  # => false

Parameters:

  • value (Fixnum)

Returns:

  • (BigDecimal)


49
50
51
52
53
54
55
56
# File 'lib/virtus/coercion/integer.rb', line 49

def self.to_boolean(value)
  case value
  when 1 then true
  when 0 then false
  else
    value
  end
end

.to_datetime(value) ⇒ DateTime

Coerce given value to a DateTime

Examples:

Virtus::Coercion::Fixnum.to_datetime(0)  # => Thu, 01 Jan 1970 00:00:00 +0000

Parameters:

Returns:



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/virtus/coercion/integer.rb', line 68

def self.to_datetime(value)
  # FIXME: Remove after Rubinius 2.0 is released
  if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx'
    datetime_format = '%Q'
    value = "#{value * 10**3}"
  else
    datetime_format = '%s'
    value = "#{value}"
  end

  ::DateTime.strptime(value, datetime_format)
end

.to_integer(value) ⇒ Float

Passthrough the value

Examples:

Virtus::Coercion::Fixnum.to_integer(1)  # => 1

Parameters:

  • value (Fixnum)

Returns:



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

def self.to_integer(value)
  value
end

.to_string(value) ⇒ String

Coerce given value to String

Examples:

Virtus::Coercion::Fixnum.to_string(1)  # => "1"

Parameters:

  • value (Fixnum)

Returns:



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

def self.to_string(value)
  value.to_s
end