Sha256: 9687fc66ed5f6ffb005a94450cbe59e5a1e7a59cbe475d9e9414072a6c423e11

Contents?: true

Size: 1.05 KB

Versions: 4

Compression:

Stored size: 1.05 KB

Contents

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 $
          (              # collect braces with var for sub
            \{?          # allow brace wrapping
            ([A-Z0-9_]+) # match the variable
            \}?          # closing brace
          )
        /xi

        def call(value, env)
          # Process embedded variables
          value.scan(VARIABLE).each do |parts|
            if parts.first == '\\'
              # Variable is escaped, don't replace it.
              replace = parts[1...-1].join('')
            else
              # Replace it with the value from the environment
              replace = env.fetch(parts.last) { ENV[parts.last] }
            end

            value = value.sub(parts[0...-1].join(''), replace || '')
          end

          value
        end
      end

    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
dotenv-2.0.0.beta lib/dotenv/substitutions/variable.rb
dotenv-1.0.2 lib/dotenv/substitutions/variable.rb
dotenv-1.0.1 lib/dotenv/substitutions/variable.rb
dotenv-1.0.0 lib/dotenv/substitutions/variable.rb