openSUSE/open-build-service

View on GitHub
src/api/app/components/diff_component.rb

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
# frozen_string_literal: true

class DiffComponent < ApplicationComponent
  attr_reader :diff, :file_index, :commentable, :commented_lines, :range, :source_file, :target_file

  def initialize(diff:, file_index: nil, commentable: nil, commented_lines: [], range: (0..), source_file: nil, target_file: nil)
    super
    @diff = parse_diff(diff)
    @file_index = file_index
    @commentable = commentable
    @commented_lines = commented_lines
    @range = range
    @source_file = source_file
    @target_file = target_file
  end

  def render?
    diff.present?
  end

  def lines
    return [] unless diff.lines

    diff.lines[range] || []
  end

  private

  def parse_diff(content)
    DiffParser.new(content: content).call
  end
end