Class: Xing::SpecDoc::Winnower

Inherits:
Object
  • Object
show all
Defined in:
lib/xing/specdoc/winnower.rb

Instance Method Summary collapse

Constructor Details

#initialize(incoming_docs, existing_docs) ⇒ Winnower

Returns a new instance of Winnower.



4
5
6
7
8
9
# File 'lib/xing/specdoc/winnower.rb', line 4

def initialize(incoming_docs, existing_docs)
  @incoming_docs = incoming_docs
  @existing_docs = existing_docs

  reset
end

Instance Method Details

#kept_newObject



11
12
13
14
15
16
# File 'lib/xing/specdoc/winnower.rb', line 11

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

#obsolete_pathsObject



18
19
20
21
22
23
# File 'lib/xing/specdoc/winnower.rb', line 18

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

#resetObject



25
26
27
28
# File 'lib/xing/specdoc/winnower.rb', line 25

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

#resolveObject



30
31
32
33
34
35
36
37
38
# File 'lib/xing/specdoc/winnower.rb', line 30

def resolve
  new_docs = significant_docs

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

  @keepable = new_docs
end

#resolve_against_existing(doc, list) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/xing/specdoc/winnower.rb', line 52

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

#significant_docsObject



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/xing/specdoc/winnower.rb', line 40

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