Sha256: d7e312b64b7cd242bf34df7260952a9020954250e3b325a28369e42e77044276

Contents?: true

Size: 722 Bytes

Versions: 7

Compression:

Stored size: 722 Bytes

Contents

# frozen_string_literal: true

module Dry
  module System
    class MagicCommentsParser
      VALID_LINE_RE = /^(#.*)?$/.freeze
      COMMENT_RE = /^#\s+(?<name>[A-Za-z]{1}[A-Za-z0-9_]+):\s+(?<value>.+?)$/.freeze

      COERCIONS = {
        'true' => true,
        'false' => false
      }.freeze

      def self.call(file_name)
        {}.tap do |options|
          File.foreach(file_name) do |line|
            break unless line =~ VALID_LINE_RE

            if (comment = line.match(COMMENT_RE))
              options[comment[:name].to_sym] = coerce(comment[:value])
            end
          end
        end
      end

      def self.coerce(value)
        COERCIONS.fetch(value) { value }
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

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