Sha256: 16dc949657db0739740644feb10566237d61fb81de71698a4e67bdb3615ad31f

Contents?: true

Size: 1.68 KB

Versions: 9

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true

module Qonfig
  # @api private
  # @since 0.2.0
  module Commands::LoadFromENV::ValueConverter
    # @return [Regexp]
    #
    # @api private
    # @since 0.2.0
    INTEGER_PATTERN = /\A\d+\z/.freeze

    # @return [Regexp]
    #
    # @api private
    # @since 0.2.0
    FLOAT_PATTERN = /\A\d+\.\d+\z/.freeze

    # @return [Regexp]
    #
    # @api private
    # @since 0.2.0
    TRUE_PATTERN = /\A(t|true)\z/i.freeze

    # @return [Regexp]
    #
    # @api private
    # @since 0.2.0
    FALSE_PATTERN = /\A(f|false)\z/i.freeze

    # @return [Regexp]
    #
    # @api private
    # @since 0.2.0
    ARRAY_PATTERN = /\A[^'"].*\s*,\s*.*[^'"]\z/.freeze

    # @return [Regexp]
    #
    # @api private
    # @since 0.2.0
    QUOTED_STRING_PATTERN = /\A['"].*['"]\z/.freeze

    class << self
      # @param env_data [Hash]
      # @return [void]
      #
      # @api private
      # @since 0.2.0
      def convert_values!(env_data)
        env_data.each_pair do |key, value|
          env_data[key] = convert_value(value)
        end
      end

      private

      # @param value [Object]
      # @return [Object]
      #
      # @api private
      # @since 0.2.0
      def convert_value(value)
        return value unless value.is_a?(String)

        case value
        when INTEGER_PATTERN
          Integer(value)
        when FLOAT_PATTERN
          Float(value)
        when TRUE_PATTERN
          true
        when FALSE_PATTERN
          false
        when ARRAY_PATTERN
          value.split(/\s*,\s*/).map(&method(:convert_value))
        when QUOTED_STRING_PATTERN
          value.gsub(/(\A['"]|['"]\z)/, '')
        else
          value
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
qonfig-0.16.0 lib/qonfig/commands/load_from_env/value_converter.rb
qonfig-0.15.0 lib/qonfig/commands/load_from_env/value_converter.rb
qonfig-0.14.0 lib/qonfig/commands/load_from_env/value_converter.rb
qonfig-0.13.0 lib/qonfig/commands/load_from_env/value_converter.rb
qonfig-0.12.0 lib/qonfig/commands/load_from_env/value_converter.rb
qonfig-0.11.0 lib/qonfig/commands/load_from_env/value_converter.rb
qonfig-0.10.0 lib/qonfig/commands/load_from_env/value_converter.rb
qonfig-0.9.0 lib/qonfig/commands/load_from_env/value_converter.rb
qonfig-0.8.0 lib/qonfig/commands/load_from_env/value_converter.rb