Sha256: e559a49be6f489d40f4dbe8442cea69202422dcdbffca04cd7ea27aaa03fef00

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

module Xing
  module SpecDoc
    class Winnower
      def initialize(incoming_docs, existing_docs)
        @incoming_docs = incoming_docs
        @existing_docs = existing_docs

        reset
      end

      def kept_new
        if @keepable.nil?
          resolve
        end
        @keepable
      end

      def obsolete_paths
        if @keepable.nil?
          resolve
        end
        @obsolete_paths
      end

      def reset
        @obsolete_paths = []
        @keepable = nil
      end

      def resolve
        new_docs = significant_docs

        @existing_docs.each do |doc|
          new_docs = resolve_against_existing(doc, new_docs)
        end

        @keepable = new_docs
      end

      def significant_docs
        @incoming_docs.reduce([]) do |list, doc|
          if list.any?{|kept| not kept.different_from?(doc.parsed_body)}
            list
          else
            list + [doc]
          end
        end
      end

      def resolve_against_existing(doc, list)
        old_doc = JSON.parse(doc.contents)

        keep, trash = list.partition do |doc|
          doc.different_from?(old_doc)
        end

        if trash.length == 0
          @obsolete_paths << doc.path
        end

        return keep
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
xing-backend-specdoc-0.0.1 lib/xing/specdoc/winnower.rb