Class: PuppetStrings::Markdown::Base
- Inherits:
-
Object
- Object
- PuppetStrings::Markdown::Base
- Defined in:
- lib/puppet-strings/markdown/base.rb
Overview
This class makes elements in a YARD::Registry hash easily accessible for templates.
Here’s an example hash: :file=>“(stdin)”, :line=>16, :inherits=>“foo::bar”, :docstring=>
{:text=>"An overview for a simple class.",
:tags=>
[{:tag_name=>"summary", :text=>"A simple class.",
:text=>"1.0.0",
:name=>"www.puppet.com",
:text=>
"class { 'klass':\n" +
" param1 => 1,\n" +
" param3 => 'foo',\n" +
"",
:name=>"This is an example"},
:text=>"eputnam",
:name=>"opts",
:text=>"SomeError",
:text=>"First param.",
:types=>["Integer"],
:name=>"param1",
:text=>"Second param.",
:types=>["Any"],
:name=>"param2",
:text=>"Third param.",
:types=>["String"],
:name=>"param3"]},
:defaults=>{“param1”=>“1”, “param2”=>“undef”, “param3”=>“‘hi’”}, :source=>
"class klass (\n" +
" Integer $param1 = 1,\n" +
" $param2 = undef,\n" +
" String $param3 = 'hi'\n" +
") inherits foo::bar +
""}
Direct Known Subclasses
DefinedType, Function, Function::Signature, PuppetClass, PuppetPlan, PuppetTask, ResourceType
Instance Method Summary collapse
-
#defaults ⇒ Array
Any defaults found for the component.
-
#examples ⇒ Array
Example tag hashes.
-
#initialize(registry, component_type) ⇒ Base
constructor
A new instance of Base.
-
#link ⇒ String
Makes the component name suitable for a GitHub markdown link.
-
#name ⇒ String
Top-level name.
-
#options ⇒ Array
Option tag hashes.
-
#options_for_param(parameter_name) ⇒ Array
Option tag hashes that have a parent parameter_name.
-
#params ⇒ Array
Parameter tag hashes.
- #private? ⇒ Boolean
-
#raises ⇒ Array
Raise tag hashes.
-
#render(template) ⇒ String
Full markdown rendering of a component.
-
#return_type ⇒ String
Data type of return value.
- #see ⇒ Array
-
#since ⇒ String
Text from @since tag.
-
#text ⇒ String
‘Overview’ text (untagged text).
-
#toc_info ⇒ Hash
Information needed for the table of contents.
-
#value_string(value) ⇒ String
Some return, default, or valid values need to be in backticks.
Constructor Details
#initialize(registry, component_type) ⇒ Base
Returns a new instance of Base.
50 51 52 53 54 |
# File 'lib/puppet-strings/markdown/base.rb', line 50 def initialize(registry, component_type) @type = component_type @registry = registry = registry[:docstring][:tags] || [] end |
Instance Method Details
#defaults ⇒ Array
Returns any defaults found for the component.
121 122 123 |
# File 'lib/puppet-strings/markdown/base.rb', line 121 def defaults @registry[:defaults] unless @registry[:defaults].nil? end |
#examples ⇒ Array
Returns example tag hashes.
98 99 100 |
# File 'lib/puppet-strings/markdown/base.rb', line 98 def examples .select { |tag| tag[:tag_name] == 'example' } unless .select { |tag| tag[:tag_name] == 'example' }[0].nil? end |
#link ⇒ String
Returns makes the component name suitable for a GitHub markdown link.
136 137 138 |
# File 'lib/puppet-strings/markdown/base.rb', line 136 def link name.delete('::').strip.gsub(' ','-').downcase end |
#name ⇒ String
Returns top-level name.
68 69 70 |
# File 'lib/puppet-strings/markdown/base.rb', line 68 def name @registry[:name].to_s unless @registry[:name].nil? end |
#options ⇒ Array
Returns option tag hashes.
108 109 110 |
# File 'lib/puppet-strings/markdown/base.rb', line 108 def .select { |tag| tag[:tag_name] == 'option' } unless .select { |tag| tag[:tag_name] == 'option' }[0].nil? end |
#options_for_param(parameter_name) ⇒ Array
Returns option tag hashes that have a parent parameter_name.
115 116 117 118 |
# File 'lib/puppet-strings/markdown/base.rb', line 115 def (parameter_name) opts_for_p = .select { |o| o[:parent] == parameter_name } unless .nil? opts_for_p unless opts_for_p.nil? || opts_for_p.length.zero? end |
#params ⇒ Array
Returns parameter tag hashes.
93 94 95 |
# File 'lib/puppet-strings/markdown/base.rb', line 93 def params .select { |tag| tag[:tag_name] == 'param' } unless .select { |tag| tag[:tag_name] == 'param' }[0].nil? end |
#private? ⇒ Boolean
153 154 155 156 157 158 159 160 |
# File 'lib/puppet-strings/markdown/base.rb', line 153 def private? result = false api = .find { |tag| tag[:tag_name] == 'api' } unless api.nil? result = api[:text] == 'private' ? true : false end result end |
#raises ⇒ Array
Returns raise tag hashes.
103 104 105 |
# File 'lib/puppet-strings/markdown/base.rb', line 103 def raises .select { |tag| tag[:tag_name] == 'raise' } unless .select { |tag| tag[:tag_name] == 'raise' }[0].nil? end |
#render(template) ⇒ String
Returns full markdown rendering of a component.
163 164 165 166 |
# File 'lib/puppet-strings/markdown/base.rb', line 163 def render(template) file = File.join(File.dirname(__FILE__),"templates/#{template}") ERB.new(File.read(file), nil, '-').result(binding) end |
#return_type ⇒ String
Returns data type of return value.
78 79 80 |
# File 'lib/puppet-strings/markdown/base.rb', line 78 def return_type .select { |tag| tag[:tag_name] == 'return' }[0][:types][0] unless .select { |tag| tag[:tag_name] == 'return' }[0].nil? end |
#see ⇒ Array
Returns @see tag hashes.
88 89 90 |
# File 'lib/puppet-strings/markdown/base.rb', line 88 def see .select { |tag| tag[:tag_name] == 'see' } unless .select { |tag| tag[:tag_name] == 'see' }[0].nil? end |
#since ⇒ String
Returns text from @since tag.
83 84 85 |
# File 'lib/puppet-strings/markdown/base.rb', line 83 def since .select { |tag| tag[:tag_name] == 'since' }[0][:text] unless .select { |tag| tag[:tag_name] == 'since' }[0].nil? end |
#text ⇒ String
Returns ‘Overview’ text (untagged text).
73 74 75 |
# File 'lib/puppet-strings/markdown/base.rb', line 73 def text @registry[:docstring][:text] unless @registry[:docstring][:text].empty? end |
#toc_info ⇒ Hash
Returns information needed for the table of contents.
126 127 128 129 130 131 132 133 |
# File 'lib/puppet-strings/markdown/base.rb', line 126 def toc_info { name: name.to_s, link: link, desc: summary || @registry[:docstring][:text][0..140].gsub("\n",' '), private: private? } end |
#value_string(value) ⇒ String
Some return, default, or valid values need to be in backticks. Instead of fu in the handler or code_object, this just does the change on the front.
144 145 146 147 148 149 150 151 |
# File 'lib/puppet-strings/markdown/base.rb', line 144 def value_string(value) to_symbol = %w[undef true false] if to_symbol.include? value return "`#{value}`" else return value end end |