Sha256: 4f924c20fecc64ea25f2c902ae3308793f3dcf2591345e4e0fe7594d1fe8bafd

Contents?: true

Size: 1.3 KB

Versions: 9

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

module Dry
  module System
    module Settings
      class FileParser
        # Regex extracted from dotenv gem
        # https://github.com/bkeepers/dotenv/blob/master/lib/dotenv/parser.rb#L14
        LINE = /
          \A
          \s*
          (?:export\s+)?    # optional export
          ([\w\.]+)         # key
          (?:\s*=\s*|:\s+?) # separator
          (                 # optional value begin
            '(?:\'|[^'])*'  #   single quoted value
            |               #   or
            "(?:\"|[^"])*"  #   double quoted value
            |               #   or
            [^#\n]+         #   unquoted value
          )?                # value end
          \s*
          (?:\#.*)?         # optional comment
          \z
        /x.freeze

        def call(file)
          File.readlines(file).each_with_object({}) do |line, hash|
            parse_line(line, hash)
          end
        rescue Errno::ENOENT
          {}
        end

        private

        def parse_line(line, hash)
          if (match = line.match(LINE))
            key, value = match.captures
            hash[key] = parse_value(value || "")
          end
          hash
        end

        def parse_value(value)
          value.strip.sub(/\A(['"])(.*)\1\z/, '\2')
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
dry-system-0.22.0 lib/dry/system/settings/file_parser.rb
dry-system-0.21.0 lib/dry/system/settings/file_parser.rb
dry-system-0.20.0 lib/dry/system/settings/file_parser.rb
dry-system-0.19.2 lib/dry/system/settings/file_parser.rb
dry-system-0.18.2 lib/dry/system/settings/file_parser.rb
dry-system-0.19.1 lib/dry/system/settings/file_parser.rb
dry-system-0.19.0 lib/dry/system/settings/file_parser.rb
dry-system-0.18.1 lib/dry/system/settings/file_parser.rb
dry-system-0.18.0 lib/dry/system/settings/file_parser.rb