Class: Coercible::Coercer::Integer
- 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
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
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
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
41 42 43 |
# File 'lib/project/coercer/integer.rb', line 41 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
48 49 50 |
# File 'lib/project/coercer/integer.rb', line 48 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
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
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
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
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
79 80 81 |
# File 'lib/project/coercer/integer.rb', line 79 def to_string(value) value.to_s end |