Class: Virtus::Coercion::String
- Inherits:
-
Object
- Object
- Virtus::Coercion
- Object
- Virtus::Coercion::String
- Defined in:
- lib/virtus/coercion/string.rb
Overview
Coerce String values
Constant Summary collapse
- TRUE_VALUES =
%w[ 1 t true ].freeze
- FALSE_VALUES =
%w[ 0 f false ].freeze
- BOOLEAN_MAP =
::Hash[ TRUE_VALUES.product([ true ]) + FALSE_VALUES.product([ false ]) ].freeze
- NUMERIC_REGEXP =
/\A(-?(?:0|[1-9]\d*)(?:\.\d+)?|(?:\.\d+))\z/.freeze
Constants inherited from Object
Object::COERCION_METHOD_REGEXP
Constants included from TypeLookup
Class Method Summary collapse
-
.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_time(value) ⇒ Time
Coerce given value to Time.
Methods inherited from Object
Methods inherited from Virtus::Coercion
Methods included from DescendantsTracker
Methods included from TypeLookup
Methods included from Options
#accept_options, #accepted_options, #options
Class Method Details
.to_boolean(value) ⇒ Boolean
Coerce value to TrueClass or FalseClass
84 85 86 |
# File 'lib/virtus/coercion/string.rb', line 84 def self.to_boolean(value) BOOLEAN_MAP.fetch(value.downcase, value) end |
.to_constant(value) ⇒ Object
Coerce give value to a constant
24 25 26 27 |
# File 'lib/virtus/coercion/string.rb', line 24 def self.to_constant(value) # TODO: add support for namespaced classes like 'Virtus::Attribute::String' ::Object.const_get(value) end |
.to_date(value) ⇒ Date
Coerce given value to Date
53 54 55 |
# File 'lib/virtus/coercion/string.rb', line 53 def self.to_date(value) parse_value(::Date, value) end |
.to_datetime(value) ⇒ DateTime
Coerce given value to DateTime
67 68 69 |
# File 'lib/virtus/coercion/string.rb', line 67 def self.to_datetime(value) parse_value(::DateTime, value) end |
.to_decimal(value) ⇒ BigDecimal
Coerce value to decimal
126 127 128 |
# File 'lib/virtus/coercion/string.rb', line 126 def self.to_decimal(value) to_numeric(value, :to_d) end |
.to_float(value) ⇒ Float
Coerce value to float
112 113 114 |
# File 'lib/virtus/coercion/string.rb', line 112 def self.to_float(value) to_numeric(value, :to_f) end |
.to_integer(value) ⇒ Integer
Coerce value to integer
98 99 100 |
# File 'lib/virtus/coercion/string.rb', line 98 def self.to_integer(value) to_numeric(value, :to_i) end |
.to_time(value) ⇒ Time
Coerce given value to Time
39 40 41 |
# File 'lib/virtus/coercion/string.rb', line 39 def self.to_time(value) parse_value(::Time, value) end |