Merge pull request #111 from whopper/PDOC-95/master/multiple_defaultfor

(PDOC-95) Properly group and display multiple provider `defaultfor`s
This commit is contained in:
Peter Huene 2016-10-11 15:33:39 -07:00 committed by GitHub
commit 4f3f1e639e
8 changed files with 85 additions and 28 deletions

28
JSON.md
View File

@ -3,7 +3,7 @@ Puppet Strings JSON Data
Puppet Strings has two flags to the `generate` action that can be used to emit JSON data: Puppet Strings has two flags to the `generate` action that can be used to emit JSON data:
* `--emit-json <file>`: Emits the JSON data to the given file. * `--emit-json <file>`: Emits the JSON data to the given file.
* `--emit-json-stdout`: Emits the JSON data to STDOUT. * `--emit-json-stdout`: Emits the JSON data to STDOUT.
Document Schema Document Schema
@ -100,13 +100,13 @@ Each entry in the `providers` list is an object with the following attributes:
| Attribute Key | Description | | Attribute Key | Description |
| ------------- | ---------------------------------------------------- | | ------------- | ---------------------------------------------------- |
| name | The name of the provider. | | name | The name of the provider. |
| type_name | The name of the resource type of the provider. | | type_name | The name of the resource type of the provider. |
| file | The file defining the provider. | | file | The file defining the provider. |
| line | The line where the provider is defined. | | line | The line where the provider is defined. |
| docstring | The *DocString* object for the provider (see below). | | docstring | The *DocString* object for the provider (see below). |
| confines | The string map of confines for the provider. | | confines | The string map of confines for the provider. |
| features | The list of features implemented by the provider. | | features | The list of features implemented by the provider. |
| defaults | The string map of "default for" for the provider. | | defaults | The list of lists of "default for" for the provider. |
| commands | The string map of commands for the provider. | | commands | The string map of commands for the provider. |
Puppet Functions Puppet Functions
@ -327,9 +327,24 @@ An example JSON document describing a Puppet class, defined type, resource type,
"implements_some_feature", "implements_some_feature",
"some_other_feature" "some_other_feature"
], ],
"defaults": { "defaults": [
"kernel": "Linux" [
}, [
"kernel",
"Linux"
]
],
[
[
"osfamily",
"RedHat",
],
[
"operatingsystemmajrelease",
"7"
]
]
],
"commands": { "commands": {
"foo": "/usr/bin/foo" "foo": "/usr/bin/foo"
} }
@ -508,4 +523,3 @@ An example JSON document describing a Puppet class, defined type, resource type,
] ]
} }
``` ```

View File

@ -56,13 +56,12 @@ class PuppetStrings::Yard::CodeObjects::Provider < PuppetStrings::Yard::CodeObje
end end
# Adds a default to the provider. # Adds a default to the provider.
# @param [String] key The default's key. # @param [Array] constraints List of related key-pair values for the default.
# @param [String] value The default's value.
# @return [void] # @return [void]
def add_default(key, value) def add_default(constraints)
return unless key && value return unless constraints
@defaults ||= {} @defaults ||= []
@defaults[key] = value @defaults << constraints
end end
# Adds a command to the provider. # Adds a command to the provider.

View File

@ -96,9 +96,14 @@ class PuppetStrings::Yard::Handlers::Ruby::ProviderHandler < PuppetStrings::Yard
elsif method_name == 'defaultfor' elsif method_name == 'defaultfor'
# Add a default to the object # Add a default to the object
next unless parameters.count >= 1 next unless parameters.count >= 1
parameters[0].each do |kvp| # Some defaultfor statements contain multiple constraints.
next unless kvp.count == 2 parameters.each do |kvps|
object.add_default(node_as_string(kvp[0]) || kvp[0].source, node_as_string(kvp[1]) || kvp[1].source) next unless kvps.count >= 1
defaultfor = []
kvps.each do |kvp|
defaultfor << [node_as_string(kvp[0]) || kvp[0].source, node_as_string(kvp[1]) || kvp[1].source]
end
object.add_default(defaultfor)
end end
elsif method_name == 'commands' elsif method_name == 'commands'
# Add the commands to the object # Add the commands to the object

View File

