Class: Coercible::Coercer::String
- Extended by:
- Configurable
- Defined in:
- lib/coercible/coercer/string.rb
Overview
Coerce String values
Constant Summary collapse
- TRUE_VALUES =
%w[ 1 on t true y yes ].freeze
- FALSE_VALUES =
%w[ 0 off f false n no ].freeze
- BOOLEAN_MAP =
::Hash[ TRUE_VALUES.product([ true ]) + FALSE_VALUES.product([ false ]) ].freeze
- INTEGER_REGEXP =
/[-+]?(?:[0-9]\d*)/.freeze
- EXPONENT_REGEXP =
/(?:[eE][-+]?\d+)/.freeze
- FRACTIONAL_REGEXP =
/(?:\.\d+)/.freeze
- NUMERIC_REGEXP =
/\A( #{INTEGER_REGEXP}#{FRACTIONAL_REGEXP}?#{EXPONENT_REGEXP}? | #{FRACTIONAL_REGEXP}#{EXPONENT_REGEXP}? )\z/x.freeze
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 the config.
Attributes inherited from Object
Class Method Summary collapse
-
.config ⇒ Configuration
private
Return default configuration for string coercer type.
Instance Method Summary collapse
-
#initialize(coercer = Coercer.new, config = self.class.config) ⇒ undefined
constructor
private
Initialize a new string coercer instance.
-
#to_boolean(value) ⇒ Boolean
Coerce value to TrueClass or FalseClass.
-
#to_constant(value) ⇒ Object
Coerce give value to a constant.
-
#to_date(value) ⇒ Date
Coerce given value to Date.
-
#to_datetime(value) ⇒ DateTime
Coerce given value to DateTime.
-
#to_decimal(value) ⇒ BigDecimal
Coerce value to decimal.
-
#to_float(value) ⇒ Float
Coerce value to float.
-
#to_integer(value) ⇒ Integer
Coerce value to integer.
-
#to_symbol(value) ⇒ Symbol
Coerce give value to a symbol.
-
#to_time(value) ⇒ Time
Coerce given value to Time.
Methods included from Configurable
config, config_name, configuration_class, extended
Methods inherited from Object
#coerced?, #inspect, #to_array, #to_hash, #to_string
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 string coercer instance
50 51 52 53 |
# File 'lib/coercible/coercer/string.rb', line 50 def initialize(coercer = Coercer.new, config = self.class.config) super(coercer) @boolean_map = config.boolean_map 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 the config
39 40 41 |
# File 'lib/coercible/coercer/string.rb', line 39 def boolean_map @boolean_map 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 configuration for string coercer type
30 31 32 |
# File 'lib/coercible/coercer/string.rb', line 30 def self.config super { |config| config.boolean_map = BOOLEAN_MAP } end |
Instance Method Details
#to_boolean(value) ⇒ Boolean
Coerce value to TrueClass or FalseClass
140 141 142 143 144 |
# File 'lib/coercible/coercer/string.rb', line 140 def to_boolean(value) boolean_map.fetch(value.downcase) { raise_unsupported_coercion(value, __method__) } end |
#to_constant(value) ⇒ Object
Coerce give value to a constant
65 66 67 68 69 |
# File 'lib/coercible/coercer/string.rb', line 65 def to_constant(value) names = value.split('::') names.shift if names.first.empty? names.inject(::Object) { |*args| constant_lookup(*args) } end |
#to_date(value) ⇒ Date
Coerce given value to Date
109 110 111 |
# File 'lib/coercible/coercer/string.rb', line 109 def to_date(value) parse_value(::Date, value, __method__) end |
#to_datetime(value) ⇒ DateTime
Coerce given value to DateTime
123 124 125 |
# File 'lib/coercible/coercer/string.rb', line 123 def to_datetime(value) parse_value(::DateTime, value, __method__) end |
#to_decimal(value) ⇒ BigDecimal
Coerce value to decimal
194 195 196 197 198 |
# File 'lib/coercible/coercer/string.rb', line 194 def to_decimal(value) to_numeric(value, :to_d) rescue UnsupportedCoercion raise_unsupported_coercion(value, __method__) end |
#to_float(value) ⇒ Float
Coerce value to float
178 179 180 181 182 |
# File 'lib/coercible/coercer/string.rb', line 178 def to_float(value) to_numeric(value, :to_f) rescue UnsupportedCoercion raise_unsupported_coercion(value, __method__) end |
#to_integer(value) ⇒ Integer
Coerce value to integer
156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/coercible/coercer/string.rb', line 156 def to_integer(value) if value =~ /\A#{INTEGER_REGEXP}\z/ value.to_i else # coerce to a Float first to evaluate scientific notation (if any) # that may change the integer part, then convert to an integer to_float(value).to_i end rescue UnsupportedCoercion raise_unsupported_coercion(value, __method__) end |
#to_symbol(value) ⇒ Symbol
Coerce give value to a symbol
81 82 83 |
# File 'lib/coercible/coercer/string.rb', line 81 def to_symbol(value) value.to_sym end |
#to_time(value) ⇒ Time
Coerce given value to Time
95 96 97 |
# File 'lib/coercible/coercer/string.rb', line 95 def to_time(value) parse_value(::Time, value, __method__) end |