Sha256: 6776d2c07d19077ee060266d702a064f519f621518bec1173a2c1805c71a7ee3

Contents?: true

Size: 1.12 KB

Versions: 15

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

module Anyway
  # Parses environment variables and provides
  # method-like access
  class Env
    using RubyNext
    using Anyway::Ext::DeepDup
    using Anyway::Ext::Hash

    include Tracing

    attr_reader :data, :traces, :type_cast

    def initialize(type_cast: AutoCast)
      @type_cast = type_cast
      @data = {}
      @traces = {}
    end

    def clear
      data.clear
      traces.clear
    end

    def fetch(prefix)
      return data[prefix].deep_dup if data.key?(prefix)

      Tracing.capture do
        data[prefix] = parse_env(prefix)
      end.then do |trace|
        traces[prefix] = trace
      end

      data[prefix].deep_dup
    end

    def fetch_with_trace(prefix)
      [fetch(prefix), traces[prefix]]
    end

    private

    def parse_env(prefix)
      match_prefix = "#{prefix}_"
      ENV.each_pair.with_object({}) do |(key, val), data|
        next unless key.start_with?(match_prefix)

        path = key.sub(/^#{prefix}_/, "").downcase

        paths = path.split("__")
        trace!(:env, *paths, key: key) { data.bury(type_cast.call(val), *paths) }
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
anyway_config-2.3.1 lib/.rbnext/3.1/anyway/env.rb
anyway_config-2.3.0 lib/.rbnext/3.1/anyway/env.rb
anyway_config-2.2.3 lib/.rbnext/3.1/anyway/env.rb
anyway_config-2.2.2 lib/.rbnext/1995.next/anyway/env.rb
anyway_config-2.2.1 lib/.rbnext/1995.next/anyway/env.rb
anyway_config-2.2.0 lib/.rbnext/1995.next/anyway/env.rb
anyway_config-2.1.0 lib/.rbnext/1995.next/anyway/env.rb
anyway_config-2.0.6 lib/anyway/env.rb
anyway_config-2.0.5 lib/anyway/env.rb
anyway_config-2.0.4 lib/anyway/env.rb
anyway_config-2.0.3 lib/anyway/env.rb
anyway_config-2.0.2 lib/anyway/env.rb
anyway_config-2.0.1 lib/anyway/env.rb
anyway_config-2.0.0 lib/anyway/env.rb
anyway_config-2.0.0.rc1 lib/anyway/env.rb