Sha256: e285c1e309d1f81e0b8cbcbaccbc1fdf7ddafb9f3b3e98a71bbc5cbce649ed08

Contents?: true

Size: 1.05 KB

Versions: 10

Compression:

Stored size: 1.05 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, overwrite: false)
          combined_env = overwrite ? ENV.to_h.merge(env) : env.merge(ENV)
          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..]
          elsif match[3]
            env.fetch(match[3], "")
          else
            variable
          end
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
dotenv-3.1.4 lib/dotenv/substitutions/variable.rb
dotenv-3.1.3 lib/dotenv/substitutions/variable.rb
dotenv-3.1.2 lib/dotenv/substitutions/variable.rb
dotenv-3.1.1 lib/dotenv/substitutions/variable.rb
dotenv-3.1.0 lib/dotenv/substitutions/variable.rb
dotenv-3.0.3 lib/dotenv/substitutions/variable.rb
dotenv-3.0.2 lib/dotenv/substitutions/variable.rb
dotenv-3.0.1 lib/dotenv/substitutions/variable.rb
dotenv-3.0.0 lib/dotenv/substitutions/variable.rb
dotenv-3.0.0.beta lib/dotenv/substitutions/variable.rb