Class: ApiGuides::Section
- Inherits:
-
Object
- Object
- ApiGuides::Section
- Defined in:
- lib/api_guides/section.rb
Overview
This class is simply a structure to hold the docs and the examples.
Sections require docs, but do not require any examples. Set examples when you want those on the right side of the docs.
You never interact with this class directly.
Instance Attribute Summary collapse
-
#docs ⇒ Object
Returns the value of attribute docs.
-
#examples ⇒ Object
Returns the value of attribute examples.
-
#reference ⇒ Object
Returns the value of attribute reference.
-
#title ⇒ Object
Returns the value of attribute title.
Class Method Summary collapse
-
.from_xml(xml) ⇒ Object
Takes an XML representation and parse it into a section instance.
Instance Method Summary collapse
-
#initialize(attributes = {}) ⇒ Section
constructor
A new instance of Section.
Constructor Details
#initialize(attributes = {}) ⇒ Section
Returns a new instance of Section.
15 16 17 18 19 |
# File 'lib/api_guides/section.rb', line 15 def initialize(attributes = {}) attributes.each_pair do |attr, value| send "#{attr}=", value end end |
Instance Attribute Details
#docs ⇒ Object
Returns the value of attribute docs.
13 14 15 |
# File 'lib/api_guides/section.rb', line 13 def docs @docs end |
#examples ⇒ Object
Returns the value of attribute examples.
13 14 15 |
# File 'lib/api_guides/section.rb', line 13 def examples @examples end |
#reference ⇒ Object
Returns the value of attribute reference.
13 14 15 |
# File 'lib/api_guides/section.rb', line 13 def reference @reference end |
#title ⇒ Object
Returns the value of attribute title.
13 14 15 |
# File 'lib/api_guides/section.rb', line 13 def title @title end |
Class Method Details
.from_xml(xml) ⇒ Object
Takes an XML representation and parse it into a section instance.
Here is XML format expected:
<section title="Foo">
<docs>
<![CDATA[
Insert your markdown here
]]>
</docs>
<reference title="Bar">
<![CDATA[
Insert your markdown here
]]>
</reference>
<examples>
<example language="ruby">
<![CDATA[
Insert your markdown here>
]]>
</example>
</example>
</section>
It also loops instantiates the Reference and examples if they are given using their ‘from_xml` methods as well
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/api_guides/section.rb', line 49 def self.from_xml(xml) doc = Nokogiri::XML.parse(xml).at_xpath('//section') section = Section.new :title => doc.attributes['title'].try(:value) section.docs = doc.at_xpath('./docs').content if doc.at_xpath('./docs') if reference_xml = doc.at_xpath('./reference') section.reference = Reference.from_xml(reference_xml.to_s) end section.examples = doc.xpath('//example').map do |example_xml| Example.from_xml example_xml.to_s end section end |