Sha256: 0b0e14459a9309e670023b51921be140c3496c64adbef53ba448d03f09acfacc

Contents?: true

Size: 884 Bytes

Versions: 3

Compression:

Stored size: 884 Bytes

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 $
          (?!\()        # shouldn't be followed by parenthesis
          \{?           # allow brace wrapping
          ([A-Z0-9_]+)? # optional alpha nums
          \}?           # closing brace
        /xi

        def call(value, env)
          value.gsub(VARIABLE) do |variable|
            match = $LAST_MATCH_INFO

            if match[1] == "\\"
              variable[1..]
            elsif match[3]
              env[match[3]] || ENV[match[3]] || ""
            else
              variable
            end
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dotenv-3.1.7 lib/dotenv/substitutions/variable.rb
dotenv-3.1.6 lib/dotenv/substitutions/variable.rb
dotenv-3.1.5 lib/dotenv/substitutions/variable.rb