Sha256: e46c55ae228ebd09c22a0022842270dd190357b7acf1decad07f618294931fec

Contents?: true

Size: 725 Bytes

Versions: 9

Compression:

Stored size: 725 Bytes

Contents

# frozen_string_literal: true

module Anyway
  module AutoCast
    # Regexp to detect array values
    # Array value is a values that contains at least one comma
    # and doesn't start/end with quote
    ARRAY_RXP = /\A[^'"].*\s*,\s*.*[^'"]\z/

    def self.call(val)
      return val unless String === val

      case val
      when ARRAY_RXP
        val.split(/\s*,\s*/).map { call(_1) }
      when /\A(true|t|yes|y)\z/i
        true
      when /\A(false|f|no|n)\z/i
        false
      when /\A(nil|null)\z/i
        nil
      when /\A\d+\z/
        val.to_i
      when /\A\d*\.\d+\z/
        val.to_f
      when /\A['"].*['"]\z/
        val.gsub(/(\A['"]|['"]\z)/, "")
      else
        val
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
anyway_config-2.1.0 lib/anyway/auto_cast.rb
anyway_config-2.0.6 lib/anyway/auto_cast.rb
anyway_config-2.0.5 lib/anyway/auto_cast.rb
anyway_config-2.0.4 lib/anyway/auto_cast.rb
anyway_config-2.0.3 lib/anyway/auto_cast.rb
anyway_config-2.0.2 lib/anyway/auto_cast.rb
anyway_config-2.0.1 lib/anyway/auto_cast.rb
anyway_config-2.0.0 lib/anyway/auto_cast.rb
anyway_config-2.0.0.rc1 lib/anyway/auto_cast.rb