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 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
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_symbol(value) ⇒ Symbol
Coerce give value to a symbol.
-
.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
#determine_type, extended, #primitive
Methods included from Options
#accept_options, #accepted_options, #options
Class Method Details
.to_boolean(value) ⇒ Boolean
Coerce value to TrueClass or FalseClass
121 122 123 |
# File 'lib/virtus/coercion/string.rb', line 121 def self.to_boolean(value) BOOLEAN_MAP.fetch(value.downcase, value) end |
.to_constant(value) ⇒ Object
Coerce give value to a constant
27 28 29 30 31 |
# 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) { |*args| constant_lookup(*args) } end |
.to_date(value) ⇒ Date
Coerce given value to Date
90 91 92 |
# File 'lib/virtus/coercion/string.rb', line 90 def self.to_date(value) parse_value(::Date, value) end |
.to_datetime(value) ⇒ DateTime
Coerce given value to DateTime
104 105 106 |
# File 'lib/virtus/coercion/string.rb', line 104 def self.to_datetime(value) parse_value(::DateTime, value) end |
.to_decimal(value) ⇒ BigDecimal
Coerce value to decimal
170 171 172 |
# File 'lib/virtus/coercion/string.rb', line 170 def self.to_decimal(value) to_numeric(value, :to_d) end |
.to_float(value) ⇒ Float
Coerce value to float
156 157 158 |
# File 'lib/virtus/coercion/string.rb', line 156 def self.to_float(value) to_numeric(value, :to_f) end |
.to_integer(value) ⇒ Integer
Coerce value to integer
135 136 137 138 139 140 141 142 143 144 |
# File 'lib/virtus/coercion/string.rb', line 135 def self.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 coerced = to_float(value) ::Float === coerced ? coerced.to_i : coerced end end |
.to_symbol(value) ⇒ Symbol
Coerce give value to a symbol
62 63 64 |
# File 'lib/virtus/coercion/string.rb', line 62 def self.to_symbol(value) value.to_sym end |
.to_time(value) ⇒ Time
Coerce given value to Time
76 77 78 |
# File 'lib/virtus/coercion/string.rb', line 76 def self.to_time(value) parse_value(::Time, value) end |