Sha256: 573c99238a7c11ebfb1a918d4827aed951a176f64a8e753bd5b512c9238c0ada

Contents?: true

Size: 1.53 KB

Versions: 2

Compression:

Stored size: 1.53 KB

Contents

class ENVied::Coercer::ENViedString
  TRUE_VALUES = %w[1 on t true y yes].freeze
  FALSE_VALUES = %w[0 off f false n no].freeze
  BOOLEAN_MAP = (TRUE_VALUES.product([ true ]) + FALSE_VALUES.product([ false ])).to_h.freeze

  def to_array(str)
    str.split(/(?<!\\),/).map{|i| i.gsub(/\\,/,',') }
  end

  def to_boolean(str)
    BOOLEAN_MAP.fetch(str&.downcase) do
      raise_unsupported_coercion(str, __method__)
    end
  end

  def to_date(str)
    require 'date'
    ::Date.parse(str)
  rescue ArgumentError
    raise_unsupported_coercion(str, __method__)
  end

  def to_float(str)
    Float(str)
  rescue ArgumentError
    raise_unsupported_coercion(str, __method__)
  end

  def to_hash(str)
    require 'cgi'
    ::CGI.parse(str).map { |key, values| [key, values[0]] }.to_h
  end

  def to_string(str)
    if str.respond_to?(:to_str)
      str.public_send(:to_str)
    else
      raise_unsupported_coercion(str, __method__)
    end
  end

  def to_env(string)
    to_string(string)
  end

  def to_symbol(str)
    str.to_sym
  end

  def to_time(str)
    require 'time'
    ::Time.parse(str)
  rescue ArgumentError
    raise_unsupported_coercion(str, __method__)
  end

  def to_uri(str)
    require 'uri'
    ::URI.parse(str)
  end

  def to_integer(str)
    Integer(str)
  rescue ArgumentError
    raise_unsupported_coercion(str, __method__)
  end

  private

  def raise_unsupported_coercion(value, method)
    raise(
      ENVied::Coercer::UnsupportedCoercion,
      "#{self.class}##{method} doesn't know how to coerce #{value.inspect}"
    )
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
envied-0.10.0.alpha3 lib/envied/coercer/envied_string.rb
envied-0.10.0.alpha1 lib/envied/coercer/envied_string.rb