Class: Virtus::Coercion::String

Inherits:
Object show all
Defined in:
lib/virtus/coercion/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|[1-9]\d*)/.freeze
EXPONENT_REGEXP =
/(?:[eE][-+]?\d+)/.freeze
FRACTIONAL_REGEXP =
/(?:\.\d+#{EXPONENT_REGEXP}?)/.freeze
NUMERIC_REGEXP =
/\A(#{INTEGER_REGEXP}#{FRACTIONAL_REGEXP}?|#{FRACTIONAL_REGEXP})\z/.freeze

Constants inherited from Object

Object::COERCION_METHOD_REGEXP

Constants included from TypeLookup

TypeLookup::TYPE_FORMAT

Class Method Summary collapse

Methods inherited from Object

to_array, to_hash, to_string

Methods inherited from Virtus::Coercion

[]

Methods included from DescendantsTracker

#add_descendant, #descendants

Methods included from TypeLookup

#determine_type, #primitive

Methods included from Options

#accept_options, #accepted_options, #options

Class Method Details

.to_boolean(value) ⇒ Boolean

Coerce value to TrueClass or FalseClass

Examples:

with “T”

Virtus::Coercion::String.to_boolean('T')  # => true

with “F”

Virtus::Coercion::String.to_boolean('F')  # => false

Parameters:

  • (#to_s)

Returns:

  • (Boolean)


108
109
110
# File 'lib/virtus/coercion/string.rb', line 108

def self.to_boolean(value)
  BOOLEAN_MAP.fetch(value.downcase, value)
end

.to_constant(value) ⇒ Object

Coerce give value to a constant

Examples:

Virtus::Coercion::String.to_constant('String') # => String

Parameters:

Returns:



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/virtus/coercion/string.rb', line 27

def self.to_constant(value)
  names = value.split('::')
  names.shift if names.first.empty?
  names.inject(::Object) do |mod, name|
    if mod.const_defined?(name, *EXTRA_CONST_ARGS)
      mod.const_get(name, *EXTRA_CONST_ARGS)
    else
      mod.const_missing(name)
    end
  end
end

.to_date(value) ⇒ Date

Coerce given value to Date

Examples:

Virtus::Coercion::String.to_date(string)  # => Date object

Parameters:

Returns:



77
78
79
# File 'lib/virtus/coercion/string.rb', line 77

def self.to_date(value)
  parse_value(::Date, value)
end

.to_datetime(value) ⇒ DateTime

Coerce given value to DateTime

Examples:

Virtus::Coercion::String.to_datetime(string)  # => DateTime object

Parameters:

Returns:



91
92
93
# File 'lib/virtus/coercion/string.rb', line 91

def self.to_datetime(value)
  parse_value(::DateTime, value)
end

.to_decimal(value) ⇒ BigDecimal

Coerce value to decimal

Examples:

Virtus::Coercion::String.to_decimal('1.2')  # => #<BigDecimal:b72157d4,'0.12E1',8(8)>

Parameters:

Returns:

  • (BigDecimal)


153
154
155
# File 'lib/virtus/coercion/string.rb', line 153

def self.to_decimal(value)
  to_numeric(value, :to_d)
end

.to_float(value) ⇒ Float

Coerce value to float

Examples:

Virtus::Coercion::String.to_float('1.2')  # => 1.2

Parameters:

Returns:



139
140
141
# File 'lib/virtus/coercion/string.rb', line 139

def self.to_float(value)
  to_numeric(value, :to_f)
end

.to_integer(value) ⇒ Integer

Coerce value to integer

Examples:

Virtus::Coercion::String.to_integer('1')  # => 1

Parameters:

Returns:



122
123
124
125
126
127
# File 'lib/virtus/coercion/string.rb', line 122

def self.to_integer(value)
  # coerce to a Float first to evaluate scientific notation (if any)
  # that may change the integer part, then convert to an integer
  coerced = to_float(value)
  ::Float === coerced ? coerced.to_i : coerced
end

.to_symbol(value) ⇒ Symbol

Coerce give value to a symbol

Examples:

Virtus::Coercion::String.to_symbol('string') # => :string

Parameters:

Returns:



49
50
51
# File 'lib/virtus/coercion/string.rb', line 49

def self.to_symbol(value)
  value.to_sym
end

.to_time(value) ⇒ Time

Coerce given value to Time

Examples:

Virtus::Coercion::String.to_time(string)  # => Time object

Parameters:

Returns:



63
64
65
# File 'lib/virtus/coercion/string.rb', line 63

def self.to_time(value)
  parse_value(::Time, value)
end