class Coercible::Coercer::String
Coerce String values
Constants
- BOOLEAN_MAP
- EXPONENT_REGEXP
- FALSE_VALUES
- FRACTIONAL_REGEXP
- INTEGER_REGEXP
- NUMERIC_REGEXP
- TRUE_VALUES
Attributes
Return boolean map from the config
@return [::Hash]
@api private
Public Class Methods
Return default configuration for string coercer type
@return [Configuration]
@api private
# File lib/coercible/coercer/string.rb, line 30 def self.config super { |config| config.boolean_map = BOOLEAN_MAP } end
Initialize a new string coercer instance
@param [Coercer]
@param [Configuration]
@return [undefined]
@api private
# 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
Public Instance Methods
Coerce value to TrueClass or FalseClass
@example with âTâ
coercer[String].to_boolean('T') # => true
@example with âFâ
coercer[String].to_boolean('F') # => false
@param [#to_s]
@return [Boolean]
@api public
# File lib/coercible/coercer/string.rb, line 140 def to_boolean(value) boolean_map.fetch(value.downcase) { raise_unsupported_coercion(value, __method__) } end
Coerce give value to a constant
@example
coercer[String].to_constant('String') # => String
@param [String] value
@return [Object]
@api public
# 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
Coerce given value to Date
@example
coercer[String].to_date(string) # => Date object
@param [String] value
@return [Date]
@api public
# File lib/coercible/coercer/string.rb, line 109 def to_date(value) parse_value(::Date, value, __method__) end
Coerce given value to DateTime
@example
coercer[String].to_datetime(string) # => DateTime object
@param [String] value
@return [DateTime]
@api public
# File lib/coercible/coercer/string.rb, line 123 def to_datetime(value) parse_value(::DateTime, value, __method__) end
Coerce value to decimal
@example
coercer[String].to_decimal('1.2') # => #<BigDecimal:b72157d4,'0.12E1',8(8)>
@param [Object] value
@return [BigDecimal]
@api public
# 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
Coerce value to float
@example
coercer[String].to_float('1.2') # => 1.2
@param [Object] value
@return [Float]
@api public
# 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
Coerce value to integer
@example
coercer[String].to_integer('1') # => 1
@param [Object] value
@return [Integer]
@api public
# 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
Coerce give value to a symbol
@example
coercer[String].to_symbol('string') # => :string
@param [String] value
@return [Symbol]
@api public
# File lib/coercible/coercer/string.rb, line 81 def to_symbol(value) value.to_sym end
Coerce given value to Time
@example
coercer[String].to_time(string) # => Time object
@param [String] value
@return [Time]
@api public
# File lib/coercible/coercer/string.rb, line 95 def to_time(value) parse_value(::Time, value, __method__) end
Private Instance Methods
Lookup a constant within a module
@param [Module] mod
@param [String] name
@return [Object]
@api private
# File lib/coercible/coercer/string.rb, line 211 def constant_lookup(mod, name) if mod.const_defined?(name, *EXTRA_CONST_ARGS) mod.const_get(name, *EXTRA_CONST_ARGS) else mod.const_missing(name) end end
Parse the value or return it as-is if it is invalid
@param [#parse] parser
@param [String] value
@return [Time]
@api private
# File lib/coercible/coercer/string.rb, line 247 def parse_value(parser, value, method) parser.parse(value) rescue ArgumentError raise_unsupported_coercion(value, method) end
Match numeric string
@param [String] value
value to typecast
@param [Symbol] method
method to typecast with
@return [Numeric]
number if matched, value if no match
@api private
# File lib/coercible/coercer/string.rb, line 230 def to_numeric(value, method) if value =~ NUMERIC_REGEXP $1.public_send(method) else raise_unsupported_coercion(value, method) end end