Class: Xing::SpecDoc::DocFamily

Inherits:
Object
  • Object
show all
Defined in:
lib/xing/specdoc/doc-family.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ DocFamily

Returns a new instance of DocFamily.



8
9
10
11
12
13
# File 'lib/xing/specdoc/doc-family.rb', line 8

def initialize(name)
  @name = name
  @docs = []
  @index = 1
  @out_stream = $stdout
end

Instance Attribute Details

#docsObject (readonly)

Returns the value of attribute docs.



15
16
17
# File 'lib/xing/specdoc/doc-family.rb', line 15

def docs
  @docs
end

#nameObject (readonly)

Returns the value of attribute name.



15
16
17
# File 'lib/xing/specdoc/doc-family.rb', line 15

def name
  @name
end

#out_streamObject

Returns the value of attribute out_stream.



16
17
18
# File 'lib/xing/specdoc/doc-family.rb', line 16

def out_stream
  @out_stream
end

Instance Method Details

#add(doc) ⇒ Object



18
19
20
# File 'lib/xing/specdoc/doc-family.rb', line 18

def add(doc)
  @docs << doc
end

#dir_glob(pattern) ⇒ Object



79
80
81
# File 'lib/xing/specdoc/doc-family.rb', line 79

def dir_glob(pattern)
  Dir.glob(pattern)
end

#ensure_target_dirObject



64
65
66
# File 'lib/xing/specdoc/doc-family.rb', line 64

def ensure_target_dir
  FileUtils.mkdir_p(File.dirname(File::join(Xing::SpecDoc.response_target_dir, name)))
end

#exist?(path) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/xing/specdoc/doc-family.rb', line 83

def exist?(path)
  File.exist?(path)
end

#kill(path) ⇒ Object



68
69
70
71
# File 'lib/xing/specdoc/doc-family.rb', line 68

def kill(path)
  @out_stream.puts "Removing outdated JSON example at #{path}"
  FileUtils::rm_f(path)
end

#next_output_pathObject



53
54
55
56
57
58
59
60
61
62
# File 'lib/xing/specdoc/doc-family.rb', line 53

def next_output_path
  loop do
    candidate = File::join(Xing::SpecDoc.response_target_dir, "#{name}-#{@index}.json")
    if exist?(candidate)
      @index += 1
    else
      return candidate
    end
  end
end

#old_doc_globObject



22
23
24
# File 'lib/xing/specdoc/doc-family.rb', line 22

def old_doc_glob
  File::join(Xing::SpecDoc.response_target_dir, name) + "*"
end

#old_doc_pathsObject



26
27
28
29
30
31
# File 'lib/xing/specdoc/doc-family.rb', line 26

def old_doc_paths
  @old_doc_paths ||= dir_glob(old_doc_glob).find_all do |path|
    re = /#{Regexp.escape(name)}-\d+\.json$/
    path =~ re
  end
end

#old_docsObject



33
34
35
36
37
# File 'lib/xing/specdoc/doc-family.rb', line 33

def old_docs
  old_doc_paths.map do |path|
    Document.new(path, File.read(path))
  end
end

#read(path) ⇒ Object



87
88
89
# File 'lib/xing/specdoc/doc-family.rb', line 87

def read(path)
  File.read(path)
end

#recordObject



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/xing/specdoc/doc-family.rb', line 39

def record
  ensure_target_dir

  winnower = Winnower.new(docs, old_docs)

  winnower.obsolete_paths.each do |path|
    kill(path)
  end

  winnower.kept_new.each do |doc|
    write(doc)
  end
end

#write(doc) ⇒ Object



73
74
75
76
77
# File 'lib/xing/specdoc/doc-family.rb', line 73

def write(doc)
  path = next_output_path
  @out_stream.puts "Writing new JSON example to       #{path}"
  File.write(path, doc.pretty_body)
end