Skip to content

Commit 242aefd

Browse files
authored
Merge pull request #281 from joshcooper/file_ensurable
Add support for ensurable in types_extras_handler
2 parents 7f96be1 + 2816281 commit 242aefd

File tree

2 files changed

+82
-3
lines changed

2 files changed

+82
-3
lines changed

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
require 'puppet-strings/yard/code_objects'
66
require 'puppet-strings/yard/util'
77

8-
# Implements the handler for Puppet resource type newparam/newproperty calls written in Ruby.
8+
# Implements the handler for Puppet resource type newparam/newproperty/ensurable calls written in Ruby.
99
class PuppetStrings::Yard::Handlers::Ruby::TypeExtrasHandler < PuppetStrings::Yard::Handlers::Ruby::TypeBase
1010
# The default docstring when ensurable is used without given a docstring.
1111
DEFAULT_ENSURABLE_DOCSTRING = 'The basic property that the resource should be in.'
1212

1313
namespace_only
1414
handles method_call(:newparam)
1515
handles method_call(:newproperty)
16+
handles method_call(:ensurable)
1617

1718
process do
1819

@@ -38,9 +39,15 @@ class PuppetStrings::Yard::Handlers::Ruby::TypeExtrasHandler < PuppetStrings::Ya
3839
method1_name = statement[0].children.drop(1).find{ |c| c.type == :ident }.source
3940
return unless ['Type', 'Puppet::Type'].include?(module_name) && method1_name == 'type'
4041

42+
# ensurable is syntatic sugar for newproperty
4143
typename = get_name(statement[0], 'Puppet::Type.type')
42-
method2_name = caller_method
43-
propertyname = get_name(statement, "Puppet::Type.type().#{method2_name}")
44+
if caller_method == 'ensurable'
45+
method2_name = 'newproperty'
46+
propertyname = 'ensure'
47+
else
48+
method2_name = caller_method
49+
propertyname = get_name(statement, "Puppet::Type.type().#{method2_name}")
50+
end
4451

4552
typeobject = get_type_yard_object(typename)
4653

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
require 'puppet-strings/yard'
5+
6+
describe PuppetStrings::Yard::Handlers::Ruby::TypeExtrasHandler do
7+
subject {
8+
YARD::Parser::SourceParser.parse_string(source, :ruby)
9+
YARD::Registry.all(:puppet_type)
10+
}
11+
12+
describe 'parsing source with newproperty' do
13+
let(:source) { <<~SOURCE
14+
Puppet::Type.newtype(:database) do
15+
desc 'database'
16+
end
17+
Puppet::Type.type(:database).newproperty(:file) do
18+
desc 'The database file to use.'
19+
end
20+
SOURCE
21+
}
22+
23+
it 'generates a doc string for a property' do
24+
expect(subject.size).to eq(1)
25+
object = subject.first
26+
expect(object.properties.size).to eq(1)
27+
expect(object.properties[0].name).to eq('file')
28+
expect(object.properties[0].docstring).to eq('The database file to use.')
29+
end
30+
end
31+
32+
describe 'parsing source with newparam' do
33+
let(:source) { <<~SOURCE
34+
Puppet::Type.newtype(:database) do
35+
desc 'database'
36+
end
37+
Puppet::Type.type(:database).newparam(:name) do
38+
desc 'The database server name.'
39+
end
40+
SOURCE
41+
}
42+
43+
it 'generates a doc string for a parameter that is also a namevar' do
44+
expect(subject.size).to eq(1)
45+
object = subject.first
46+
expect(object.parameters.size).to eq(1)
47+
expect(object.parameters[0].name).to eq('name')
48+
expect(object.parameters[0].docstring).to eq('The database server name.')
49+
expect(object.parameters[0].isnamevar).to eq(true)
50+
end
51+
end
52+
53+
describe 'parsing source with ensurable' do
54+
let(:source) { <<~SOURCE
55+
Puppet::Type.newtype(:database) do
56+
desc 'database'
57+
end
58+
Puppet::Type.type(:database).ensurable do
59+
desc 'What state the database should be in.'
60+
end
61+
SOURCE
62+
}
63+
64+
it 'generates a doc string for an ensurable' do
65+
expect(subject.size).to eq(1)
66+
object = subject.first
67+
expect(object.properties.size).to eq(1)
68+
expect(object.properties[0].name).to eq('ensure')
69+
expect(object.properties[0].docstring).to eq('What state the database should be in.')
70+
end
71+
end
72+
end

0 commit comments

Comments
 (0)