Sha256: 109572dba6d68b592706cc4f6547df2c52bee88f69c6d804ce20e56f32abd352

Contents?: true

Size: 896 Bytes

Versions: 2

Compression:

Stored size: 896 Bytes

Contents

require_relative 'base_parser'

module LintTrap
  module Parsers
    # Handles parsing line by line with regex
    class LineParser < BaseParser
      def parse
        @io.each_line do |line|
          next unless (violation = parse_line(line))

          yield violation
        end
      end

    private

      def parse_line(line)
        return unless (match = line.match(violation_regex))

        violation_fields.each_with_object({}) do |field, violation|
          violation[field.to_sym] = if match.names.include?(field) && !match[field].empty?
            match[field]
          else
            nil
          end
        end
      end

      def violation_fields
        %w(file line column length rule severity message)
      end

      def violation_regex
        raise NotImplementedError, "Subclass #{self.class.name} must implement violation_regex."
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lint_trap-0.0.2 lib/lint_trap/parsers/line_parser.rb
lint_trap-0.0.1 lib/lint_trap/parsers/line_parser.rb