autodoc 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 17b75404749690142753e26acbf21f0a6de60e55
4
- data.tar.gz: f80c464a81660e905980d3d7320e13f456bc23ab
3
+ metadata.gz: 8b183416b270d62f730b447ecf874adbe4a98a3c
4
+ data.tar.gz: f97bce1b7f34b7cd873027f161c8dbe03fb881fa
5
5
  SHA512:
6
- metadata.gz: a73209f5f781748ec290b6cbb3a440011badce544d33168ad88853d3ded1c3d440047a00939abb745b4afac35949e3ebb0fb60a1727b534964edddb6ff5151bb
7
- data.tar.gz: 0d9b83795d1b0c116804ef78794d9a5de0081cac9f1a6d40cc736be125d8a0fc1637a5854f2cf4f70fd9912fc76fd0429d647ef9f6e3c01dc3472804874597b1
6
+ metadata.gz: 1d82ae6802bd524f235ac12354426c7938ff6cb03984f6fd7e0b21433b851368b6cb41b0cdf49a3cba9b18c96bac9b83aa03b5db6fd45b03f615dfc4b783fb50
7
+ data.tar.gz: 6025d431e657cc03a59f80875c1fbaf2b797e5ef0faa5f2c7c60f21a4f75ea2924b29fab7fdfef991e5d20cc209905ef2af35f31b3876e1d574300397d401978
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 0.2.5
2
+ * add ToC generator
3
+ * use Autodoc.configuration.path to change documents path
4
+
1
5
  ## 0.2.4
2
6
  * add Documents class
3
7
 
data/README.md CHANGED
@@ -7,12 +7,7 @@ gem "autodoc", group: :test
7
7
  ```
8
8
 
9
9
  ## Usage
10
- All request-specs tagged with `:autodoc` will be auto-documented.
11
- You must include a HTTP method and request path in the example description.
12
-
13
- ```
14
- $ AUTODOC=1 rspec
15
- ```
10
+ All request-specs tagged with `:autodoc` will be auto-documented.
16
11
 
17
12
  ```ruby
18
13
  # spec/requests/recipes_spec.rb
@@ -30,67 +25,18 @@ describe "Recipes" do
30
25
  end
31
26
  ```
32
27
 
33
- and the following document is generated in [doc/recipes.md](https://github.com/r7kamura/autodoc/blob/master/spec/dummy/doc/recipes.md).
28
+ Run rspec with AUTODOC=1 to generate documents. ([example](https://github.com/r7kamura/autodoc/blob/master/spec/dummy/doc/recipes.md))
34
29
 
35
- <pre>
36
- ## POST /recipes
37
- Creates a new recipe
38
-
39
- ```
40
- POST /recipes
30
+ ```sh
31
+ # shell-command
32
+ AUTODOC=1 rspec
41
33
  ```
42
34
 
43
- ### parameters
44
- * `name` string (required)
45
- * `type` integer
46
-
47
- ### response
48
- ```
49
- Status: 201
50
- location: http://www.example.com/recipes/1
51
- response:
52
- {
53
- "created_at" => "2013-06-07T08:28:35Z",
54
- "id" => 1,
55
- "name" => "alice",
56
- "type" => 1,
57
- "updated_at" => "2013-06-07T08:28:35Z"
58
- }
59
- ```
60
- </pre>
61
-
62
- ## Custom template
63
- You can customize autodoc's template ([Example](https://github.com/r7kamura/autodoc/blob/master/lib/autodoc/configuration.rb#L18-L33)).
64
- Here are avilable variables:
65
-
66
- * description
67
- * example
68
- * method
69
- * parameters_section
70
- * path
71
- * request
72
- * request_body_section
73
- * response
74
- * response_body
75
- * response_headers
76
- * response_status
77
-
78
- ### Example
79
- <pre>
80
- Autodoc.configuration.template = &lt;&lt;-EOF
81
- ## &lt;%= method %&gt; &lt;%= path %&gt;
82
- &lt;%= description %&gt;
83
- &lt;%= parameters_section %&gt;
84
- ### request
85
- ```
86
- &lt;%= method %&gt; &lt;%= path %&gt;
87
- ```
88
- &lt;%= request_body_section %&gt;
89
- ### response
90
- ```ruby
91
- Status: &lt;%= response_status %&gt;&lt;%= response_headers %&gt;
92
- response: &lt;%= response_body %&gt;
93
- ```
35
+ ### Configuration
36
+ You can configure `Autodoc.configuration` to change its behavior:
94
37
 
