Class: Coercible::Coercer::Integer
- 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
Constants included from TypeLookup
Instance Attribute Summary collapse
-
#boolean_map ⇒ ::Hash
readonly
private
Return boolean map from config.
-
#datetime_format ⇒ ::String
readonly
private
Return datetime format from config.
-
#datetime_proc ⇒ Proc
readonly
private
Return datetime proc from config.
Attributes inherited from Object
Class Method Summary collapse
-
.config ⇒ Configuration
private
Return default config for Integer coercer type.
Instance Method Summary collapse
-
#initialize(coercer = Coercer.new, config = self.class.config) ⇒ undefined
constructor
private
Initialize a new Integer coercer instance and set its configuration.
-
#to_boolean(value) ⇒ BigDecimal
Coerce given value to a Boolean.
-
#to_datetime(value) ⇒ DateTime
Coerce given value to a DateTime.
-
#to_integer(value) ⇒ Float
Passthrough the value.
-
#to_string(value) ⇒ String
Coerce given value to String.
Methods included from Configurable
config, config_name, configuration_class, extended
Methods inherited from Numeric
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
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
38 39 40 |
# File 'lib/coercible/coercer/integer.rb', line 38 def datetime_format @datetime_format end |
#datetime_proc ⇒ Proc (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
45 46 47 |
# File 'lib/coercible/coercer/integer.rb', line 45 def datetime_proc @datetime_proc end |
Class Method Details
.config ⇒ Configuration
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
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
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
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
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
76 77 78 |
# File 'lib/coercible/coercer/integer.rb', line 76 def to_string(value) value.to_s end |