Sha256: f7f27a1c192de974cab561df7827e9dcdec9ef878b9153a64932b6b2551651a7

Contents?: true

Size: 1.04 KB

Versions: 9

Compression:

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

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
study_line-0.2.7 vendor/bundle/ruby/3.2.0/gems/dotenv-2.8.1/lib/dotenv/substitutions/variable.rb
study_line-0.2.6 vendor/bundle/ruby/3.2.0/gems/dotenv-2.8.1/lib/dotenv/substitutions/variable.rb
study_line-0.2.5 vendor/bundle/ruby/3.2.0/gems/dotenv-2.8.1/lib/dotenv/substitutions/variable.rb
study_line-0.2.4 vendor/bundle/ruby/3.2.0/gems/dotenv-2.8.1/lib/dotenv/substitutions/variable.rb
study_line-0.2.3 vendor/bundle/ruby/3.2.0/gems/dotenv-2.8.1/lib/dotenv/substitutions/variable.rb
study_line-0.2.2 vendor/bundle/ruby/3.2.0/gems/dotenv-2.8.1/lib/dotenv/substitutions/variable.rb
study_line-0.2.1 vendor/bundle/ruby/3.2.0/gems/dotenv-2.8.1/lib/dotenv/substitutions/variable.rb
study_line-0.2.0 vendor/bundle/ruby/3.2.0/gems/dotenv-2.8.1/lib/dotenv/substitutions/variable.rb
dotenv-2.8.1 lib/dotenv/substitutions/variable.rb