Skip to content

Commit 3710669

Browse files
seanmilscotje
authored andcommitted
(PDOC-295) Add 'enum' tag support for Enum data types
Enum data types would benefit from the ability to add extended documentation on each option similarly to the '@option' tag for hashes. The option type is not a good fit for Enum data type parameters as it requires data types to be specified, which doesn't make sense in the context of a Enum type.
1 parent 9028b54 commit 3710669

File tree

15 files changed

+278
-14
lines changed

15 files changed

+278
-14
lines changed

lib/puppet-strings/markdown/base.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ def options
111111
select_tags('option')
112112
end
113113

114+
# @return [Array] enum tag hashes
115+
def enums
116+
select_tags('enum')
117+
end
118+
114119
# @param parameter_name
115120
# parameter name to match to option tags
116121
# @return [Array] option tag hashes that have a parent parameter_name
@@ -119,6 +124,14 @@ def options_for_param(parameter_name)
119124
opts_for_p unless opts_for_p.nil? || opts_for_p.length.zero?
120125
end
121126

127+
# @param parameter_name
128+
# parameter name to match to enum tags
129+
# @return [Array] enum tag hashes that have a parent parameter_name
130+
def enums_for_param(parameter_name)
131+
enums_for_p = enums.select { |e| e[:parent] == parameter_name } unless enums.nil?
132+
enums_for_p unless enums_for_p.nil? || enums_for_p.length.zero?
133+
end
134+
122135
# @return [Hash] any defaults found for the component
123136
def defaults
124137
@registry[:defaults] unless @registry[:defaults].nil?

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ Options:
6565
* **<%= o[:opt_name] %>** `<%= o[:opt_types][0] %>`: <%= o[:opt_text] %>
6666
<% end -%>
6767

68+
<% end -%>
69+
<% if enums_for_param(param[:name]) -%>
70+
Options:
71+
72+
<% enums_for_param(param[:name]).each do |e| -%>
73+
* **<%= e[:opt_name] %>**: <%= e[:opt_text] %>
74+
<% end -%>
75+
6876
<% end -%>
6977
<% if defaults && defaults[param[:name]] -%>
7078
Default value: <%= value_string(defaults[param[:name]]) %>

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ Options:
6969
* **<%= o[:opt_name] %>** `<%= o[:opt_types][0] %>`: <%= o[:opt_text] %>
7070
<% end -%>
7171

72+
<% end -%>
73+
<% if enums_for_param(param[:name]) -%>
74+
Options:
75+
76+
<% enums_for_param(param[:name]).each do |e| -%>
77+
* **<%= e[:opt_name] %>**: <%= e[:opt_text] %>
78+
<% end -%>
79+
7280
<% end -%>
7381
<% if defaults && defaults[param[:name]] -%>
7482
Default value: <%= value_string(defaults[param[:name]]) %>

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ Options:
8787
* **<%= o[:opt_name] %>** `<%= o[:opt_types][0] %>`: <%= o[:opt_text] %>
8888
<% end -%>
8989

90+
<% end -%>
91+
<% if sig.enums_for_param(param[:name]) -%>
92+
Options:
93+
94+
<% sig.enums_for_param(param[:name]).each do |e| -%>
95+
* **<%= e[:opt_name] %>**: <%= e[:opt_text] %>
96+
<% end -%>
97+
9098
<% end -%>
9199
<% end -%>
92100
<% end -%>

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,14 @@ Options:
7777
* **<%= o[:opt_name] %>** `<%= o[:opt_types][0] %>`: <%= o[:opt_text] %>
7878
<% end -%>
7979

80+
<% end -%>
81+
<% if enums_for_param(prop[:name]) -%>
82+
Options:
83+
84+
<% enums_for_param(prop[:name]).each do |e| -%>
85+
* **<%= e[:opt_name] %>**: <%= e[:opt_text] %>
86+
<% end -%>
87+
8088
<% end -%>
8189
<% if prop[:default] -%>
8290
Default value: <%= prop[:default] %>
@@ -117,6 +125,14 @@ Options:
117125
* **<%= o[:opt_name] %>** `<%= o[:opt_types][0] %>`: <%= o[:opt_text] %>
118126
<% end -%>
119127

