Sha256: 2919486b097ee6c3a85e06ea20c93dbaa79880a6e9cfd08f5322f776b843b9c4

Contents?: true

Size: 1.17 KB

Versions: 11

Compression:

Stored size: 1.17 KB

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 or curly braces
    ARRAY_RXP = /\A[^'"{].*\s*,\s*.*[^'"}]\z/

    class << self
      def call(val)
        return val unless val.is_a?(::Hash) || val.is_a?(::String)

        case val
        when Hash
          val.transform_values { call(_1) }
        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

      def cast_hash(obj)
        obj.transform_values do |val|
          val.is_a?(::Hash) ? cast_hash(val) : call(val)
        end
      end

      def coerce(_key, val)
        call(val)
      end
    end
  end

  module NoCast
    def self.call(val) ;  val; end

    def self.coerce(_key, val) ;  val; end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
anyway_config-2.5.4 lib/.rbnext/3.0/anyway/auto_cast.rb
anyway_config-2.5.3 lib/.rbnext/3.0/anyway/auto_cast.rb
anyway_config-2.5.2 lib/.rbnext/3.0/anyway/auto_cast.rb
anyway_config-2.5.1 lib/.rbnext/3.0/anyway/auto_cast.rb
anyway_config-2.5.0 lib/.rbnext/3.0/anyway/auto_cast.rb
anyway_config-2.4.2 lib/.rbnext/3.0/anyway/auto_cast.rb
anyway_config-2.4.1 lib/.rbnext/3.0/anyway/auto_cast.rb
anyway_config-2.4.0 lib/.rbnext/3.0/anyway/auto_cast.rb
anyway_config-2.3.1 lib/.rbnext/3.0/anyway/auto_cast.rb
anyway_config-2.3.0 lib/.rbnext/3.0/anyway/auto_cast.rb
anyway_config-2.2.3 lib/.rbnext/3.0/anyway/auto_cast.rb