Skip to content

Commit a76dd4c

Browse files
authored
Merge pull request #235 from glennsarti/gh225-datatype-functions
(GH-225) Document functions in Puppet Datatypes
2 parents 8941b63 + b6bf0f8 commit a76dd4c

File tree

16 files changed

+702
-1837
lines changed

16 files changed

+702
-1837
lines changed

lib/puppet-strings/markdown/data_type.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,31 @@ module PuppetStrings::Markdown
44
# This class encapsualtes ruby data types and puppet type aliases
55
class DataType < Base
66
attr_reader :alias_of
7+
attr_reader :functions
78

89
def initialize(registry)
910
@template = 'data_type.erb'
1011
super(registry, 'data type')
1112
@alias_of = registry[:alias_of] unless registry[:alias_of].nil?
13+
@functions = @registry[:functions].nil? ? nil : @registry[:functions].map { |func| DataType::Function.new(func) }
1214
end
1315

1416
def render
1517
super(@template)
1618
end
1719
end
20+
21+
class DataType::Function < Base
22+
def initialize(registry)
23+
super(registry, 'data_type_function')
24+
end
25+
26+
def render
27+
super('data_type_function.erb')
28+
end
29+
30+
def signature
31+
@registry[:signature]
32+
end
33+
end
1834
end

lib/puppet-strings/markdown/templates/data_type.erb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,10 @@ Default value: <%= value_string(defaults[param[:name]]) %>
8484
<% end -%>
8585
<% end -%>
8686
<% end -%>
87+
<% if functions -%>
88+
#### Functions
89+
90+
The following functions are available in the `<%= name %>` <%= @type %>.
91+
92+
<% functions.each do |func| -%><%= func.render -%><% end -%>
93+
<% end -%>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
### <%= name %>
2+
3+
#### `<%= signature %>`
4+
5+
<% if text -%>
6+
<%= text %>
7+
8+
<% elsif summary -%>
9+
<%= summary %>
10+
11+
<% else -%>
12+
<%= "The #{name} function." %>
13+
14+
<% end -%>
15+
<% if note -%>
16+
* **Note** <%= note %>
17+
18+
<% end -%>
19+
<% if return_type -%>
20+
Returns: `<%= return_type %>`<% if return_val %> <%= return_val %><% end %>
21+
22+
<% end -%>
23+
<% if raises -%>
24+
Raises:
25+
<% raises.each do |r| -%>
26+
* <%= error_type(r[:text]) %> <%= error_text(r[:text]) %>
27+
<% end -%>
28+
29+
<% end -%>
30+
<% if examples -%>
31+
##### Examples
32+
33+
<% examples.each do |eg| -%>
34+
###### <%= eg[:name] %>
35+
36+
```puppet
37+
<%= eg[:text] %>
38+
```
39+
40+
<% end -%>
41+
<% end -%>
42+
<% if params -%>
43+
<% params.each do |param| -%>
44+
##### `<%= param[:name] %>`
45+
46+
Data type: `<%= param[:types][0] %>`
47+
48+
<%= param[:text] %>
49+
50+
<% if options_for_param(param[:name]) -%>
51+
Options:
52+
53+
<% options_for_param(param[:name]).each do |o| -%>
54+
* **<%= o[:opt_name] %>** `<%= o[:opt_types][0] %>`: <%= o[:opt_text] %>
55+
<% end -%>
56+
57+
<% end -%>
58+
<% if enums_for_param(param[:name]) -%>
59+
Options:
60+
61+
<% enums_for_param(param[:name]).each do |e| -%>
62+
* **<%= e[:opt_name] %>**: <%= e[:opt_text] %>
63+
<% end -%>
64+
65+
<% end -%>
66+
<% end -%>
67+
<% end -%>

lib/puppet-strings/yard/code_objects/data_type.rb

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ class PuppetStrings::Yard::CodeObjects::DataType < PuppetStrings::Yard::CodeObje
2424
# @return [void]
2525
def initialize(name)
2626
super(PuppetStrings::Yard::CodeObjects::DataTypes.instance, name)
27-
@parameters = []
2827
@defaults = {}
2928
end
3029

@@ -41,10 +40,6 @@ def source
4140
nil
4241
end
4342

44-
def parameter_exist?(name)
45-
!docstring.tags(:param).find { |item| item.name == name }.nil?
46-
end
47-
4843
def add_parameter(name, type, default)
4944
tag = docstring.tags(:param).find { |item| item.name == name }
5045
if tag.nil?
@@ -65,16 +60,41 @@ def parameters
6560
docstring.tags(:param).map { |tag| [tag.name, defaults[tag.name]] }
6661
end
6762

63+
def add_function(name, return_type, parameter_types)
64+
meth_obj = YARD::CodeObjects::MethodObject.new(self, name, :class)
65+
66+
# Add return tag
67+
meth_obj.add_tag(YARD::Tags::Tag.new(:return, '', return_type))
68+
69+
# Add parameters
70+
parameter_types.each_with_index do |param_type, index|
71+
meth_obj.add_tag(YARD::Tags::Tag.new(:param, '', [param_type], "param#{index + 1}"))
72+
end
73+
74+
self.meths << meth_obj
75+
end
76+
77+
def functions
78+
meths
79+
end
80+
6881
# Converts the code object to a hash representation.
6982
# @return [Hash] Returns a hash representation of the code object.
7083
def to_hash
7184
hash = {}
7285
hash[:name] = name
7386
hash[:file] = file
7487
hash[:line] = line
75-
hash[:docstring] = PuppetStrings::Yard::Util.docstring_to_hash(docstring)
88+
hash[:docstring] = PuppetStrings::Yard::Util.docstring_to_hash(docstring, %i[param option enum return example])
7689
hash[:defaults] = defaults unless defaults.empty?
7790
hash[:source] = source unless source && source.empty?
91+
hash[:functions] = functions.map do |func|
92+
{
93+
name: func.name,
94+
signature: func.signature,
95+
docstring: PuppetStrings::Yard::Util.docstring_to_hash(func.docstring, %i[param option enum return example])
96+
}
97+
end
7898
hash
7999
end
80100
end

0 commit comments

Comments
 (0)