diff --git a/lib/puppet-strings/markdown/base.rb b/lib/puppet-strings/markdown/base.rb index 9bd28d8c6..57ba6d746 100644 --- a/lib/puppet-strings/markdown/base.rb +++ b/lib/puppet-strings/markdown/base.rb @@ -111,6 +111,11 @@ def options select_tags('option') end + # @return [Array] enum tag hashes + def enums + select_tags('enum') + end + # @param parameter_name # parameter name to match to option tags # @return [Array] option tag hashes that have a parent parameter_name @@ -119,6 +124,14 @@ def options_for_param(parameter_name) opts_for_p unless opts_for_p.nil? || opts_for_p.length.zero? end + # @param parameter_name + # parameter name to match to enum tags + # @return [Array] enum tag hashes that have a parent parameter_name + def enums_for_param(parameter_name) + enums_for_p = enums.select { |e| e[:parent] == parameter_name } unless enums.nil? + enums_for_p unless enums_for_p.nil? || enums_for_p.length.zero? + end + # @return [Hash] any defaults found for the component def defaults @registry[:defaults] unless @registry[:defaults].nil? diff --git a/lib/puppet-strings/markdown/templates/classes_and_defines.erb b/lib/puppet-strings/markdown/templates/classes_and_defines.erb index ceaaf9351..9ebbfb766 100644 --- a/lib/puppet-strings/markdown/templates/classes_and_defines.erb +++ b/lib/puppet-strings/markdown/templates/classes_and_defines.erb @@ -65,6 +65,14 @@ Options: * **<%= o[:opt_name] %>** `<%= o[:opt_types][0] %>`: <%= o[:opt_text] %> <% end -%> +<% end -%> +<% if enums_for_param(param[:name]) -%> +Options: + +<% enums_for_param(param[:name]).each do |e| -%> +* **<%= e[:opt_name] %>**: <%= e[:opt_text] %> +<% end -%> + <% end -%> <% if defaults && defaults[param[:name]] -%> Default value: <%= value_string(defaults[param[:name]]) %> diff --git a/lib/puppet-strings/markdown/templates/data_type.erb b/lib/puppet-strings/markdown/templates/data_type.erb index 6306b69d2..0631a2b64 100644 --- a/lib/puppet-strings/markdown/templates/data_type.erb +++ b/lib/puppet-strings/markdown/templates/data_type.erb @@ -69,6 +69,14 @@ Options: * **<%= o[:opt_name] %>** `<%= o[:opt_types][0] %>`: <%= o[:opt_text] %> <% end -%> +<% end -%> +<% if enums_for_param(param[:name]) -%> +Options: + +<% enums_for_param(param[:name]).each do |e| -%> +* **<%= e[:opt_name] %>**: <%= e[:opt_text] %> +<% end -%> + <% end -%> <% if defaults && defaults[param[:name]] -%> Default value: <%= value_string(defaults[param[:name]]) %> diff --git a/lib/puppet-strings/markdown/templates/function.erb b/lib/puppet-strings/markdown/templates/function.erb index 504a934a4..4c520fe51 100644 --- a/lib/puppet-strings/markdown/templates/function.erb +++ b/lib/puppet-strings/markdown/templates/function.erb @@ -87,6 +87,14 @@ Options: * **<%= o[:opt_name] %>** `<%= o[:opt_types][0] %>`: <%= o[:opt_text] %> <% end -%> +<% end -%> +<% if sig.enums_for_param(param[:name]) -%> +Options: + +<% sig.enums_for_param(param[:name]).each do |e| -%> +* **<%= e[:opt_name] %>**: <%= e[:opt_text] %> +<% end -%> + <% end -%> <% end -%> <% end -%> diff --git a/lib/puppet-strings/markdown/templates/resource_type.erb b/lib/puppet-strings/markdown/templates/resource_type.erb index bea6f5598..befac4cfa 100644 --- a/lib/puppet-strings/markdown/templates/resource_type.erb +++ b/lib/puppet-strings/markdown/templates/resource_type.erb @@ -77,6 +77,14 @@ Options: * **<%= o[:opt_name] %>** `<%= o[:opt_types][0] %>`: <%= o[:opt_text] %> <% end -%> +<% end -%> +<% if enums_for_param(prop[:name]) -%> +Options: + +<% enums_for_param(prop[:name]).each do |e| -%> +* **<%= e[:opt_name] %>**: <%= e[:opt_text] %> +<% end -%> + <% end -%> <% if prop[:default] -%> Default value: <%= prop[:default] %> @@ -117,6 +125,14 @@ Options: * **<%= o[:opt_name] %>** `<%= o[:opt_types][0] %>`: <%= o[:opt_text] %> <% end -%> +<% end -%> +<% if enums_for_param(param[:name]) -%> +Options: + +<% enums_for_param(param[:name]).each do |e| -%> +* **<%= e[:opt_name] %>**: <%= e[:opt_text] %> +<% end -%> + <% end -%> <% if param[:default] -%> Default value: <%= value_string(param[:default]) %> diff --git a/lib/puppet-strings/yard.rb b/lib/puppet-strings/yard.rb index 56ea11145..f7e4c4038 100644 --- a/lib/puppet-strings/yard.rb +++ b/lib/puppet-strings/yard.rb @@ -11,6 +11,9 @@ module PuppetStrings::Yard # Sets up YARD for use with puppet-strings. # @return [void] def self.setup! + # Register our factory + YARD::Tags::Library.default_factory = PuppetStrings::Yard::Tags::Factory + # Register the template path YARD::Templates::Engine.register_template_path(File.join(File.dirname(__FILE__), 'yard', 'templates')) @@ -30,6 +33,9 @@ def self.setup! # Register the summary tag PuppetStrings::Yard::Tags::SummaryTag.register! + # Register the enum tag + PuppetStrings::Yard::Tags::EnumTag.register! + # Ignore documentation on Puppet DSL calls # This prevents the YARD DSL parser from emitting warnings for Puppet's Ruby DSL YARD::Handlers::Ruby::DSLHandlerMethods::IGNORE_METHODS['create_function'] = true diff --git a/lib/puppet-strings/yard/code_objects/function.rb b/lib/puppet-strings/yard/code_objects/function.rb index a0dd51f5c..121702151 100644 --- a/lib/puppet-strings/yard/code_objects/function.rb +++ b/lib/puppet-strings/yard/code_objects/function.rb @@ -88,10 +88,10 @@ def to_hash if self.has_tag? :overload # loop over overloads and append onto the signatures array self.tags(:overload).each do |o| - hash[:signatures] << { :signature => o.signature, :docstring => PuppetStrings::Yard::Util.docstring_to_hash(o.docstring, %i[param option return example]) } + hash[:signatures] << { :signature => o.signature, :docstring => PuppetStrings::Yard::Util.docstring_to_hash(o.docstring, %i[param option enum return example]) } end else - hash[:signatures] << { :signature => self.signature, :docstring => PuppetStrings::Yard::Util.docstring_to_hash(docstring, %i[param option return example]) } + hash[:signatures] << { :signature => self.signature, :docstring => PuppetStrings::Yard::Util.docstring_to_hash(docstring, %i[param option enum return example]) } end hash[:docstring] = PuppetStrings::Yard::Util.docstring_to_hash(docstring) diff --git a/lib/puppet-strings/yard/tags.rb b/lib/puppet-strings/yard/tags.rb index 211812cfa..b3b3a329d 100644 --- a/lib/puppet-strings/yard/tags.rb +++ b/lib/puppet-strings/yard/tags.rb @@ -1,7 +1,9 @@ # The module for custom YARD tags. module PuppetStrings::Yard::Tags + require 'puppet-strings/yard/tags/factory' require 'puppet-strings/yard/tags/parameter_directive' require 'puppet-strings/yard/tags/property_directive' require 'puppet-strings/yard/tags/overload_tag' require 'puppet-strings/yard/tags/summary_tag' + require 'puppet-strings/yard/tags/enum_tag' end diff --git a/lib/puppet-strings/yard/tags/enum_tag.rb b/lib/puppet-strings/yard/tags/enum_tag.rb new file mode 100644 index 000000000..3a55c4d84 --- /dev/null +++ b/lib/puppet-strings/yard/tags/enum_tag.rb @@ -0,0 +1,11 @@ +require 'yard/tags/option_tag' + +# Implements an enum tag for describing enumerated value data types + +class PuppetStrings::Yard::Tags::EnumTag < YARD::Tags::OptionTag + # Registers the tag with YARD. + # @return [void] + def self.register! + YARD::Tags::Library.define_tag("puppet.enum", :enum, :with_enums) + end +end diff --git a/lib/puppet-strings/yard/tags/factory.rb b/lib/puppet-strings/yard/tags/factory.rb new file mode 100644 index 000000000..518726ad2 --- /dev/null +++ b/lib/puppet-strings/yard/tags/factory.rb @@ -0,0 +1,16 @@ +require 'yard/tags/default_factory' +require 'puppet-strings/yard/tags/enum_tag' + +class PuppetStrings::Yard::Tags::Factory < YARD::Tags::DefaultFactory + + # Parses tag text and creates a new enum tag type. Modeled after + # the parse_tag_with_options method in YARD::Tags::DefaultFactory. + # + # @param tag_name the name of the tag to parse + # @param [String] text the raw tag text + # @return [Tag] a tag object with the tag_name, name, and nested Tag as type + def parse_tag_with_enums(tag_name, text) + name, text = *extract_name_from_text(text) + PuppetStrings::Yard::Tags::EnumTag.new(tag_name, name, parse_tag_with_name(tag_name, text)) + end +end diff --git a/lib/puppet-strings/yard/util.rb b/lib/puppet-strings/yard/util.rb index bb46eb3c6..b25ce97d4 100644 --- a/lib/puppet-strings/yard/util.rb +++ b/lib/puppet-strings/yard/util.rb @@ -38,11 +38,11 @@ def self.tags_to_hashes(tags) next t.to_hash if t.respond_to?(:to_hash) tag = { tag_name: t.tag_name } - # grab nested information for @option tags - if tag[:tag_name] == 'option' + # grab nested information for @option and @enum tags + if tag[:tag_name] == 'option' || tag[:tag_name] == 'enum' tag[:opt_name] = t.pair.name tag[:opt_text] = t.pair.text - tag[:opt_types] = t.pair.types + tag[:opt_types] = t.pair.types if t.pair.types tag[:parent] = t.name end tag[:text] = t.text if t.text diff --git a/spec/fixtures/unit/markdown/output.md b/spec/fixtures/unit/markdown/output.md index a48a1e25f..333df8636 100644 --- a/spec/fixtures/unit/markdown/output.md +++ b/spec/fixtures/unit/markdown/output.md @@ -101,6 +101,19 @@ Third param. Default value: 'hi' +##### `param4` + +Data type: `Enum['one', 'two']` + +Fourth param. + +Options: + +* **:one**: One option +* **:two**: Second option + +Default value: 'two' + ## Defined types ### klass::dt @@ -162,6 +175,19 @@ Fourth param. Default value: `true` +##### `param5` + +Data type: `Enum['a', 'b']` + +Fifth param. + +Options: + +* **:a**: Option A +* **:b**: Option B + +Default value: 'a' + ## Resource types ### apt_key @@ -238,6 +264,11 @@ Aliases: "up"=>"present", "down"=>"absent" What state the database should be in. +Options: + +* **:up**: Upstate +* **:down**: Downstate + Default value: up ##### `file` @@ -292,7 +323,7 @@ A simple Puppet function. $result = func(1, 2) ``` -#### `func(Integer $param1, Any $param2, String $param3 = hi)` +#### `func(Integer $param1, Any $param2, String $param3 = hi, Enum['yes', 'no'] $param4 = 'yes')` A simple Puppet function. @@ -331,6 +362,17 @@ Options: * **:param3opt** `Array`: Something about this option +##### `param4` + +Data type: `Enum['yes', 'no']` + +Fourth param. + +Options: + +* **:yes**: Yes option. +* **:no**: No option. + ### func3x Type: Ruby 3.x API @@ -391,7 +433,7 @@ $result = func4x(1, 'foo') $result = func4x(1, 'foo', ['bar']) ``` -#### `func4x(Integer $param1, Any $param2, Optional[Array[String]] $param3)` +#### `func4x(Integer $param1, Any $param2, Optional[Array[String]] $param3, Optional[Enum[one, two]] $param4)` An overview for the first overload. @@ -428,6 +470,17 @@ Data type: `Optional[Array[String]]` The third parameter. +##### `param4` + +Data type: `Optional[Enum[one, two]]` + +The fourth parameter. + +Options: + +* **:one**: Option one. +* **:two**: Option two. + #### `func4x(Boolean $param, Callable &$block)` An overview for the second overload. diff --git a/spec/fixtures/unit/markdown/output_with_data_types.md b/spec/fixtures/unit/markdown/output_with_data_types.md index e53416f96..013a5b2c1 100644 --- a/spec/fixtures/unit/markdown/output_with_data_types.md +++ b/spec/fixtures/unit/markdown/output_with_data_types.md @@ -107,6 +107,19 @@ Third param. Default value: 'hi' +##### `param4` + +Data type: `Enum['one', 'two']` + +Fourth param. + +Options: + +* **:one**: One option +* **:two**: Second option + +Default value: 'two' + ## Defined types ### klass::dt @@ -168,6 +181,19 @@ Fourth param. Default value: `true` +##### `param5` + +Data type: `Enum['a', 'b']` + +Fifth param. + +Options: + +* **:a**: Option A +* **:b**: Option B + +Default value: 'a' + ## Resource types ### apt_key @@ -244,6 +270,11 @@ Aliases: "up"=>"present", "down"=>"absent" What state the database should be in. +Options: + +* **:up**: Upstate +* **:down**: Downstate + Default value: up ##### `file` @@ -298,7 +329,7 @@ A simple Puppet function. $result = func(1, 2) ``` -#### `func(Integer $param1, Any $param2, String $param3 = hi)` +#### `func(Integer $param1, Any $param2, String $param3 = hi, Enum['yes', 'no'] $param4 = 'yes')` A simple Puppet function. @@ -337,6 +368,17 @@ Options: * **:param3opt** `Array`: Something about this option +##### `param4` + +Data type: `Enum['yes', 'no']` + +Fourth param. + +Options: + +* **:yes**: Yes option. +* **:no**: No option. + ### func3x Type: Ruby 3.x API @@ -397,7 +439,7 @@ $result = func4x(1, 'foo') $result = func4x(1, 'foo', ['bar']) ``` -#### `func4x(Integer $param1, Any $param2, Optional[Array[String]] $param3)` +#### `func4x(Integer $param1, Any $param2, Optional[Array[String]] $param3, Optional[Enum[one, two]] $param4)` An overview for the first overload. @@ -434,6 +476,17 @@ Data type: `Optional[Array[String]]` The third parameter. +##### `param4` + +Data type: `Optional[Enum[one, two]]` + +The fourth parameter. + +Options: + +* **:one**: Option one. +* **:two**: Option two. + #### `func4x(Boolean $param, Callable &$block)` An overview for the second overload. diff --git a/spec/fixtures/unit/markdown/output_with_plan.md b/spec/fixtures/unit/markdown/output_with_plan.md index cb15c309a..ec9660ca3 100644 --- a/spec/fixtures/unit/markdown/output_with_plan.md +++ b/spec/fixtures/unit/markdown/output_with_plan.md @@ -105,6 +105,19 @@ Third param. Default value: 'hi' +##### `param4` + +Data type: `Enum['one', 'two']` + +Fourth param. + +Options: + +* **:one**: One option +* **:two**: Second option + +Default value: 'two' + ## Defined types ### klass::dt @@ -166,6 +179,19 @@ Fourth param. Default value: `true` +##### `param5` + +Data type: `Enum['a', 'b']` + +Fifth param. + +Options: + +* **:a**: Option A +* **:b**: Option B + +Default value: 'a' + ## Resource types ### apt_key @@ -242,6 +268,11 @@ Aliases: "up"=>"present", "down"=>"absent" What state the database should be in. +Options: + +* **:up**: Upstate +* **:down**: Downstate + Default value: up ##### `file` @@ -296,7 +327,7 @@ A simple Puppet function. $result = func(1, 2) ``` -#### `func(Integer $param1, Any $param2, String $param3 = hi)` +#### `func(Integer $param1, Any $param2, String $param3 = hi, Enum['yes', 'no'] $param4 = 'yes')` A simple Puppet function. @@ -335,6 +366,17 @@ Options: * **:param3opt** `Array`: Something about this option +##### `param4` + +Data type: `Enum['yes', 'no']` + +Fourth param. + +Options: + +* **:yes**: Yes option. +* **:no**: No option. + ### func3x Type: Ruby 3.x API @@ -395,7 +437,7 @@ $result = func4x(1, 'foo') $result = func4x(1, 'foo', ['bar']) ``` -#### `func4x(Integer $param1, Any $param2, Optional[Array[String]] $param3)` +#### `func4x(Integer $param1, Any $param2, Optional[Array[String]] $param3, Optional[Enum[one, two]] $param4)` An overview for the first overload. @@ -432,6 +474,17 @@ Data type: `Optional[Array[String]]` The third parameter. +##### `param4` + +Data type: `Optional[Enum[one, two]]` + +The fourth parameter. + +Options: + +* **:one**: Option one. +* **:two**: Option two. + #### `func4x(Boolean $param, Callable &$block)` An overview for the second overload. diff --git a/spec/unit/puppet-strings/markdown_spec.rb b/spec/unit/puppet-strings/markdown_spec.rb index 8510f4528..04f16db1b 100644 --- a/spec/unit/puppet-strings/markdown_spec.rb +++ b/spec/unit/puppet-strings/markdown_spec.rb @@ -29,11 +29,15 @@ def parse_shared_content # @option param2 [String] :opt1 something about opt1 # @option param2 [Hash] :opt2 a hash of stuff # @param param3 Third param. +# @param param4 Fourth param. +# @enum param4 :one One option +# @enum param4 :two Second option # class klass ( Integer $param1 = 1, $param2 = undef, - String $param3 = 'hi' + String $param3 = 'hi', + Enum['one', 'two'] $param4 = 'two', ) inherits foo::bar { } @@ -58,11 +62,15 @@ class noparams () {} # @option param2 [Hash] :opt2 a hash of stuff # @param param3 Third param. # @param param4 Fourth param. +# @param param5 Fifth param. +# @enum param5 :a Option A +# @enum param5 :b Option B define klass::dt ( Integer $param1 = 44, $param2, String $param3 = 'hi', - Boolean $param4 = true + Boolean $param4 = true, + Enum['a', 'b'] $param5 = 'a' ) { } SOURCE @@ -98,11 +106,14 @@ class noparams () {} # @param param2 Second param. # @param param3 Third param. # @option param3 [Array] :param3opt Something about this option +# @param param4 Fourth param. +# @enum param4 :yes Yes option. +# @enum param4 :no No option. # @raise SomeError this is some error # @return [Undef] Returns nothing. # @example Test # $result = func(1, 2) -function func(Integer $param1, $param2, String $param3 = hi) { +function func(Integer $param1, $param2, String $param3 = hi, Enum['yes', 'no'] $param4 = 'yes') { } SOURCE @@ -122,6 +133,9 @@ class noparams () {} # @option param2 [String] :option an option # @option param2 [String] :option2 another option # @param param3 The third parameter. + # @param param4 The fourth parameter. + # @enum param4 :one Option one. + # @enum param4 :two Option two. # @return Returns nothing. # @example Calling the function foo # $result = func4x(1, 'foooo') @@ -130,6 +144,7 @@ class noparams () {} param 'Integer', :param1 param 'Any', :param2 optional_param 'Array[String]', :param3 + optional_param 'Enum[one, two]', :param4 return_type 'Undef' end @@ -183,6 +198,8 @@ class noparams () {} desc <<-DESC An example database server type. @option opts :foo bar +@enum ensure :up Upstate +@enum ensure :down Downstate @raise SomeError @example here's an example database { 'foo':