@ -2,8 +2,15 @@
<div class="tags"> <div class="tags">
<p class="tag_title"><%= @title %></p> <p class="tag_title"><%= @title %></p>
<ul> <ul>
<% @collection.each do |key, value| %>
<li><tt><%= key %> &mdash; <%= value %></tt></li> <% if @collection.is_a?(Hash) %>
<% @collection.each do |key, value| %>
<li><tt><%= key %> &mdash; <%= value %></tt></li>
<% end %>
<% elsif @collection.is_a?(Array) %>
<% @collection.each do |kvps| %>
<li><tt><%= kvps.map{|k,v| "#{k} &mdash; #{v}"}.join(', ') %></tt></li>
<% end %>
<% end %> <% end %>
</ul> </ul>
</div> </div>

View File

@ -84,7 +84,7 @@
{ {
"name": "database", "name": "database",
"file": "(stdin)", "file": "(stdin)",
"line": 43, "line": 44,
"docstring": { "docstring": {
"text": "An example database server resource type." "text": "An example database server resource type."
}, },
@ -166,9 +166,24 @@
"implements_some_feature", "implements_some_feature",
"some_other_feature" "some_other_feature"
], ],
"defaults": { "defaults": [
"kernel": "Linux" [
}, [
"kernel",
"Linux"
]
],
[
[
"osfamily",
"RedHat"
],
[
"operatingsystemmajrelease",
"7"
]
]
],
"commands": { "commands": {
"foo": "/usr/bin/foo" "foo": "/usr/bin/foo"
} }

View File

@ -166,9 +166,24 @@
"implements_some_feature", "implements_some_feature",
"some_other_feature" "some_other_feature"
], ],
"defaults": { "defaults": [
"kernel": "Linux" [
}, [
"kernel",
"Linux"
]
],
[
[
"osfamily",
"RedHat"
],
[
"operatingsystemmajrelease",
"7"
]
]
],
"commands": { "commands": {
"foo": "/usr/bin/foo" "foo": "/usr/bin/foo"
} }

View File

@ -69,7 +69,8 @@ Puppet::Type.type(:database).provide :linux do
desc 'An example provider on Linux.' desc 'An example provider on Linux.'
confine kernel: 'Linux' confine kernel: 'Linux'
confine osfamily: 'RedHat' confine osfamily: 'RedHat'
defaultfor kernel: 'Linux' defaultfor :kernel => 'Linux'
defaultfor :osfamily => 'RedHat', :operatingsystemmajrelease => '7'
has_feature :implements_some_feature has_feature :implements_some_feature
has_feature :some_other_feature has_feature :some_other_feature
commands foo: /usr/bin/foo commands foo: /usr/bin/foo

View File

@ -61,7 +61,8 @@ Puppet::Type.type(:custom).provide :linux do
desc 'An example provider on Linux.' desc 'An example provider on Linux.'
confine kernel: 'Linux' confine kernel: 'Linux'
confine osfamily: 'RedHat' confine osfamily: 'RedHat'
defaultfor kernel: 'Linux' defaultfor :kernel => 'Linux'
defaultfor :osfamily => 'RedHat', :operatingsystemmajrelease => '7'
has_feature :implements_some_feature has_feature :implements_some_feature
has_feature :some_other_feature has_feature :some_other_feature
commands foo: /usr/bin/foo commands foo: /usr/bin/foo
@ -82,7 +83,7 @@ SOURCE
expect(tags.size).to eq(1) expect(tags.size).to eq(1)
expect(tags[0].text).to eq('public') expect(tags[0].text).to eq('public')
expect(object.confines).to eq({ 'kernel' => 'Linux', 'osfamily' => 'RedHat'}) expect(object.confines).to eq({ 'kernel' => 'Linux', 'osfamily' => 'RedHat'})
expect(object.defaults).to eq({ 'kernel' => 'Linux'}) expect(object.defaults).to eq([[["kernel", "Linux"]], [["osfamily", "RedHat"], ["operatingsystemmajrelease", "7"]]])
expect(object.features).to eq(['implements_some_feature', 'some_other_feature']) expect(object.features).to eq(['implements_some_feature', 'some_other_feature'])
expect(object.commands).to eq({'foo' => '/usr/bin/foo'}) expect(object.commands).to eq({'foo' => '/usr/bin/foo'})
end end