128+
<% end -%>
129+
<% if enums_for_param(param[:name]) -%>
130+
Options:
131+
132+
<% enums_for_param(param[:name]).each do |e| -%>
133+
* **<%= e[:opt_name] %>**: <%= e[:opt_text] %>
134+
<% end -%>
135+
120136
<% end -%>
121137
<% if param[:default] -%>
122138
Default value: <%= value_string(param[:default]) %>

lib/puppet-strings/yard.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ module PuppetStrings::Yard
1111
# Sets up YARD for use with puppet-strings.
1212
# @return [void]
1313
def self.setup!
14+
# Register our factory
15+
YARD::Tags::Library.default_factory = PuppetStrings::Yard::Tags::Factory
16+
1417
# Register the template path
1518
YARD::Templates::Engine.register_template_path(File.join(File.dirname(__FILE__), 'yard', 'templates'))
1619

@@ -30,6 +33,9 @@ def self.setup!
3033
# Register the summary tag
3134
PuppetStrings::Yard::Tags::SummaryTag.register!
3235

36+
# Register the enum tag
37+
PuppetStrings::Yard::Tags::EnumTag.register!
38+
3339
# Ignore documentation on Puppet DSL calls
3440
# This prevents the YARD DSL parser from emitting warnings for Puppet's Ruby DSL
3541
YARD::Handlers::Ruby::DSLHandlerMethods::IGNORE_METHODS['create_function'] = true

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@ def to_hash
8888
if self.has_tag? :overload
8989
# loop over overloads and append onto the signatures array
9090
self.tags(:overload).each do |o|
91-
hash[:signatures] << { :signature => o.signature, :docstring => PuppetStrings::Yard::Util.docstring_to_hash(o.docstring, %i[param option return example]) }
91+
hash[:signatures] << { :signature => o.signature, :docstring => PuppetStrings::Yard::Util.docstring_to_hash(o.docstring, %i[param option enum return example]) }
9292
end
9393
else
94-
hash[:signatures] << { :signature => self.signature, :docstring => PuppetStrings::Yard::Util.docstring_to_hash(docstring, %i[param option return example]) }
94+
hash[:signatures] << { :signature => self.signature, :docstring => PuppetStrings::Yard::Util.docstring_to_hash(docstring, %i[param option enum return example]) }
9595
end
9696

9797
hash[:docstring] = PuppetStrings::Yard::Util.docstring_to_hash(docstring)

lib/puppet-strings/yard/tags.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# The module for custom YARD tags.
22
module PuppetStrings::Yard::Tags
3+
require 'puppet-strings/yard/tags/factory'
34
require 'puppet-strings/yard/tags/parameter_directive'
45
require 'puppet-strings/yard/tags/property_directive'
56
require 'puppet-strings/yard/tags/overload_tag'
67
require 'puppet-strings/yard/tags/summary_tag'
8+
require 'puppet-strings/yard/tags/enum_tag'
79
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
require 'yard/tags/option_tag'
2+
3+
# Implements an enum tag for describing enumerated value data types
4+
5+
class PuppetStrings::Yard::Tags::EnumTag < YARD::Tags::OptionTag
6+
# Registers the tag with YARD.
7+
# @return [void]
8+
def self.register!
9+
YARD::Tags::Library.define_tag("puppet.enum", :enum, :with_enums)
10+
end
11+
end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require 'yard/tags/default_factory'
2+
require 'puppet-strings/yard/tags/enum_tag'
3+
4+
class PuppetStrings::Yard::Tags::Factory < YARD::Tags::DefaultFactory
5+
6+
# Parses tag text and creates a new enum tag type. Modeled after
7+
# the parse_tag_with_options method in YARD::Tags::DefaultFactory.
8+
#
9+
# @param tag_name the name of the tag to parse
10+
# @param [String] text the raw tag text
11+
# @return [Tag] a tag object with the tag_name, name, and nested Tag as type
12+
def parse_tag_with_enums(tag_name, text)
13+
name, text = *extract_name_from_text(text)
14+
PuppetStrings::Yard::Tags::EnumTag.new(tag_name, name, parse_tag_with_name(tag_name, text))
15+
end
16+
end

0 commit comments

Comments
 (0)