Skip to content

Commit 3ed398f

Browse files
authored
Merge pull request #232 from scotje/229_add_checks_to_resource_types
Add checks to resource_type handler and code objects
2 parents cee0da1 + 926af93 commit 3ed398f

File tree

9 files changed

+86
-11
lines changed

9 files changed

+86
-11
lines changed

lib/puppet-strings/markdown/resource_type.rb

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,28 @@ def render
1212
end
1313

1414
def properties
15-
@registry[:properties]
15+
return nil unless @registry[:properties]
16+
17+
@registry[:properties].sort_by { |p| p[:name] }
18+
end
19+
20+
def checks
21+
return nil unless @registry[:checks]
22+
23+
@registry[:checks].sort_by { |p| p[:name] }
24+
end
25+
26+
# "checks" (such as "onlyif" or "creates") are another type of property
27+
def properties_and_checks
28+
return nil if properties.nil? && checks.nil?
29+
30+
((properties || []) + (checks || [])).sort_by { |p| p[:name] }
1631
end
1732

1833
def parameters
19-
@registry[:parameters]
34+
return nil unless @registry[:parameters]
35+
36+
@registry[:parameters].sort_by { |p| p[:name] }
2037
end
2138

2239
def regex_in_data_type?(data_type)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@
4444

4545
<% end -%>
4646
<% end -%>
47-
<% if properties -%>
47+
<% if properties_and_checks -%>
4848
#### Properties
4949

5050
The following properties are available in the `<%= name %>` <%= @type %>.
5151

52-
<% properties.each do |prop| -%>
52+
<% properties_and_checks.each do |prop| -%>
5353
##### `<%= prop[:name] %>`
5454

5555
<% if prop[:values] -%>

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

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ def to_hash
7373
class Property < Parameter
7474
end
7575

76+
class Check < Parameter
77+
end
78+
7679
# Represents a resource type feature.
7780
class Feature
7881
attr_reader :name, :docstring
@@ -95,7 +98,7 @@ def to_hash
9598
end
9699
end
97100

98-
attr_reader :properties, :features
101+
attr_reader :properties, :features, :checks
99102

100103
# Initializes a new resource type.
101104
# @param [String] name The resource type name.
@@ -134,6 +137,14 @@ def add_feature(feature)
134137
@features << feature
135138
end
136139

140+
# Adds a check to the resource type.
141+
# @param [PuppetStrings::Yard::CodeObjects::Type::Check] check The check to add.
142+
# @return [void]
143+
def add_check(check)
144+
@checks ||= []
145+
@checks << check
146+
end
147+
137148
def parameters
138149
# just return params if there are no providers
139150
return @parameters if providers.empty?
@@ -163,14 +174,18 @@ def providers
163174
# @return [Hash] Returns a hash representation of the code object.
164175
def to_hash
165176
hash = {}
177+
166178
hash[:name] = name
167179
hash[:file] = file
168180
hash[:line] = line
169-
hash[:docstring] = PuppetStrings::Yard::Util.docstring_to_hash(docstring)
170-
hash[:properties] = properties.map(&:to_hash) if properties && !properties.empty?
171-
hash[:parameters] = parameters.map(&:to_hash) if parameters && !parameters.empty?
172-
hash[:features] = features.map(&:to_hash) if features && !features.empty?
173-
hash[:providers] = self.providers.map(&:to_hash) if providers && !providers.empty?
181+
182+
hash[:docstring] = PuppetStrings::Yard::Util.docstring_to_hash(docstring)
183+
hash[:properties] = properties.sort_by { |p| p.name }.map(&:to_hash) if properties && !properties.empty?
184+
hash[:parameters] = parameters.sort_by { |p| p.name }.map(&:to_hash) if parameters && !parameters.empty?
185+
hash[:checks] = checks.sort_by { |c| c.name }.map(&:to_hash) if checks && !checks.empty?
186+
hash[:features] = features.sort_by { |f| f.name }.map(&:to_hash) if features && !features.empty?
187+
hash[:providers] = providers.sort_by { |p| p.name }.map(&:to_hash) if providers && !providers.empty?
188+
174189
hash
175190
end
176191
end

lib/puppet-strings/yard/handlers/ruby/type_base.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ def create_property(name, node)
5252
property
5353
end
5454

55+
def create_check(name, node)
56+
check = PuppetStrings::Yard::CodeObjects::Type::Check.new(name, find_docstring(node, "Puppet resource check '#{name}'"))
57+
set_values(node, check)
58+
check
59+
end
60+
5561
def set_values(node, object)
5662
return unless node.block && node.block.count >= 2
5763

lib/puppet-strings/yard/handlers/ruby/type_handler.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ def populate_type_data(object)
5959
name = node_as_string(parameters[0])
6060
next unless name
6161
object.add_parameter(create_parameter(name, node))
62+
elsif method_name == 'newcheck'
63+
# Add a check to the object
64+
next unless parameters.count >= 1
65+
name = node_as_string(parameters[0])
66+
next unless name
67+
object.add_check(create_check(name, node))
6268
elsif method_name == 'feature'
6369
# Add a feature to the object
6470
next unless parameters.count >= 2

lib/puppet-strings/yard/templates/default/puppet_type/html/setup.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ def box_info
1616
def properties
1717
# Properties are the same thing as parameters (from the documentation standpoint),
1818
# so reuse the same template but with a different title and data source.
19-
@parameters = object.properties || []
19+
#
20+
# "checks" such as "creates" and "onlyif" are another type of property
21+
@parameters = (object.properties || []) + (object.checks || [])
2022
@parameters.sort_by! { |p| p.name }
2123
@tag_title = 'Properties'
2224
erb(:parameters)

spec/fixtures/ruby/resource_type.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,8 @@
4444
newvalue(:error)
4545
defaultto 'warn'
4646
end
47+
48+
newcheck(:exists) do
49+
desc 'Check to see if the database already exists'
50+
end
4751
end

spec/unit/puppet-strings/markdown_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ def parse_data_type_content
8484
end
8585
end
8686

87+
describe 'resource types' do
88+
it 'includes checks in parameter list for the database type' do
89+
expect(output).to match(/check to see if the database already exists/i)
90+
end
91+
end
92+
8793
describe 'with Puppet Plans', :if => TEST_PUPPET_PLANS do
8894
before(:each) do
8995
parse_plan_content

spec/unit/puppet-strings/yard/handlers/ruby/type_handler_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,25 @@
278278
end
279279
end
280280

281+
describe 'parsing a type with a check with the name of "onlyif"' do
282+
let(:source) { <<-SOURCE
283+
Puppet::Type.newtype(:testexec) do
284+
desc 'An example exec type with a check.'
285+
newcheck(:onlyif) do
286+
desc 'a test check param'
287+
end
288+
end
289+
SOURCE
290+
}
291+
292+
it 'should register a check object on the parent type object' do
293+
expect(subject.size).to eq(1)
294+
type_object = subject.first
295+
expect(type_object.checks.size).to eq(1)
296+
expect(type_object.checks[0].name).to eq('onlyif')
297+
end
298+
end
299+
281300
describe 'parsing a type with a summary' do
282301
context 'when the summary has fewer than 140 characters' do
283302
let(:source) { <<-SOURCE

0 commit comments

Comments
 (0)