Class: Coercible::Coercer::Integer

Inherits:
Numeric show all
Extended by:
Configurable
Defined in:
lib/project/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

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



62
63
64
65
66
67
# File 'lib/project/coercer/integer.rb', line 62

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)


55
56
57
# File 'lib/project/coercer/integer.rb', line 55

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)


41
42
43
# File 'lib/project/coercer/integer.rb', line 41

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)


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

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:



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/project/coercer/integer.rb', line 22

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:



110
111
112
113
114
# File 'lib/project/coercer/integer.rb', line 110

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:



126
127
128
# File 'lib/project/coercer/integer.rb', line 126

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:



93
94
95
# File 'lib/project/coercer/integer.rb', line 93

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:



79
80
81
# File 'lib/project/coercer/integer.rb', line 79

def to_string(value)
  value.to_s
end