Class: Coercible::Coercer::Integer

Inherits:
Numeric show all
Extended by:
Configurable
Defined in:
lib/coercible/coercer/integer.rb

Overview

Coerce Fixnum values

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 collapse

Attributes inherited from Object

#coercers

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Configurable

config, config_name, configuration_class, extended

Methods inherited from Numeric

#to_decimal, #to_float

Methods inherited from Object

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

Methods included from Options

#accept_options, #accepted_options, extended, #options

Methods included from TypeLookup

#determine_type, extended, #primitive

Constructor Details

#initialize(coercer = Coercer.new, config = self.class.config) ⇒ undefined

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize a new Integer coercer instance and set its configuration



59
60
61
62
63
64
# 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

Dynamic Method Handling

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

Instance Attribute Details

#boolean_map::Hash (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return boolean map from config

Returns:

  • (::Hash)


52
53
54
# File 'lib/coercible/coercer/integer.rb', line 52

def boolean_map
  @boolean_map
end

#datetime_format::String (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return datetime format from config

Returns:

  • (::String)


38
39
40
# File 'lib/coercible/coercer/integer.rb', line 38

def datetime_format
  @datetime_format
end

#datetime_procProc (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return datetime proc from config

Returns:

  • (Proc)


45
46
47
# File 'lib/coercible/coercer/integer.rb', line 45

def datetime_proc
  @datetime_proc
end

Class Method Details

.configConfiguration

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return default config for Integer coercer type

Returns:

See Also:



19
20
21
22
23
24
25
26
27
28
29
30
31
# 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

Instance Method Details

#to_boolean(value) ⇒ BigDecimal

Coerce given value to a Boolean

Examples:

with a 1

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

with a 0

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

Parameters:

  • value (Fixnum)

Returns:

  • (BigDecimal)


107
108
109
110
111
# 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) ⇒ DateTime

Coerce given value to a DateTime

Examples:

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

Parameters:

Returns:



123
124
125
# File 'lib/coercible/coercer/integer.rb', line 123

def to_datetime(value)
  ::DateTime.strptime(datetime_proc.call(value), datetime_format)
end

#to_integer(value) ⇒ Float

Passthrough the value

Examples:

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

Parameters:

  • value (Fixnum)

Returns:



90
91
92
# File 'lib/coercible/coercer/integer.rb', line 90

def to_integer(value)
  value
end

#to_string(value) ⇒ String

Coerce given value to String

Examples:

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

Parameters:

  • value (Fixnum)

Returns:



76
77
78
# File 'lib/coercible/coercer/integer.rb', line 76

def to_string(value)
  value.to_s
end