Sha256: 931a9d84b7e152f74bd4e53b34f2d3f738812f3224279438f6a27e9e7d038de4

Contents?: true

Size: 708 Bytes

Versions: 4

Compression:

Stored size: 708 Bytes

Contents

# frozen_string_literal: true

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

      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

4 entries across 4 versions & 1 rubygems

Version Path
dry-system-1.1.1 lib/dry/system/magic_comments_parser.rb
dry-system-1.1.0 lib/dry/system/magic_comments_parser.rb
dry-system-1.1.0.beta2 lib/dry/system/magic_comments_parser.rb
dry-system-1.1.0.beta1 lib/dry/system/magic_comments_parser.rb