Class: PuppetStrings::Markdown::Base

Inherits:
Object
  • Object
show all
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 +
""}

Instance Method Summary collapse

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

#defaultsHash

Returns any defaults found for the component.

Returns:

  • (Hash)

    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

#examplesArray

Returns example tag hashes.

Returns:

  • (Array)

    example tag hashes



100
101
102
# File 'lib/puppet-strings/markdown/base.rb', line 100

def examples
  select_tags('example')
end

Returns makes the component name suitable for a GitHub markdown link.

Returns:

  • (String)

    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

#nameString

Returns top-level name.

Returns:

  • (String)

    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

#optionsArray

Returns option tag hashes.

Returns:

  • (Array)

    option tag hashes



110
111
112
# File 'lib/puppet-strings/markdown/base.rb', line 110

def options
  select_tags('option')
end

#options_for_param(parameter_name) ⇒ Array

Returns option tag hashes that have a parent parameter_name.

Parameters:

  • parameter_name

    parameter name to match to option tags

Returns:

  • (Array)

    option tag hashes that have a parent parameter_name



117
118
119
120
# File 'lib/puppet-strings/markdown/base.rb', line 117

def options_for_param(parameter_name)
  opts_for_p = options.select { |o| o[:parent] == parameter_name } unless options.nil?
  opts_for_p unless opts_for_p.nil? || opts_for_p.length.zero?
end

#paramsArray

Returns parameter tag hashes.

Returns:

  • (Array)

    parameter tag hashes



95
96
97
# File 'lib/puppet-strings/markdown/base.rb', line 95

def params
  select_tags('param')
end

#private?Boolean

Returns:

  • (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

#raisesArray

Returns raise tag hashes.

Returns:

  • (Array)

    raise tag hashes



105
106
107
# File 'lib/puppet-strings/markdown/base.rb', line 105

def raises
  select_tags('raise')
end

#render(template) ⇒ String

Returns full markdown rendering of a component.

Returns:

  • (String)

    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_typeString

Returns data type of return value.

Returns:

  • (String)

    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

#seeArray

Returns @see tag hashes.

Returns:

  • (Array)

    @see tag hashes



90
91
92
# File 'lib/puppet-strings/markdown/base.rb', line 90

def see
  select_tags('see')
end

#sinceString

Returns text from @since tag.

Returns:

  • (String)

    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

#textString

Returns ‘Overview’ text (untagged text).

Returns:

  • (String)

    ‘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_infoHash

Returns information needed for the table of contents.

Returns:

  • (Hash)

    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.

Parameters:

  • value

    any string

Returns:

  • (String)

    value or value in backticks if it is in a list



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