Sha256: 03444c5fae117b153a3e0ff4a8690802b26114d1b778748570d85273ddfb30a7

Contents?: true

Size: 1.39 KB

Versions: 2

Compression:

Stored size: 1.39 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 doc.contents.empty?
            list
          elsif list.any?{|kept| not kept.different_from?(doc.parsed_body)}
            list
          else
            list + [doc]
          end
        end
      end

      def resolve_against_existing(doc, list)
        if doc.contents.empty?
          @obsolete_paths << doc.path
          return list
        end

        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

2 entries across 2 versions & 1 rubygems

Version Path
xing-backend-specdoc-1.0.0.pre.beta lib/xing/specdoc/winnower.rb
xing-backend-specdoc-0.0.2 lib/xing/specdoc/winnower.rb