Skip to content

Commit a9a0747

Browse files
committed
(PDOC-226) Add Puppet Data Type documentation
Previously Puppet-Strings could not document Ruby Data Types. This commit: * Updates the ruby handler to receive `Puppet::DataTypes.create_type` method invocations and document the call site. The documention is similar to that of a puppet custom type. * Adds tests for ruby parsing to ensure the Yard Code Object is populated correctly * Adds support for JSON, Markdown and HTML rendering. * Adds tests for JSON and Markdown rendering
1 parent 763eaee commit a9a0747

File tree

29 files changed

+1377
-2
lines changed

29 files changed

+1377
-2
lines changed

JSON.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ puppet strings generate --format json
99
Document Schema
1010
===============
1111

12-
At the top level, there are seven arrays in the JSON document:
12+
At the top level, there are eight arrays in the JSON document:
1313

1414
| Document Key | Description |
1515
| ---------------- | ----------------------------------------------------------------------------- |
1616
| puppet_classes | The list of Puppet classes that were parsed. |
17+
| data_types | The list of data types that were parsed. |
1718
| defined_types | The list of defined types that were parsed. |
1819
| resource_types | The list of resource types that were parsed. |
1920
| providers | The list of resource providers that were parsed. |
@@ -36,6 +37,20 @@ Each entry in the `puppet_classes` list is an object with the following attribut
3637
| defaults | The map of parameter names to default values. |
3738
| source | The Puppet source code for the class. |
3839

40+
Data Types
41+
----------
42+
43+
Each entry in the `data_types` list is an object with the following attributes:
44+
45+
| Attribute Key | Description |
46+
| ------------- | ----------------------------------------------------------- |
47+
| name | The name of the data type. |
48+
| file | The file defining the data type. |
49+
| line | The line where the data type is data. |
50+
| docstring | The *DocString* object for the data type (see below). |
51+
| defaults | The map of parameter names to default values. |
52+
| source | The ruby source code for the data type. (Not Implemented) |
53+
3954
Defined Types
4055
-------------
4156

