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 ⇒ Hash
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 @tags = registry[:docstring][:tags] || [] end |
Instance Method Details
#defaults ⇒ Hash
Returns any defaults found for the component.
123 124 125 |
# File 'lib/puppet-strings/markdown/base.rb', line 123 def defaults @registry[:defaults] unless @registry[:defaults].nil? end |
#examples ⇒ Array
Returns example tag hashes.
100 101 102 |
# File 'lib/puppet-strings/markdown/base.rb', line 100 def examples ('example') end |
#link ⇒ String
Returns makes the component name suitable for a GitHub markdown link.
138 139 140 |
# File 'lib/puppet-strings/markdown/base.rb', line 138 def link name.delete('::').strip.gsub(' ','-').downcase end |
#name ⇒ String
Returns top-level name.
70 71 72 |
# File 'lib/puppet-strings/markdown/base.rb', line 70 def name @registry[:name].to_s unless @registry[:name].nil? end |
#options ⇒ Array
Returns option tag hashes.
110 111 112 |
# File 'lib/puppet-strings/markdown/base.rb', line 110 def ('option') end |
#options_for_param(parameter_name) ⇒ Array
Returns option tag hashes that have a parent parameter_name.
117 118 119 120 |
# File 'lib/puppet-strings/markdown/base.rb', line 117 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.
95 96 97 |
# File 'lib/puppet-strings/markdown/base.rb', line 95 def params ('param') end |
#private? ⇒ Boolean
155 156 157 |
# File 'lib/puppet-strings/markdown/base.rb', line 155 def private? @tags.any? { |tag| tag[:tag_name] == 'api' && tag[:text] == 'private' } end |
#raises ⇒ Array
Returns raise tag hashes.
105 106 107 |
# File 'lib/puppet-strings/markdown/base.rb', line 105 def raises ('raise') end |
#render(template) ⇒ String
Returns full markdown rendering of a component.
160 161 162 163 |
# File 'lib/puppet-strings/markdown/base.rb', line 160 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.
80 81 82 |
# File 'lib/puppet-strings/markdown/base.rb', line 80 def return_type @tags.find { |tag| tag[:tag_name] == 'return' }[:types][0] if @tags.any? { |tag| tag[:tag_name] == 'return' } end |
#see ⇒ Array
Returns @see tag hashes.
90 91 92 |
# File 'lib/puppet-strings/markdown/base.rb', line 90 def see ('see') end |
#since ⇒ String
Returns text from @since tag.
85 86 87 |
# File 'lib/puppet-strings/markdown/base.rb', line 85 def since @tags.find { |tag| tag[:tag_name] == 'since' }[:text] if @tags.any? { |tag| tag[:tag_name] == 'since' } end |
#text ⇒ String
Returns ‘Overview’ text (untagged text).
75 76 77 |
# File 'lib/puppet-strings/markdown/base.rb', line 75 def text @registry[:docstring][:text] unless @registry[:docstring][:text].empty? end |
#toc_info ⇒ Hash
Returns information needed for the table of contents.
128 129 130 131 132 133 134 135 |
# File 'lib/puppet-strings/markdown/base.rb', line 128 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.
146 147 148 149 150 151 152 153 |
# File 'lib/puppet-strings/markdown/base.rb', line 146 def value_string(value) to_symbol = %w[undef true false] if to_symbol.include? value return "`#{value}`" else return value end end |