class ENVied::Coercer::ENViedString

Constants

BOOLEAN_MAP
FALSE_VALUES
TRUE_VALUES

Public Instance Methods

to_array(str) click to toggle source
# File lib/envied/coercer/envied_string.rb, line 6
def to_array(str)
  str.split(/(?<!\\),/).map{|i| i.gsub(/\\,/,',') }
end
to_boolean(str) click to toggle source
# File lib/envied/coercer/envied_string.rb, line 10
def to_boolean(str)
  BOOLEAN_MAP.fetch(str&.downcase) do
    raise_unsupported_coercion(str, __method__)
  end
end
to_date(str) click to toggle source
# File lib/envied/coercer/envied_string.rb, line 16
def to_date(str)
  require 'date'
  ::Date.parse(str)
rescue ArgumentError
  raise_unsupported_coercion(str, __method__)
end
to_float(str) click to toggle source
# File lib/envied/coercer/envied_string.rb, line 23
def to_float(str)
  Float(str)
rescue ArgumentError
  raise_unsupported_coercion(str, __method__)
end
to_hash(str) click to toggle source
# File lib/envied/coercer/envied_string.rb, line 29
def to_hash(str)
  require 'cgi'
  ::CGI.parse(str).map { |key, values| [key, values[0]] }.to_h
end
to_integer(str) click to toggle source
# File lib/envied/coercer/envied_string.rb, line 58
def to_integer(str)
  Integer(str)
rescue ArgumentError
  raise_unsupported_coercion(str, __method__)
end
to_string(str) click to toggle source
# File lib/envied/coercer/envied_string.rb, line 34
def to_string(str)
  if str.respond_to?(:to_str)
    str.public_send(:to_str)
  else
    raise_unsupported_coercion(str, __method__)
  end
end
to_symbol(str) click to toggle source
# File lib/envied/coercer/envied_string.rb, line 42
def to_symbol(str)
  str.to_sym
end
to_time(str) click to toggle source
# File lib/envied/coercer/envied_string.rb, line 46
def to_time(str)
  require 'time'
  ::Time.parse(str)
rescue ArgumentError
  raise_unsupported_coercion(str, __method__)
end
to_uri(str) click to toggle source
# File lib/envied/coercer/envied_string.rb, line 53
def to_uri(str)
  require 'uri'
  ::URI.parse(str)
end

Private Instance Methods

raise_unsupported_coercion(value, method) click to toggle source
# File lib/envied/coercer/envied_string.rb, line 66
def raise_unsupported_coercion(value, method)
  raise(
    ENVied::Coercer::UnsupportedCoercion,
    "#{self.class}##{method} doesn't know how to coerce #{value.inspect}"
  )
end