Sha256: a850b1248fb42e37419303fc4898ca787dc4d84f4d71276783519eae6da093b4

Contents?: true

Size: 1.3 KB

Versions: 7

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

7 entries across 7 versions & 1 rubygems

Version Path
dry-system-0.17.0 lib/dry/system/settings/file_parser.rb
dry-system-0.15.0 lib/dry/system/settings/file_parser.rb
dry-system-0.14.1 lib/dry/system/settings/file_parser.rb
dry-system-0.14.0 lib/dry/system/settings/file_parser.rb
dry-system-0.13.2 lib/dry/system/settings/file_parser.rb
dry-system-0.13.1 lib/dry/system/settings/file_parser.rb
dry-system-0.13.0 lib/dry/system/settings/file_parser.rb