Skip to content

Commit e84c6c0

Browse files
committed
(maint) Simplifies SSLContext intialization
Ruby 2.5 added the `keyword_init: true` option to Struct.new to enable keyword arguments. This commit updates the Puppet::SSL::SSLContext to take advantage of this language feature and removes a workaround needed for older versions of Ruby. Additionally, Ruby 3.2 addeed the ability to initialize a Struct with keyword arguments without the `keyword_init: true` option. This commit adds a comment noting that, so it can be removed in the future when Puppet drops support for Ruby < 3.2.
1 parent fd7f09f commit e84c6c0

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

lib/puppet/ssl/ssl_context.rb

+11-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
require_relative '../../puppet/ssl'
22

33
module Puppet::SSL
4+
# Struct in Ruby >= 2.5 uses the `keyword_init: true` option to allow for keyword arguments. Ruby >= 3.2 allows Struct.new to use keyword
5+
# arguments without `keyword_init: true`
46
SSLContext = Struct.new(
57
:store,
68
:cacerts,
@@ -9,22 +11,16 @@ module Puppet::SSL
911
:client_cert,
1012
:client_chain,
1113
:revocation,
12-
:verify_peer
14+
:verify_peer,
15+
keyword_init: true
1316
) do
14-
DEFAULTS = {
15-
cacerts: [],
16-
crls: [],
17-
client_chain: [],
18-
revocation: true,
19-
verify_peer: true
20-
}.freeze
21-
22-
# This is an idiom to initialize a Struct from keyword
23-
# arguments. Ruby 2.5 introduced `keyword_init: true` for
24-
# that purpose, but we need to support older versions.
25-
def initialize(kwargs = {})
26-
super({})
27-
DEFAULTS.merge(**kwargs).each { |k,v| self[k] = v }
17+
def initialize(**)
18+
super
19+
self[:cacerts] ||= []
20+
self[:crls] ||= []
21+
self[:client_chain] ||= []
22+
self[:revocation] ||= true
23+
self[:verify_peer] ||= true
2824
end
2925
end
3026
end

0 commit comments

Comments
 (0)