Class: Coercible::Coercer::String

Inherits:
Object
  • Object
show all
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

Options::Undefined

Constants included from TypeLookup

TypeLookup::TYPE_FORMAT

Instance Attribute Summary collapse

Attributes inherited from Object

#coercers

Class Method Summary collapse

Instance Method Summary collapse

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

Parameters:



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

Returns:

  • (::Hash)


39
40
41
# File 'lib/coercible/coercer/string.rb', line 39

def boolean_map
  @boolean_map
end

Class Method Details

.configConfiguration

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

Returns:



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

Examples:

with “T”

coercer[String].to_boolean('T')  # => true

with “F”

coercer[String].to_boolean('F')  # => false

Parameters:

  • (#to_s)

Returns:

  • (Boolean)


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

Examples:

coercer[String].to_constant('String') # => String

Parameters:

Returns:



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

Examples:

coercer[String].to_date(string)  # => Date object

Parameters:

Returns:



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

Examples:

coercer[String].to_datetime(string)  # => DateTime object

Parameters:

Returns:



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

Examples:

coercer[String].to_decimal('1.2')  # => #<BigDecimal:b72157d4,'0.12E1',8(8)>

Parameters:

Returns:

  • (BigDecimal)


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

Examples:

coercer[String].to_float('1.2')  # => 1.2

Parameters:

Returns:



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

Examples:

coercer[String].to_integer('1')  # => 1

Parameters:

Returns:



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

Examples:

coercer[String].to_symbol('string') # => :string

Parameters:

Returns:



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

Examples:

coercer[String].to_time(string)  # => Time object

Parameters:

Returns:



95
96
97
# File 'lib/coercible/coercer/string.rb', line 95

def to_time(value)
  parse_value(::Time, value, __method__)
end