Sha256: 426b3e2dcaa2be2fe577e8de151d8c0f28d8be03c52c40045e04f44be4664e16

Contents?: true

Size: 1.18 KB

Versions: 6

Compression:

Stored size: 1.18 KB

Contents

module Stash
  class PullRequest
    class Diff
      def initialize(diff)
        @diff = diff
      end

      def filenames_with_added_lines
        diffs_with_added_lines.map do |diff|
          diff['destination']['toString']
        end
      end

      def added_line?(file, line)
        added_lines(file).include?(line)
      end

      private

      def diffs_with_added_lines
        @diff['diffs'].select do |diff|
          diff['destination'] &&
          diff['hunks'].find do |hunk|
            hunk['segments'].find do |segment|
              segment['type'] == 'ADDED'
            end
          end
        end
      end

      def added_lines(file)
        @added_lines ||= {}
        @added_lines[file] ||= begin
          file_diff(file)['hunks'].map do |hunk|
            hunk['segments'].select{|segment| segment['type'] == 'ADDED' }.map do |segment|
              segment['lines'].map do |line|
                line['destination']
              end
            end
          end.flatten
        end
      end

      def file_diff(file)
        @diff['diffs'].detect{|diff| diff['destination'] && diff['destination']['toString'] == file } || {'hunks' => []}
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
face_control-0.8.4 lib/stash/pull_request/diff.rb
face_control-0.8.3 lib/stash/pull_request/diff.rb
face_control-0.8.2 lib/stash/pull_request/diff.rb
face_control-0.8.1 lib/stash/pull_request/diff.rb
face_control-0.8.0 lib/stash/pull_request/diff.rb
face_control-0.7.0 lib/stash/pull_request/diff.rb