lib/puppet-strings/json.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module PuppetStrings::Json
88
def self.render(file = nil)
99
document = {
1010
puppet_classes: YARD::Registry.all(:puppet_class).sort_by!(&:name).map!(&:to_hash),
11+
data_types: YARD::Registry.all(:puppet_data_type).sort_by!(&:name).map!(&:to_hash),
1112
defined_types: YARD::Registry.all(:puppet_defined_type).sort_by!(&:name).map!(&:to_hash),
1213
resource_types: YARD::Registry.all(:puppet_type).sort_by!(&:name).map!(&:to_hash),
1314
providers: YARD::Registry.all(:puppet_provider).sort_by!(&:name).map!(&:to_hash),

lib/puppet-strings/markdown.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module PuppetStrings::Markdown
55
require_relative 'markdown/puppet_classes'
66
require_relative 'markdown/functions'
77
require_relative 'markdown/defined_types'
8+
require_relative 'markdown/data_types'
89
require_relative 'markdown/resource_types'
910
require_relative 'markdown/puppet_tasks'
1011
require_relative 'markdown/puppet_plans'
@@ -20,6 +21,7 @@ def self.generate
2021
final << PuppetStrings::Markdown::DefinedTypes.render
2122
final << PuppetStrings::Markdown::ResourceTypes.render
2223
final << PuppetStrings::Markdown::Functions.render
24+
final << PuppetStrings::Markdown::DataTypes.render
2325
final << PuppetStrings::Markdown::PuppetTasks.render
2426
final << PuppetStrings::Markdown::PuppetPlans.render
2527

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require 'puppet-strings/markdown/base'
2+
3+
module PuppetStrings::Markdown
4+
class DataType < Base
5+
def initialize(registry)
6+
@template = 'data_type.erb'
7+
super(registry, 'data type')
8+
end
9+
10+
def render
11+
super(@template)
12+
end
13+
end
14+
end
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
require_relative 'data_type'
2+
3+
module PuppetStrings::Markdown
4+
module DataTypes
5+
6+
# @return [Array] list of data types
7+
def self.in_dtypes
8+
arr = YARD::Registry.all(:puppet_data_type).sort_by!(&:name).map!(&:to_hash)
9+
arr.map! { |a| PuppetStrings::Markdown::DataType.new(a) }
10+
end
11+
12+
def self.contains_private?
13+
result = false
14+
unless in_dtypes.nil?
15+
in_dtypes.find { |type| type.private? }.nil? ? false : true
16+
end
17+
end
18+
19+
def self.render
20+
final = in_dtypes.length > 0 ? "## Data types\n\n" : ""
21+
in_dtypes.each do |type|
22+
final << type.render unless type.private?
23+
end
24+
final
25+
end
26+
27+
28+
def self.toc_info
29+
final = ["Data types"]
30+
31+
in_dtypes.each do |type|
32+
final.push(type.toc_info)
33+
end
34+
35+
final
36+
end
37+
end
38+
end

lib/puppet-strings/markdown/table_of_contents.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ def self.render
77
PuppetStrings::Markdown::DefinedTypes,
88
PuppetStrings::Markdown::ResourceTypes,
99
PuppetStrings::Markdown::Functions,
10+
PuppetStrings::Markdown::DataTypes,
1011
PuppetStrings::Markdown::PuppetTasks,
1112
PuppetStrings::Markdown::PuppetPlans].each do |r|
1213
toc = r.toc_info
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
### <%= name %>
2+
3+
<% if text -%>
4+
<%= text %>
5+
<% elsif summary -%>
6+
<%= summary %>
7+
<% else -%>
8+
<%= "The #{name} data type." %>
9+
<% end -%>
10+
11+
<% if todo -%>
12+
* **TODO** <%= todo %>
13+
14+
<% end -%>
15+
<% if note -%>
16+
* **Note** <%= note %>
17+
18+
<% end -%>
19+
<% if since -%>
20+
* **Since** <%= since %>
21+
22+
<% end -%>
23+
<% if see -%>
24+
* **See also**
25+
<% see.each do |sa| -%>
26+
<% if sa[:name] -%>
27+
<%= sa[:name] %>
28+
<% end -%>
29+
<% if sa[:text] -%>
30+
<%= sa[:text] %>
31+
<% end -%>
32+
<% end -%>
33+
34+
<% end -%>
35+
<% if examples -%>
36+
#### Examples
37+
38+
<% examples.each do |eg| -%>
39+
##### <%= eg[:name] %>
40+
41+
```puppet
42+
<%= eg[:text] %>
43+
```
44+
45+
<% end -%>
46+
<% end -%>
47+
<% if params -%>
48+
#### Parameters
49+
50+
The following parameters are available in the `<%= name %>` <%= @type %>.
51+
52+
<% params.each do |param| -%>
53+
##### `<%= param[:name] %>`
54+
55+
<% if param[:types] -%>
56+
Data type: `<%= param[:types].join(', ') -%>`
57+
58+
<% end -%>
59+
<%= param[:text] %>
60+
61+
<% if options_for_param(param[:name]) -%>
62+
Options:
63+
64+
<% options_for_param(param[:name]).each do |o| -%>
65+
* **<%= o[:opt_name] %>** `<%= o[:opt_types][0] %>`: <%= o[:opt_text] %>
66+
<% end -%>
67+
68+
<% end -%>
69+
<% if defaults && defaults[param[:name]] -%>
70+
Default value: <%= value_string(defaults[param[:name]]) %>
71+
72+
<% end -%>
73+
<% end -%>
74+
<% end -%>

lib/puppet-strings/yard.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def all_objects
4646
:module,
4747
:class,
4848
:puppet_class,
49+
:puppet_data_type,
4950
:puppet_defined_type,
5051
:puppet_type,
5152
:puppet_provider,
@@ -64,6 +65,10 @@ def stats_for_puppet_classes
6465
output 'Puppet Classes', *type_statistics_all(:puppet_class)
6566
end
6667

