Skip to content

Commit a57e1a5

Browse files
committed
Make URI friendly to Ractor
1 parent 2ec303b commit a57e1a5

File tree

12 files changed

+51
-13
lines changed

12 files changed

+51
-13
lines changed

lib/uri.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
# class RSYNC < Generic
3131
# DEFAULT_PORT = 873
3232
# end
33-
# @@schemes['RSYNC'] = RSYNC
33+
# register_scheme('RSYNC', RSYNC)
3434
# end
3535
# #=> URI::RSYNC
3636
#

lib/uri/common.rb

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ module URI
1717
Parser = RFC2396_Parser
1818
RFC3986_PARSER = RFC3986_Parser.new
1919

20+
Ractor.make_shareable(RFC3986_PARSER) if defined?(Ractor)
21+
2022
# URI::Parser.new
2123
DEFAULT_PARSER = Parser.new
2224
DEFAULT_PARSER.pattern.each_pair do |sym, str|
@@ -27,6 +29,7 @@ module URI
2729
DEFAULT_PARSER.regexp.each_pair do |sym, str|
2830
const_set(sym, str)
2931
end
32+
Ractor.make_shareable(DEFAULT_PARSER) if defined?(Ractor)
3033

3134
module Util # :nodoc:
3235
def make_components_hash(klass, array_hash)
@@ -62,10 +65,26 @@ def make_components_hash(klass, array_hash)
6265

6366
include REGEXP
6467

65-
@@schemes = {}
68+
SCHEMES = {}.freeze
69+
private_constant :SCHEMES
70+
6671
# Returns a Hash of the defined schemes.
6772
def self.scheme_list
68-
@@schemes
73+
SCHEMES
74+
end
75+
76+
# Registers a new scheme when adding custom URIs.
77+
# Example:
78+
# module URI
79+
# class RSYNC < Generic
80+
# DEFAULT_PORT = 873
81+
# end
82+
# register_scheme('RSYNC', RSYNC)
83+
# end
84+
def self.register_scheme(name, mod)
85+
updated = SCHEMES.merge("#{name}" => mod).freeze
86+
remove_const(:SCHEMES)
87+
const_set(:SCHEMES, updated)
6988
end
7089

7190
#
@@ -74,7 +93,7 @@ def self.scheme_list
7493
#
7594
def self.for(scheme, *arguments, default: Generic)
7695
if scheme
77-
uri_class = @@schemes[scheme.upcase] || default
96+
uri_class = SCHEMES[scheme.upcase] || default
7897
else
7998
uri_class = default
8099
end

lib/uri/file.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,5 @@ def set_password(v)
9090
end
9191
end
9292

93-
@@schemes['FILE'] = File
93+
register_scheme('FILE', File)
9494
end

lib/uri/ftp.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,5 +262,5 @@ def to_s
262262
return str
263263
end
264264
end
265-
@@schemes['FTP'] = FTP
265+
register_scheme('FTP', FTP)
266266
end

lib/uri/http.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,6 @@ def request_uri
8282
end
8383
end
8484

85-
@@schemes['HTTP'] = HTTP
85+
register_scheme('HTTP', HTTP)
8686

8787
end

lib/uri/https.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ class HTTPS < HTTP
1818
# A Default port of 443 for URI::HTTPS
1919
DEFAULT_PORT = 443
2020
end
21-
@@schemes['HTTPS'] = HTTPS
21+
register_scheme('HTTPS', HTTPS)
2222
end

lib/uri/ldap.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,5 +257,5 @@ def hierarchical?
257257
end
258258
end
259259

260-
@@schemes['LDAP'] = LDAP
260+
register_scheme('LDAP', LDAP)
261261
end

lib/uri/ldaps.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ class LDAPS < LDAP
1717
# A Default port of 636 for URI::LDAPS
1818
DEFAULT_PORT = 636
1919
end
20-
@@schemes['LDAPS'] = LDAPS
20+
register_scheme('LDAPS', LDAPS)
2121
end

lib/uri/mailto.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,5 +289,5 @@ def to_mailtext
289289
alias to_rfc822text to_mailtext
290290
end
291291

292-
@@schemes['MAILTO'] = MailTo
292+
register_scheme('MAILTO', MailTo)
293293
end

lib/uri/ws.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,6 @@ def request_uri
7979
end
8080
end
8181

82-
@@schemes['WS'] = WS
82+
register_scheme('WS', WS)
8383

8484
end

0 commit comments

Comments
 (0)