Skip to content

(GH-225) Document functions in Puppet Datatypes #235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions lib/puppet-strings/markdown/data_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,31 @@ module PuppetStrings::Markdown
# This class encapsualtes ruby data types and puppet type aliases
class DataType < Base
attr_reader :alias_of
attr_reader :functions

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

def render
super(@template)
end
end

class DataType::Function < Base
def initialize(registry)
super(registry, 'data_type_function')
end

def render
super('data_type_function.erb')
end

def signature
@registry[:signature]
end
end
end
7 changes: 7 additions & 0 deletions lib/puppet-strings/markdown/templates/data_type.erb
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,10 @@ Default value: <%= value_string(defaults[param[:name]]) %>
<% end -%>
<% end -%>
<% end -%>
<% if functions -%>
#### Functions

The following functions are available in the `<%= name %>` <%= @type %>.

<% functions.each do |func| -%><%= func.render -%><% end -%>
<% end -%>
67 changes: 67 additions & 0 deletions lib/puppet-strings/markdown/templates/data_type_function.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
### <%= name %>

#### `<%= signature %>`

<% if text -%>
<%= text %>

<% elsif summary -%>
<%= summary %>

<% else -%>
<%= "The #{name} function." %>

<% end -%>
<% if note -%>
* **Note** <%= note %>

<% end -%>
<% if return_type -%>
Returns: `<%= return_type %>`<% if return_val %> <%= return_val %><% end %>

<% end -%>
<% if raises -%>
Raises:
<% raises.each do |r| -%>
* <%= error_type(r[:text]) %> <%= error_text(r[:text]) %>
<% end -%>

<% end -%>
<% if examples -%>
##### Examples

<% examples.each do |eg| -%>
###### <%= eg[:name] %>

```puppet
<%= eg[:text] %>
```

<% end -%>
<% end -%>
<% if params -%>
<% params.each do |param| -%>
##### `<%= param[:name] %>`

Data type: `<%= param[:types][0] %>`

<%= param[:text] %>

<% if options_for_param(param[:name]) -%>
Options:

<% options_for_param(param[:name]).each do |o| -%>
* **<%= 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 -%>
<% end -%>
<% end -%>
32 changes: 26 additions & 6 deletions lib/puppet-strings/yard/code_objects/data_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class PuppetStrings::Yard::CodeObjects::DataType < PuppetStrings::Yard::CodeObje
# @return [void]
def initialize(name)
super(PuppetStrings::Yard::CodeObjects::DataTypes.instance, name)
@parameters = []
@defaults = {}
end

Expand All @@ -41,10 +40,6 @@ def source
nil
end

def parameter_exist?(name)
!docstring.tags(:param).find { |item| item.name == name }.nil?
end

def add_parameter(name, type, default)
tag = docstring.tags(:param).find { |item| item.name == name }
if tag.nil?
Expand All @@ -65,16 +60,41 @@ def parameters
docstring.tags(:param).map { |tag| [tag.name, defaults[tag.name]] }
end

def add_function(name, return_type, parameter_types)
meth_obj = YARD::CodeObjects::MethodObject.new(self, name, :class)

# Add return tag
meth_obj.add_tag(YARD::Tags::Tag.new(:return, '', return_type))

# Add parameters
parameter_types.each_with_index do |param_type, index|
meth_obj.add_tag(YARD::Tags::Tag.new(:param, '', [param_type], "param#{index + 1}"))
end

self.meths << meth_obj
end

def functions
meths
end

# Converts the code object to a hash representation.
# @return [Hash] Returns a hash representation of the code object.
def to_hash
hash = {}
hash[:name] = name
hash[:file] = file
hash[:line] = line
hash[:docstring] = PuppetStrings::Yard::Util.docstring_to_hash(docstring)
hash[:docstring] = PuppetStrings::Yard::Util.docstring_to_hash(docstring, %i[param option enum return example])
hash[:defaults] = defaults unless defaults.empty?
hash[:source] = source unless source && source.empty?
hash[:functions] = functions.map do |func|
{
name: func.name,
signature: func.signature,
docstring: PuppetStrings::Yard::Util.docstring_to_hash(func.docstring, %i[param option enum return example])
}
end
hash
end
end
Loading