class Coercible::Coercer::Integer

Coerce Fixnum values

Attributes

boolean_map[R]

Return boolean map from config

@return [::Hash]

@api private

datetime_format[R]

Return datetime format from config

@return [::String]

@api private

datetime_proc[R]

Return datetime proc from config

@return [Proc]

@api private

Public Class Methods

config() click to toggle source

Return default config for Integer coercer type

@return [Configuration]

@see Coercible::Coercer::Configurable#config

@api private

Calls superclass method
# File lib/coercible/coercer/integer.rb, line 19
def self.config
  super do |config|
    # FIXME: Remove after Rubinius 2.0 is released
    config.datetime_format, config.datetime_proc =
      if Coercible.rbx?
        [ '%Q', Proc.new { |value| "#{value * 10**3}" } ]
      else
        [ '%s', Proc.new { |value| "#{value}" } ]
      end

    config.boolean_map = { 0 => false, 1 => true }
  end
end
new(coercer = Coercer.new, config = self.class.config) click to toggle source

Initialize a new Integer coercer instance and set its configuration

@return [undefined]

@api private

Calls superclass method
# File lib/coercible/coercer/integer.rb, line 59
def initialize(coercer = Coercer.new, config = self.class.config)
  super(coercer)
  @boolean_map     = config.boolean_map
  @datetime_format = config.datetime_format
  @datetime_proc   = config.datetime_proc
end

Public Instance Methods

to_boolean(value) click to toggle source

Coerce given value to a Boolean

@example with a 1

coercer[Integer].to_boolean(1)  # => true

@example with a 0

coercer[Integer].to_boolean(0)  # => false

@param [Fixnum] value

@return [BigDecimal]

@api public

# File lib/coercible/coercer/integer.rb, line 107
def to_boolean(value)
  boolean_map.fetch(value) {
    raise_unsupported_coercion(value, __method__)
  }
end
to_datetime(value) click to toggle source

Coerce given value to a DateTime

@example

coercer[Integer].to_datetime(0)  # => Thu, 01 Jan 1970 00:00:00 +0000

@param [Integer] value

@return [DateTime]

@api public

# File lib/coercible/coercer/integer.rb, line 123
def to_datetime(value)
  ::DateTime.strptime(datetime_proc.call(value), datetime_format)
end
to_integer(value) click to toggle source

Passthrough the value

@example

coercer[Integer].to_integer(1)  # => 1

@param [Fixnum] value

@return [Float]

@api public

# File lib/coercible/coercer/integer.rb, line 90
def to_integer(value)
  value
end
to_string(value) click to toggle source

Coerce given value to String

@example

coercer[Integer].to_string(1)  # => "1"

@param [Fixnum] value

@return [String]

@api public

# File lib/coercible/coercer/integer.rb, line 76
def to_string(value)
  value.to_s
end