68+
def stats_for_puppet_data_types
69+
output 'Puppet Data Types', *type_statistics_all(:puppet_data_type)
70+
end
71+
6772
def stats_for_puppet_defined_types
6873
output 'Puppet Defined Types', *type_statistics_all(:puppet_defined_type)
6974
end

lib/puppet-strings/yard/code_objects.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# The module for custom YARD code objects.
22
module PuppetStrings::Yard::CodeObjects
33
require 'puppet-strings/yard/code_objects/class'
4+
require 'puppet-strings/yard/code_objects/data_type'
45
require 'puppet-strings/yard/code_objects/defined_type'
56
require 'puppet-strings/yard/code_objects/type'
67
require 'puppet-strings/yard/code_objects/provider'
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
require 'puppet-strings/yard/code_objects/group'
2+
require 'puppet-strings/yard/util'
3+
4+
# Implements the group for Puppet DataTypes.
5+
class PuppetStrings::Yard::CodeObjects::DataTypes < PuppetStrings::Yard::CodeObjects::Group
6+
# Gets the singleton instance of the group.
7+
# @return Returns the singleton instance of the group.
8+
def self.instance
9+
super(:puppet_data_types)
10+
end
11+
12+
# Gets the display name of the group.
13+
# @param [Boolean] prefix whether to show a prefix. Ignored for Puppet group namespaces.
14+
# @return [String] Returns the display name of the group.
15+
def name(prefix = false)
16+
'Puppet Data Types'
17+
end
18+
end
19+
20+
# Implements the Puppet DataType code object.
21+
class PuppetStrings::Yard::CodeObjects::DataType < PuppetStrings::Yard::CodeObjects::Base
22+
# Initializes a Puppet class code object.
23+
# @param [String] The name of the Data Type
24+
# @return [void]
25+
def initialize(name)
26+
super(PuppetStrings::Yard::CodeObjects::DataTypes.instance, name)
27+
@parameters = []
28+
@defaults = {}
29+
end
30+
31+
# Gets the type of the code object.
32+
# @return Returns the type of the code object.
33+
def type
34+
:puppet_data_type
35+
end
36+
37+
# Gets the source of the code object.
38+
# @return Returns the source of the code object.
39+
def source
40+
# Not implemented, but would be nice!
41+
nil
42+
end
43+
44+
def parameter_exist?(name)
45+
!docstring.tags(:param).find { |item| item.name == name }.nil?
46+
end
47+
48+
def add_parameter(name, type, default)
49+
tag = docstring.tags(:param).find { |item| item.name == name }
50+
if tag.nil?
51+
tag = YARD::Tags::Tag.new(:param, '', nil, name)
52+
docstring.add_tag(tag)
53+
end
54+
type = [type] unless type.is_a?(Array)
55+
tag.types = type if tag.types.nil?
56+
set_parameter_default(name, default)
57+
end
58+
59+
def set_parameter_default(param_name, default)
60+
defaults.delete(param_name)
61+
defaults[param_name] = default unless default.nil?
62+
end
63+
64+
def parameters
65+
docstring.tags(:param).map { |tag| [tag.name, defaults[tag.name]] }
66+
end
67+
68+
# Converts the code object to a hash representation.
69+
# @return [Hash] Returns a hash representation of the code object.
70+
def to_hash
71+
hash = {}
72+
hash[:name] = name
73+
hash[:file] = file
74+
hash[:line] = line
75+
hash[:docstring] = PuppetStrings::Yard::Util.docstring_to_hash(docstring)
76+
hash[:defaults] = defaults unless defaults.empty?
77+
hash[:source] = source unless source && source.empty?
78+
hash
79+
end
80+
end

lib/puppet-strings/yard/handlers.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
module PuppetStrings::Yard::Handlers
33
# The module for custom Ruby YARD handlers.
44
module Ruby
5+
require 'puppet-strings/yard/handlers/ruby/data_type_handler'
56
require 'puppet-strings/yard/handlers/ruby/type_handler'
67
require 'puppet-strings/yard/handlers/ruby/type_extras_handler'
78
require 'puppet-strings/yard/handlers/ruby/rsapi_handler'

0 commit comments

Comments
 (0)