File: variable.rb

package info (click to toggle)
ruby-dotenv 2.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 244 kB
  • ctags: 56
  • sloc: ruby: 664; makefile: 9
file content (34 lines) | stat: -rw-r--r-- 768 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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 $
          \{?          # allow brace wrapping
          ([A-Z0-9_]+) # match the variable
          \}?          # closing brace
        /xi

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

            if match[1] == '\\'
              variable[1..-1]
            else
              env.fetch(match[3]) { ENV[match[3]] }
            end
          end
        end
      end
    end
  end
end