Sha256: e9ad8e39d137b3699f1f7cfc64a00d7e66f315ec41ae41e45e0e48659b18b1a3

Contents?: true

Size: 1.15 KB

Versions: 24

Compression:

Stored size: 1.15 KB

Contents

require "English"

module Dotenv
  module Substitutions
    # Substitute variables in a value.
    #
    #   HOST=example.com
    #   URL="https://$HOST"
    #
    module Variable
      class << self
        VARIABLE = /
          (\\)?         # is it escaped with a backslash?
          (\$)          # literal $
          (?!\()        # shouldnt be followed by paranthesis
          \{?           # allow brace wrapping
          ([A-Z0-9_]+)? # optional alpha nums
          \}?           # closing brace
        /xi

        def call(value, env, is_load)
          combined_env = if is_load
                           env.merge(ENV)
                         else
                           ENV.to_h.merge(env)
                         end
          value.gsub(VARIABLE) do |variable|
            match = $LAST_MATCH_INFO
            substitute(match, variable, combined_env)
          end
        end

        private

        def substitute(match, variable, env)
          if match[1] == '\\'
            variable[1..-1]
          elsif match[3]
            env.fetch(match[3], "")
          else
            variable
          end
        end
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 5 rubygems

Version Path
avalara_sdk-24.12.2 vendor/bundle/ruby/2.7.0/gems/dotenv-2.7.6/lib/dotenv/substitutions/variable.rb
avalara_sdk-24.12.1 vendor/bundle/ruby/2.7.0/gems/dotenv-2.7.6/lib/dotenv/substitutions/variable.rb
avalara_sdk-24.12.0 vendor/bundle/ruby/2.7.0/gems/dotenv-2.7.6/lib/dotenv/substitutions/variable.rb
avalara_sdk-24.2.29 vendor/bundle/ruby/2.7.0/gems/dotenv-2.7.6/lib/dotenv/substitutions/variable.rb
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/dotenv-2.7.6/lib/dotenv/substitutions/variable.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/dotenv-2.7.6/lib/dotenv/substitutions/variable.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/dotenv-2.7.6/lib/dotenv/substitutions/variable.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/dotenv-2.7.6/lib/dotenv/substitutions/variable.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/dotenv-2.7.6/lib/dotenv/substitutions/variable.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/dotenv-2.7.6/lib/dotenv/substitutions/variable.rb
dotenv-2.7.6 lib/dotenv/substitutions/variable.rb
files.com-1.0.55 docs/vendor/bundle/ruby/2.5.0/gems/dotenv-2.7.0/lib/dotenv/substitutions/variable.rb
chatops-rpc-0.0.2 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/dotenv-2.7.5/lib/dotenv/substitutions/variable.rb
chatops-rpc-0.0.1 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/dotenv-2.7.5/lib/dotenv/substitutions/variable.rb
dotenv-2.7.5 lib/dotenv/substitutions/variable.rb
dotenv-2.7.4 lib/dotenv/substitutions/variable.rb
dotenv-2.7.3 lib/dotenv/substitutions/variable.rb
dotenv-2.7.2 lib/dotenv/substitutions/variable.rb
dotenv-2.7.1 lib/dotenv/substitutions/variable.rb
dotenv-2.7.0 lib/dotenv/substitutions/variable.rb