Skip to content

Commit ec120b2

Browse files
committed
All writers now inherit from a base writer
* the writers are now responsible for cleaning docs * added an append_json writer that does not delete docs and simply modifies index.json
1 parent d72deda commit ec120b2

14 files changed

+111
-61
lines changed

lib/rspec_api_documentation.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ module RspecApiDocumentation
2828
module Writers
2929
extend ActiveSupport::Autoload
3030

31+
autoload :Writer
3132
autoload :GeneralMarkupWriter
3233
autoload :HtmlWriter
3334
autoload :TextileWriter
3435
autoload :JsonWriter
36+
autoload :AppendJsonWriter
3537
autoload :JsonIodocsWriter
36-
autoload :IndexWriter
38+
autoload :IndexHelper
3739
autoload :CombinedTextWriter
3840
autoload :CombinedJsonWriter
3941
end

lib/rspec_api_documentation/api_documentation.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ def initialize(configuration)
1010
end
1111

1212
def clear_docs
13-
if File.exists?(docs_dir)
14-
FileUtils.rm_rf(docs_dir, :secure => true)
13+
writers.each do |writer|
14+
writer.clear_docs(docs_dir)
1515
end
16-
FileUtils.mkdir_p(docs_dir)
1716
end
1817

1918
def document_example(rspec_example)

lib/rspec_api_documentation/views/markup_index.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def api_name
1414
end
1515

1616
def sections
17-
RspecApiDocumentation::Writers::IndexWriter.sections(examples, @configuration)
17+
RspecApiDocumentation::Writers::IndexHelper.sections(examples, @configuration)
1818
end
1919
end
2020
end
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
require 'rspec_api_documentation/writers/formatter'
2+
3+
module RspecApiDocumentation
4+
module Writers
5+
class AppendJsonWriter < JsonWriter
6+
def write
7+
index_file = docs_dir.join("index.json")
8+
if File.exists?(index_file) && (output = File.read(index_file)).length >= 2
9+
existing_index_hash = JSON.parse(output)
10+
end
11+
File.open(index_file, "w+") do |f|
12+
f.write Formatter.to_json(AppendJsonIndex.new(index, configuration, existing_index_hash))
13+
end
14+
write_examples
15+
end
16+
17+
def self.clear_docs(docs_dir)
18+
nil #noop
19+
end
20+
end
21+
22+
class AppendJsonIndex < JsonIndex
23+
def initialize(index, configuration, existing_index_hash = nil)
24+
@index = index
25+
@configuration = configuration
26+
@existing_index_hash = clean_index_hash(existing_index_hash)
27+
end
28+
29+
def as_json(opts = nil)
30+
sections.inject(@existing_index_hash) do |h, section|
31+
h[:resources].push(section_hash(section))
32+
h
33+
end
34+
end
35+
36+
def clean_index_hash(existing_index_hash)
37+
unless existing_index_hash.is_a?(Hash) && existing_index_hash["resources"].is_a?(Array) #check format
38+
existing_index_hash = {:resources => []}
39+
end
40+
existing_index_hash = existing_index_hash.deep_symbolize_keys
41+
existing_index_hash[:resources].map!(&:deep_symbolize_keys).reject! do |resource|
42+
resource_names = sections.map{|s| s[:resource_name]}
43+
resource_names.include? resource[:name]
44+
end
45+
existing_index_hash
46+
end
47+
end
48+
end
49+
end

lib/rspec_api_documentation/writers/combined_json_writer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module RspecApiDocumentation
44
module Writers
5-
class CombinedJsonWriter
5+
class CombinedJsonWriter < Writer
66
def self.write(index, configuration)
77
File.open(configuration.docs_dir.join("combined.json"), "w+") do |f|
88
examples = []

lib/rspec_api_documentation/writers/combined_text_writer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module RspecApiDocumentation
22
module Writers
3-
class CombinedTextWriter
3+
class CombinedTextWriter < Writer
44
def self.write(index, configuration)
55
index.examples.each do |rspec_example|
66
example = CombinedTextExample.new(rspec_example)

lib/rspec_api_documentation/writers/general_markup_writer.rb

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,8 @@
11
module RspecApiDocumentation
22
module Writers
3-
class GeneralMarkupWriter
4-
attr_accessor :index, :configuration
5-
3+
class GeneralMarkupWriter < Writer
64
INDEX_FILE_NAME = 'index'
7-
8-
def initialize(index, configuration)
9-
self.index = index
10-
self.configuration = configuration
11-
end
125

13-
def self.write(index, configuration)
14-
writer = new(index, configuration)
15-
writer.write
16-
end
17-
186
def write
197
File.open(configuration.docs_dir.join(index_file_name + '.' + extension), "w+") do |f|
208
f.write markup_index_class.new(index, configuration).render

lib/rspec_api_documentation/writers/html_writer.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
module RspecApiDocumentation
22
module Writers
33
class HtmlWriter < GeneralMarkupWriter
4-
attr_accessor :index, :configuration
5-
64
EXTENSION = 'html'
75

86
def markup_index_class

lib/rspec_api_documentation/writers/index_writer.rb renamed to lib/rspec_api_documentation/writers/index_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module RspecApiDocumentation
44
module Writers
5-
module IndexWriter
5+
module IndexHelper
66
def sections(examples, configuration)
77
resources = examples.group_by(&:resource_name).inject([]) do |arr, (resource_name, examples)|
88
ordered_examples = configuration.keep_source_order ? examples : examples.sort_by(&:description)

