Skip to content

Commit aa41b45

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 65f30af commit aa41b45

File tree

1 file changed

+10
-15
lines changed

1 file changed

+10
-15
lines changed

lib/puppet/ssl/ssl_context.rb

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require_relative '../../puppet/ssl'
22

33
module Puppet::SSL
4+
# The `keyword_init: true` option is no longer needed in Ruby >= 3.2
45
SSLContext = Struct.new(
56
:store,
67
:cacerts,
@@ -9,22 +10,16 @@ module Puppet::SSL
910
:client_cert,
1011
:client_chain,
1112
:revocation,
12-
:verify_peer
13+
:verify_peer,
14+
keyword_init: true
1315
) 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 }
16+
def initialize(*)
17+
super
18+
self[:cacerts] ||= []
19+
self[:crls] ||= []
20+
self[:client_chain] ||= []
21+
self[:revocation] = true if self[:revocation].nil?
22+
self[:verify_peer] = true if self[:verify_peer].nil?
2823
end
2924
end
3025
end

0 commit comments

Comments
 (0)