Sha256: 13a79c696434c1af87e75a608e77a286eec3b98857b5467653615c7ebd444f86

Contents?: true

Size: 886 Bytes

Versions: 6

Compression:

Stored size: 886 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 $
          (?!\()        # shouldnt be followed by paranthesis
          \{?           # 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..-1]
            elsif match[3]
              env.fetch(match[3]) { ENV[match[3]] }
            else
              variable
            end
          end
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
dotenv-2.2.2 lib/dotenv/substitutions/variable.rb
dotenv-2.2.1 lib/dotenv/substitutions/variable.rb
dotenv-2.2.0 lib/dotenv/substitutions/variable.rb
dotenv-2.1.2 lib/dotenv/substitutions/variable.rb
dotenv-2.1.1 lib/dotenv/substitutions/variable.rb
dotenv-2.1.0 lib/dotenv/substitutions/variable.rb