lib/rspec_api_documentation/writers/json_iodocs_writer.rb

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,15 @@
22

33
module RspecApiDocumentation
44
module Writers
5-
class JsonIodocsWriter
6-
attr_accessor :index, :configuration, :api_key
5+
class JsonIodocsWriter < Writer
6+
attr_accessor :api_key
77
delegate :docs_dir, :to => :configuration
88

99
def initialize(index, configuration)
10-
self.index = index
11-
self.configuration = configuration
10+
super
1211
self.api_key = configuration.api_name.parameterize
1312
end
1413

15-
def self.write(index, configuration)
16-
writer = new(index, configuration)
17-
writer.write
18-
end
19-
2014
def write
2115
File.open(docs_dir.join("apiconfig.json"), "w+") do |file|
2216
file.write Formatter.to_json(ApiConfig.new(configuration))
@@ -34,7 +28,7 @@ def initialize(index, configuration)
3428
end
3529

3630
def sections
37-
IndexWriter.sections(examples, @configuration)
31+
IndexHelper.sections(examples, @configuration)
3832
end
3933

4034
def examples

lib/rspec_api_documentation/writers/json_writer.rb

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,17 @@
22

33
module RspecApiDocumentation
44
module Writers
5-
class JsonWriter
6-
attr_accessor :index, :configuration
5+
class JsonWriter < Writer
76
delegate :docs_dir, :to => :configuration
87

9-
def initialize(index, configuration)
10-
self.index = index
11-
self.configuration = configuration
12-
end
13-
14-
def self.write(index, configuration)
15-
writer = new(index, configuration)
16-
writer.write
17-
end
18-
198
def write
209
File.open(docs_dir.join("index.json"), "w+") do |f|
2110
f.write Formatter.to_json(JsonIndex.new(index, configuration))
2211
end
12+
write_examples
13+
end
14+
15+
def write_examples
2316
index.examples.each do |example|
2417
json_example = JsonExample.new(example, configuration)
2518
FileUtils.mkdir_p(docs_dir.join(json_example.dirname))
@@ -37,7 +30,7 @@ def initialize(index, configuration)
3730
end
3831

3932
def sections
40-
IndexWriter.sections(examples, @configuration)
33+
IndexHelper.sections(examples, @configuration)
4134
end
4235

4336
def examples
@@ -46,19 +39,23 @@ def examples
4639

4740
def as_json(opts = nil)
4841
sections.inject({:resources => []}) do |h, section|
49-
h[:resources].push(
50-
:name => section[:resource_name],
51-
:examples => section[:examples].map { |example|
52-
{
53-
:description => example.description,
54-
:link => "#{example.dirname}/#{example.filename}",
55-
:groups => example.metadata[:document]
56-
}
57-
}
58-
)
42+
h[:resources].push(section_hash(section))
5943
h
6044
end
6145
end
46+
47+
def section_hash(section)
48+
{
49+
:name => section[:resource_name],
50+
:examples => section[:examples].map { |example|
51+
{
52+
:description => example.description,
53+
:link => "#{example.dirname}/#{example.filename}",
54+
:groups => example.metadata[:document]
55+
}
56+
}
57+
}
58+
end
6259
end
6360

6461
class JsonExample

lib/rspec_api_documentation/writers/textile_writer.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
module RspecApiDocumentation
22
module Writers
33
class TextileWriter < GeneralMarkupWriter
4-
attr_accessor :index, :configuration
5-
64
EXTENSION = 'textile'
75

86
def markup_index_class
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module RspecApiDocumentation
2+
module Writers
3+
class Writer
4+
attr_accessor :index, :configuration
5+
6+
def initialize(index, configuration)
7+
self.index = index
8+
self.configuration = configuration
9+
end
10+
11+
def self.write(index, configuration)
12+
writer = new(index, configuration)
13+
writer.write
14+
end
15+
16+
def self.clear_docs(docs_dir)
17+
if File.exists?(docs_dir)
18+
FileUtils.rm_rf(docs_dir, :secure => true)
19+
end
20+
FileUtils.mkdir_p(docs_dir)
21+
end
22+
end
23+
end
24+
end
25+

spec/writers/index_writer_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'spec_helper'
22

3-
describe RspecApiDocumentation::Writers::IndexWriter do
3+
describe RspecApiDocumentation::Writers::IndexHelper do
44
describe "#sections" do
55
let(:example_1) { double(:resource_name => "Order", :description => "Updating an order") }
66
let(:example_2) { double(:resource_name => "Order", :description => "Creating an order") }
@@ -9,7 +9,7 @@
99

1010
context "with default value for keep_source_order" do
1111
let(:configuration) { RspecApiDocumentation::Configuration.new }
12-
subject { RspecApiDocumentation::Writers::IndexWriter.sections(examples, configuration) }
12+
subject { RspecApiDocumentation::Writers::IndexHelper.sections(examples, configuration) }
1313

1414
it "should order resources by resource name" do
1515
subject.map { |resource| resource[:resource_name] }.should == ["Cart", "Order"]
@@ -21,7 +21,7 @@
2121
end
2222

2323
context "with keep_source_order set to true" do
24-
subject { RspecApiDocumentation::Writers::IndexWriter.sections(examples, double(:keep_source_order => true)) }
24+
subject { RspecApiDocumentation::Writers::IndexHelper.sections(examples, double(:keep_source_order => true)) }
2525

2626
it "should order resources by source code declaration" do
2727
subject.map { |resource| resource[:resource_name] }.should == ["Order", "Cart"]

0 commit comments

Comments
 (0)