Class: Shaf::ApiDoc::Document
- Inherits:
-
Object
- Object
- Shaf::ApiDoc::Document
- Defined in:
- lib/shaf/api_doc/document.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#curies ⇒ Object
Returns the value of attribute curies.
-
#embeds ⇒ Object
Returns the value of attribute embeds.
-
#links ⇒ Object
Returns the value of attribute links.
- #model ⇒ Object
-
#policy ⇒ Object
Returns the value of attribute policy.
-
#serializer_class ⇒ Object
Returns the value of attribute serializer_class.
Instance Method Summary collapse
- #attribute(attr, comment) ⇒ Object
- #curie(rel, comment) ⇒ Object
- #embedded(name, comment) ⇒ Object
- #generate_markdown! ⇒ Object
- #generate_yaml! ⇒ Object
- #has_enough_info? ⇒ Boolean
-
#initialize ⇒ Document
constructor
A new instance of Document.
- #link(rel, comment) ⇒ Object
- #to_markdown ⇒ Object
- #write_html(output) ⇒ Object
- #write_yaml(output) ⇒ Object
Constructor Details
#initialize ⇒ Document
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/shaf/api_doc/document.rb', line 13 def initialize @model = nil @serializer_class = nil @policy = nil @attributes = {} @links = {} @curies = {} = {} @md = {} end |
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
11 12 13 |
# File 'lib/shaf/api_doc/document.rb', line 11 def attributes @attributes end |
#curies ⇒ Object
Returns the value of attribute curies.
11 12 13 |
# File 'lib/shaf/api_doc/document.rb', line 11 def curies @curies end |
#embeds ⇒ Object
Returns the value of attribute embeds.
11 12 13 |
# File 'lib/shaf/api_doc/document.rb', line 11 def end |
#links ⇒ Object
Returns the value of attribute links.
11 12 13 |
# File 'lib/shaf/api_doc/document.rb', line 11 def links @links end |
#model ⇒ Object
24 25 26 |
# File 'lib/shaf/api_doc/document.rb', line 24 def model @model || @serializer_class && @serializer_class.sub("Serializer", "") end |
#policy ⇒ Object
Returns the value of attribute policy.
11 12 13 |
# File 'lib/shaf/api_doc/document.rb', line 11 def policy @policy end |
#serializer_class ⇒ Object
Returns the value of attribute serializer_class.
11 12 13 |
# File 'lib/shaf/api_doc/document.rb', line 11 def serializer_class @serializer_class end |
Instance Method Details
#attribute(attr, comment) ⇒ Object
28 29 30 |
# File 'lib/shaf/api_doc/document.rb', line 28 def attribute(attr, comment) @attributes[attr] = comment unless comment.empty? end |
#curie(rel, comment) ⇒ Object
36 37 38 |
# File 'lib/shaf/api_doc/document.rb', line 36 def curie(rel, comment) @curies[rel] = comment unless comment.empty? end |
#embedded(name, comment) ⇒ Object
40 41 42 |
# File 'lib/shaf/api_doc/document.rb', line 40 def (name, comment) [strip_curie(name)] = comment unless comment.empty? end |
#generate_markdown! ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/shaf/api_doc/document.rb', line 49 def generate_markdown! return @md unless @md.empty? generate_title! generate_policy! generate_section!(key: :attributes, heading: "Attributes") generate_section!(key: :curies, heading: "Curies", sub_title: "rel") generate_section!(key: :links, heading: "Links", sub_title: "rel") generate_section!(key: :embeds, heading: "Embedded resources", sub_title: "rel") @md[:doc] end |
#generate_yaml! ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/shaf/api_doc/document.rb', line 61 def generate_yaml! generate_markdown! renderer = Redcarpet::Markdown.new(Redcarpet::Render::StripDown) hash = {} hash['policy'] = renderer.render(@md[:policy]).chomp if @md[:policy] [:attributes, :curies, :links, :embeds].each do |key| hash[key.to_s] = @md[key].map { |k, v| [k.to_s, renderer.render(v).chomp] }.to_h end hash.to_yaml end |
#has_enough_info? ⇒ Boolean
44 45 46 47 |
# File 'lib/shaf/api_doc/document.rb', line 44 def has_enough_info? return false unless model attributes.merge(links).merge(curies).any? end |
#link(rel, comment) ⇒ Object
32 33 34 |
# File 'lib/shaf/api_doc/document.rb', line 32 def link(rel, comment) @links[strip_curie(rel)] = comment unless comment.empty? end |
#to_markdown ⇒ Object
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/shaf/api_doc/document.rb', line 74 def to_markdown # For some reason redcarpet don't like to surround my markdown code blocks # with <pre> tags, so let's fix that here. = {autolink: true, fenced_code_blocks: true} markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, ) html = markdown.render(generate_markdown!) html.gsub!("<code>", "<pre><code>") html.gsub!("</code>", "</code></pre>") html end |
#write_html(output) ⇒ Object
85 86 87 88 89 90 |
# File 'lib/shaf/api_doc/document.rb', line 85 def write_html(output) FileUtils.mkdir_p(output) unless Dir.exist? output File.open(File.join(output, "#{model.downcase}.html"), "w") do |file| file.write(to_markdown) end end |
#write_yaml(output) ⇒ Object
92 93 94 95 96 97 |
# File 'lib/shaf/api_doc/document.rb', line 92 def write_yaml(output) FileUtils.mkdir_p(output) unless Dir.exist? output File.open(File.join(output, "#{model.downcase}.yml"), "w") do |file| file.write(generate_yaml!) end end |