Object
Coerce String values
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
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
Generated with the Darkfish Rdoc Generator 2.