95
- EOF
96
- </pre>
38
+ * path - [String] location to put files (default: ./doc)
39
+ * headers - [Array] keys of documented response header (default: ["Location"])
40
+ * template - [String] ERB template for each document (default: [document.md.erb](https://github.com/r7kamura/autodoc/blob/master/lib/autodoc/templates/document.md.erb))
41
+ * toc_template - [String] ERB template for ToC (default: [toc.md.erb](https://github.com/r7kamura/autodoc/blob/master/lib/autodoc/templates/toc.md.erb))
42
+ * toc - [Boolean] whether to generate toc.md (default: false)
@@ -1,21 +1,37 @@
1
1
  module Autodoc
2
2
  class Configuration
3
- attr_writer :headers, :base_path, :template
3
+ class << self
4
+ def property(name, &default)
5
+ define_method(name) do
6
+ if instance_variable_defined?("@#{name}")
7
+ instance_variable_get("@#{name}")
8
+ else
9
+ instance_variable_set("@#{name}", instance_exec(&default))
10
+ end
11
+ end
4
12
 
5
- def headers
6
- @headers ||= %w[Location]
13
+ attr_writer name
14
+ end
7
15
  end
8
16
 
9
- def base_path
10
- @base_path ||= begin
11
- path = Rails.root.join("doc")
12
- path += ENV["AUTODOC"] if ENV["AUTODOC"] != "1"
13
- path
14
- end
17
+ property :path do
18
+ "doc"
19
+ end
20
+
21
+ property :headers do
22
+ %w[Location]
23
+ end
24
+
25
+ property :template do
26
+ File.read(File.expand_path("../templates/document.md.erb", __FILE__))
27
+ end
28
+
29
+ property :toc_template do
30
+ File.read(File.expand_path("../templates/toc.md.erb", __FILE__))
15
31
  end
16
32
 
17
- def template
18
- @template ||= File.read(File.expand_path("../template.md.erb", __FILE__))
33
+ property :toc do
34
+ false
19
35
  end
20
36
  end
21
37
  end
@@ -1,5 +1,6 @@
1
1
  require "action_dispatch/http/request"
2
2
  require "erb"
3
+ require "pathname"
3
4
 
4
5
  module Autodoc
5
6
  class Document
@@ -11,10 +12,25 @@ module Autodoc
11
12
  @context = context
12
13
  end
13
14
 
15
+ def pathname
16
+ @path ||= begin
17
+ payload = @context.example.file_path.gsub(%r<\./spec/requests/(.+)_spec\.rb>, '\1.md')
18
+ Pathname.new(Autodoc.configuration.path) + payload
19
+ end
20
+ end
21
+
14
22
  def render
15
23
  ERB.new(Autodoc.configuration.template, nil, "-").result(binding)
16
24
  end
17
25
 
26
+ def title
27
+ "#{method} #{path}"
28
+ end
29
+
30
+ def identifier
31
+ title.gsub(" ", "-").gsub(/[:_\/]/, "").downcase
32
+ end
33
+
18
34
  private
19
35
 
20
36
  def request
@@ -1,3 +1,5 @@
1
+ require "pathname"
2
+
1
3
  module Autodoc
2
4
  class Documents
3
5
  def initialize
@@ -5,16 +7,34 @@ module Autodoc
5
7
  end
6
8
 
7
9
  def append(context)
8
- path = context.example.file_path.gsub(%r<\./spec/requests/(.+)_spec\.rb>, '\1.md')
9
- key = Autodoc.configuration.base_path + path
10
- @table[key] << Autodoc::Document.render(context)
10
+ document = Autodoc::Document.new(context.clone)
11
+ @table[document.pathname] << document
11
12
  end
12
13
 
13
14
  def write
15
+ write_toc if Autodoc.configuration.toc
16
+ write_documents
17
+ end
18
+
19
+ private
20
+
21
+ def write_documents
14
22
  @table.each do |pathname, documents|
15
23
  pathname.parent.mkpath
16
- pathname.open("w") {|file| file << documents.join("\n").rstrip + "\n" }
24
+ pathname.open("w") {|file| file << documents.map(&:render).join("\n").rstrip + "\n" }
17
25
  end
18
26
  end
27
+
28
+ def write_toc
29
+ toc_path.open("w") {|file| file << render_toc }
30
+ end
31
+
32
+ def render_toc
33
+ ERB.new(Autodoc.configuration.toc_template, nil, "-").result(binding)
34
+ end
35
+
36
+ def toc_path
37
+ Pathname.new(Autodoc.configuration.path) + "toc.md"
38
+ end
19
39
  end
20
40
  end
@@ -1,5 +1,5 @@
1
1
  <%# coding: UTF-8 -%>
2
- ## <%= method %> <%= path %>
2
+ ## <%= title %>
3
3
  <%= description %>
4
4
  <%= parameters_section %>
5
5
  ### request
@@ -0,0 +1,11 @@
1
+ <%# coding: UTF-8 -%>
2
+ ## Table of Contents
3
+ <% @table.each do |pathname, documents| -%>
4
+ * [<%= pathname.basename %>](<%= pathname.basename %>)
5
+ <% documents.group_by(&:title).each do |title, documents| -%>
6
+ <% documents.each_with_index do |document, index| -%>
7
+ <% suffix = index == 0 ? "" : "-#{index}" -%>
8
+ * [<%= title %>](<%= "#{pathname.basename}##{document.identifier}#{suffix}" %>)
9
+ <% end -%>
10
+ <% end -%>
11
+ <% end -%>
@@ -1,3 +1,3 @@
1
1
  module Autodoc
2
- VERSION = "0.2.4"
2
+ VERSION = "0.2.5"
3
3
  end
@@ -23,12 +23,11 @@ response:
23
23
  "id": 1,
24
24
  "name": "name",
25
25
  "type": 1,
26
- "created_at": "2013-11-30T13:59:21.061Z",
27
- "updated_at": "2013-11-30T13:59:21.061Z"
26
+ "created_at": "2013-11-30T18:15:08.928Z",
27
+ "updated_at": "2013-11-30T18:15:08.928Z"
28
28
  }
29
29
  ```
30
30
 
31
-
32
31
  ## POST /recipes
33
32
  Creates a new recipe.
34
33
 
@@ -54,7 +53,7 @@ response:
54
53
  "id": 1,
55
54
  "name": "name",
56
55
  "type": 1,
57
- "created_at": "2013-11-30T13:59:21.072Z",
58
- "updated_at": "2013-11-30T13:59:21.072Z"
56
+ "created_at": "2013-11-30T18:15:08.938Z",
57
+ "updated_at": "2013-11-30T18:15:08.938Z"
59
58
  }
60
59
  ```
@@ -0,0 +1,4 @@
1
+ ## Table of Contents
2
+ * [recipes.md](recipes.md)
3
+ * [POST /recipes](recipes.md#post-recipes)
4
+ * [POST /recipes](recipes.md#post-recipes-1)
data/spec/spec_helper.rb CHANGED
@@ -3,6 +3,9 @@ require File.expand_path("../../spec/dummy/config/environment", __FILE__)
3
3
  require "rspec/rails"
4
4
  require "rspec/autorun"
5
5
 
6
+ Autodoc.configuration.toc = true
7
+ Autodoc.configuration.path = "spec/dummy/doc"
8
+
6
9
  RSpec.configure do |config|
7
10
  # If you"re not using ActiveRecord, or you"d prefer not to run each of your
8
11
  # examples within a transaction, remove the following line or assign false
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: autodoc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
@@ -113,7 +113,8 @@ files:
113
113
  - lib/autodoc/document.rb
114
114
  - lib/autodoc/documents.rb
115
115
  - lib/autodoc/rspec.rb
116
- - lib/autodoc/template.md.erb
116
+ - lib/autodoc/templates/document.md.erb
117
+ - lib/autodoc/templates/toc.md.erb
117
118
  - lib/autodoc/version.rb
118
119
  - spec/dummy/Rakefile
119
120
  - spec/dummy/app/assets/javascripts/application.js
@@ -144,6 +145,7 @@ files:
144
145
  - spec/dummy/db/migrate/20130607075126_create_recipes.rb
145
146
  - spec/dummy/db/schema.rb
146
147
  - spec/dummy/doc/recipes.md
148
+ - spec/dummy/doc/toc.md
147
149
  - spec/dummy/lib/assets/.gitkeep
148
150
  - spec/dummy/log/.gitkeep
149
151
  - spec/dummy/public/404.html
@@ -207,6 +209,7 @@ test_files:
207
209
  - spec/dummy/db/migrate/20130607075126_create_recipes.rb
208
210
  - spec/dummy/db/schema.rb
209
211
  - spec/dummy/doc/recipes.md
212
+ - spec/dummy/doc/toc.md
210
213
  - spec/dummy/lib/assets/.gitkeep
211
214
  - spec/dummy/log/.gitkeep
212
215
  - spec/dummy/public/404.html