ruby3-backward-compatibility 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d949727b9c8b236fcf673511dd789fb18730bc77dad9ea7a9eeedeaad1614521
4
- data.tar.gz: '0488f263cc00e3483b14634722ee893bb8ec4729cac1329b63a53d825b24258f'
3
+ metadata.gz: 4551e0985507c3ab5633b97825f059f34962dcfea27dfdb5ad9a3fe7d15a19f6
4
+ data.tar.gz: c211c8d15240c592d3ab2fd355f76dfb58ed88b548129e951b23764c577a9d51
5
5
  SHA512:
6
- metadata.gz: fb554d8ab1435ef27db945530b579b7954544b13a2a8b281d9ce7429bf5bc059f95c30850726fdd5ed289ecc1c1f7676fbd1d1d44575dc9bfa29733e5e7a9e09
7
- data.tar.gz: b5dc1372bb36f7eb3bbae7ddaab3d9094e1f480270cb837a3ac50661893d727e4ea8681c514fe75ee2db2538c45023b707d2e57c4cff77ae6c92c9d315565b4b
6
+ metadata.gz: a79863218e982c240626e13c6276e0cf1afd5a5bbffc823e427f75e126534c211efa94c5947cc253bc9e4011fe2a2bf1729d7e0ed37b3ac244353c22bd1fa115
7
+ data.tar.gz: c9f29e66e4162bfc9a8b0afee0e162dacca15eb2158cd51ba5e92cf01c0ba7c33e27255c485ac88257afa715af16823279f078cbf96cca03e5ea17995ff434d5
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --force-color
3
+ --require spec_helper
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.1.2
data/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.2] - 2022-10-25
4
+
5
+ - `ruby3_keywords` does not change visibility of private or protected methods.
6
+
7
+ ## [0.1.1] - 2022-10-25
8
+
9
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in ruby3-backward-compatibility.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 13.0'
9
+
10
+ gem 'rspec', '~> 3.0'
data/Gemfile.lock ADDED
@@ -0,0 +1,34 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ruby3-backward-compatibility (0.1.2)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.5.0)
10
+ rake (13.0.6)
11
+ rspec (3.11.0)
12
+ rspec-core (~> 3.11.0)
13
+ rspec-expectations (~> 3.11.0)
14
+ rspec-mocks (~> 3.11.0)
15
+ rspec-core (3.11.0)
16
+ rspec-support (~> 3.11.0)
17
+ rspec-expectations (3.11.0)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.11.0)
20
+ rspec-mocks (3.11.1)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.11.0)
23
+ rspec-support (3.11.0)
24
+
25
+ PLATFORMS
26
+ x86_64-linux
27
+
28
+ DEPENDENCIES
29
+ rake (~> 13.0)
30
+ rspec (~> 3.0)
31
+ ruby3-backward-compatibility!
32
+
33
+ BUNDLED WITH
34
+ 2.3.22
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Tobias Kraze
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,191 @@
1
+ # ruby3-backwards-compatibility
2
+
3
+ This gem provides a compatibility layer to Ruby 3 projects that still have legacy code written against Ruby 2's stdlib. Only use this if you upgrade to Ruby 3 but still depend on some third-party code that breaks on Ruby 3.
4
+
5
+ The gem will fix only some (but the most common) incompatibilities.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'ruby3-backward-compatibility'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install ruby3-backward-compatibility
22
+
23
+ ## Usage
24
+
25
+ You need to require the specific backports yourself, see below.
26
+
27
+ You can also require all included backports using
28
+
29
+ ```
30
+ require 'ruby3-backward-compatibility/compatibility/all'
31
+ ```
32
+
33
+
34
+ ## List of backports
35
+
36
+ ### ruby3 keyword arguments
37
+
38
+ One breaking change in Ruby 3 is that methods defined using keyword arguments have to be called with keyword arguments, not with option hashes. For example
39
+
40
+ ```
41
+ class Ruby3Class
42
+ def some_method(foo:)
43
+ puts foo
44
+ end
45
+ end
46
+
47
+ Ruby3Class.new.some_method(foo: 'bar') # works
48
+ Ruby3Class.new.some_method({ foo: 'bar' }) # raises an ArgumentError
49
+ ```
50
+
51
+ To fix this for arbitrary methods, you can use the `ruby3_keywords` method included in this gem, like this:
52
+
53
+ ```
54
+ require 'ruby3_backward_compatibility'
55
+
56
+ class Ruby3Class
57
+ # reopen the class
58
+
59
+ extend Ruby3BackwardCompatibility::Ruby3Keywords
60
+
61
+ ruby3_keywords :some_method
62
+ end
63
+
64
+ Ruby3Class.new.some_method(foo: 'bar') # still works
65
+ Ruby3Class.new.some_method({ foo: 'bar' }) # now works as well
66
+ ```
67
+
68
+ This will wrap the given method and convert a hash given as the last argument into keywords, similar to how it worked in Ruby 2.
69
+
70
+ ### ERB
71
+
72
+ `ERB.new` used to have the signature
73
+
74
+ ```
75
+ ERB.new(string, safe_level, trim_mode, eoutvar = '_erbout')
76
+ ```
77
+
78
+ but this was changed to
79
+
80
+ ```
81
+ ERB.new(string, safe_level: nil, trim_mode: nil, eoutvar: '_erbout')
82
+ ```
83
+
84
+ To allow both styles, use
85
+
86
+ ```
87
+ require 'ruby3_backward_compatibility/compatibility/erb'
88
+ ```
89
+
90
+
91
+ ### Object
92
+
93
+ The methods `Object#taint` and `Object#untaint` were no-ops for a while but started to raise deprecation warnings.
94
+
95
+ To add them back as no-ops, use
96
+
97
+ ```
98
+ require 'ruby3_backward_compatibility/compatibility/object'
99
+ ```
100
+
101
+
102
+ ### YAML (Psych)
103
+
104
+ Psych version 4 (default for Ruby 3.1) has two changes:
105
+ - `Psych.load` has been renamed to `Psych.unsafe_load`
106
+ - The signature of `Psych.safe_load` has been changed from
107
+
108
+ ```
109
+ Psych.safe_load(yaml, permitted_classes = [], permitted_symbols = [], aliases = false, filename = nil)
110
+ ```
111
+
112
+ to
113
+
114
+ ```
115
+ Psych.safe_load(yaml, permitted_classes: [], permitted_symbols: [], aliases: false, filename: nil)
116
+ ```
117
+
118
+ To alias `Psych.unsafe_load` as `Psych.load`, and to allow both styles of calling `Psych.safe_load`, use
119
+
120
+ ```
121
+ require 'ruby3_backward_compatibility/psych'
122
+ ```
123
+
124
+ **Attention:** There has been a very good reason why Psych renamed the `.load` method: You may never use `.load` on any external strings. It is possible to create valid YAML strings that lead to the execution of arbitrary code, so calling `YAML.load` on user input is a major security vulnerability.
125
+
126
+
127
+ ### String
128
+
129
+ `String#encode` require keyword arguments. To allow it to be called with an options hash, use
130
+
131
+ ```
132
+ require 'ruby3_backward_compatibility/compatibility/string'
133
+ ```
134
+
135
+
136
+ ### URI
137
+
138
+ The `URI` module allows other libraries to register their own URI schemes. In the past, it was possible to do something like
139
+
140
+ ```
141
+ module URI
142
+ @schemes['MYSCHEME'] = MySchemeImplementation
143
+ end
144
+ ```
145
+
146
+ In Ruby 3, you have to use
147
+
148
+ ```
149
+ URI.register_scheme 'MYSCHEME', MySchemeImplementation
150
+ ```
151
+
152
+ To add back the old behavior, use
153
+
154
+ ```
155
+ require 'ruby3_backward_compatibility/compatibility/uri'
156
+ ```
157
+
158
+
159
+ ## Things we cannot fix
160
+
161
+ ### Proc.new
162
+
163
+ In Ruby 2, it was possible to use `Proc.new` without giving a block. In this case, the Proc took the block from the current context. A common usecase worked like this:
164
+
165
+ ```
166
+ def call_me(block = Proc.new)
167
+ block.call
168
+ end
169
+
170
+ block = proc do
171
+ puts "called"
172
+ end
173
+
174
+ call_me(block) # works
175
+ call_me(&block) # also works
176
+ call_me { puts "called" } # also works
177
+ ```
178
+
179
+ This is no longer possible in Ruby 3 and we are not aware of a way to make a generic port for this.
180
+
181
+
182
+
183
+ ## Development
184
+
185
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
186
+
187
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
188
+
189
+ ## License
190
+
191
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'ruby3_backward_compatibility'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,7 @@
1
+ require 'ruby3_backward_compatibility'
2
+
3
+ require 'ruby3_backward_compatibility/compatibility/erb'
4
+ require 'ruby3_backward_compatibility/compatibility/object'
5
+ require 'ruby3_backward_compatibility/compatibility/psych'
6
+ require 'ruby3_backward_compatibility/compatibility/string'
7
+ require 'ruby3_backward_compatibility/compatibility/uri'
@@ -0,0 +1,17 @@
1
+ require 'erb'
2
+
3
+ module Ruby3BackwardCompatibility
4
+ module ERBCompatibility
5
+ def initialize(str, safe_level = NOT_GIVEN, legacy_trim_mode = NOT_GIVEN, legacy_eoutvar = NOT_GIVEN, trim_mode: nil, eoutvar: '_erbout')
6
+ if legacy_trim_mode != NOT_GIVEN
7
+ trim_mode = legacy_trim_mode
8
+ end
9
+ if legacy_eoutvar != NOT_GIVEN
10
+ eoutvar = legacy_eoutvar
11
+ end
12
+ super(str, trim_mode: trim_mode, eoutvar: eoutvar)
13
+ end
14
+ end
15
+ end
16
+
17
+ ERB.prepend Ruby3BackwardCompatibility::ERBCompatibility
@@ -0,0 +1,14 @@
1
+ module Ruby3BackwardCompatibility
2
+ module ObjectCompatibility
3
+ # Taint/untaint used to be a noop for a while.
4
+ def taint
5
+ self
6
+ end
7
+
8
+ def untaint
9
+ self
10
+ end
11
+ end
12
+ end
13
+
14
+ Object.include Ruby3BackwardCompatibility::ObjectCompatibility
@@ -0,0 +1,36 @@
1
+ require 'psych'
2
+
3
+ module Ruby3BackwardCompatibility
4
+ module PsychCompatibility
5
+
6
+ def self.prepended(by)
7
+ by.singleton_class.prepend ClassMethods
8
+ end
9
+
10
+ module ClassMethods
11
+ if Psych::VERSION >= '4'
12
+ def load(...)
13
+ unsafe_load(...)
14
+ end
15
+ end
16
+
17
+ def safe_load(yaml, legacy_permitted_classes = NOT_GIVEN, legacy_permitted_symbols = NOT_GIVEN, legacy_aliases = NOT_GIVEN, legacy_filename = NOT_GIVEN, permitted_classes: [], permitted_symbols: [], aliases: false, filename: nil, **args)
18
+ if legacy_permitted_classes != NOT_GIVEN
19
+ permitted_classes = legacy_permitted_classes
20
+ end
21
+ if legacy_permitted_symbols != NOT_GIVEN
22
+ permitted_symbols = legacy_permitted_symbols
23
+ end
24
+ if legacy_aliases != NOT_GIVEN
25
+ aliases = legacy_aliases
26
+ end
27
+ if legacy_filename != NOT_GIVEN
28
+ filename = legacy_filename
29
+ end
30
+ super(yaml, permitted_classes: permitted_classes, permitted_symbols: permitted_symbols, aliases: aliases, filename: filename, **args)
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ Psych.prepend Ruby3BackwardCompatibility::PsychCompatibility
@@ -0,0 +1,5 @@
1
+ class String
2
+ extend Ruby3BackwardCompatibility::Ruby3Keywords
3
+
4
+ ruby3_keywords :encode
5
+ end
@@ -0,0 +1,19 @@
1
+ require 'uri'
2
+
3
+ module Ruby3BackwardCompatibility
4
+ module URICompatibility
5
+ class SchemeProxy
6
+ def [](scheme)
7
+ URI.scheme_list[scheme]
8
+ end
9
+
10
+ def []=(scheme, value)
11
+ URI.register_scheme(scheme, value)
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ module URI
18
+ @@schemes ||= Ruby3BackwardCompatibility::URICompatibility::SchemeProxy.new
19
+ end
@@ -0,0 +1,33 @@
1
+ module Ruby3BackwardCompatibility
2
+ module Ruby3Keywords
3
+ def ruby3_keywords(*methods)
4
+ methods.each do |method|
5
+ method_is_private = private_instance_methods.include?(method)
6
+ method_is_protected = protected_instance_methods.include?(method)
7
+
8
+ _ruby3_keywords_module.define_method(method) do |*args, **keyword_args|
9
+ if args.last.is_a?(Hash)
10
+ keyword_args.merge!(args.pop)
11
+ end
12
+ super(*args, **keyword_args)
13
+ end
14
+
15
+ if method_is_private
16
+ _ruby3_keywords_module.send(:private, method)
17
+ elsif method_is_protected
18
+ _ruby3_keywords_module.send(:protected, method)
19
+ end
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def _ruby3_keywords_module
26
+ @_ruby3_keywords_module ||= begin
27
+ mod = Module.new
28
+ prepend mod
29
+ mod
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ruby3BackwardCompatibility
4
+ VERSION = '0.1.2'
5
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ruby3BackwardCompatibility
4
+ NOT_GIVEN = Object.new
5
+
6
+ # TODO private?
7
+ end
8
+
9
+ require 'ruby3_backward_compatibility/ruby3_keywords'
10
+ require 'ruby3_backward_compatibility/version'
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/ruby3_backward_compatibility/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'ruby3-backward-compatibility'
7
+ spec.version = Ruby3BackwardCompatibility::VERSION
8
+ spec.authors = ['Tobias Kraze']
9
+ spec.email = ['tobias.kraze@makandra.de']
10
+
11
+ spec.summary = 'Backward compatibility for Ruby 3 stdlib'
12
+ spec.homepage = 'https://github.com/makandra/ruby3-backward-compatibility'
13
+ spec.license = 'MIT'
14
+ spec.required_ruby_version = '>= 3.0.0'
15
+
16
+ spec.metadata['homepage_uri'] = spec.homepage
17
+ spec.metadata['source_code_uri'] = 'https://github.com/makandra/ruby3-backward-compatibility'
18
+ spec.metadata['changelog_uri'] = 'https://github.com/makandra/ruby3-backward-compatibility/blob/main/CHANGELOG.md'
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
23
+ `git ls-files -z`.split(/\x0|\u0000/).reject do |f|
24
+ (f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
25
+ end
26
+ end
27
+ spec.bindir = 'exe'
28
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ['lib']
30
+
31
+ # Uncomment to register a new dependency of your gem
32
+ # spec.add_dependency 'example-gem', '~> 1.0'
33
+
34
+ # For more information and examples about making a new gem, checkout our
35
+ # guide at: https://bundler.io/guides/creating_gem.html
36
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby3-backward-compatibility
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tobias Kraze
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-10-24 00:00:00.000000000 Z
11
+ date: 2022-10-25 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -16,7 +16,27 @@ email:
16
16
  executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
- files: []
19
+ files:
20
+ - ".rspec"
21
+ - ".ruby-version"
22
+ - CHANGELOG.md
23
+ - Gemfile
24
+ - Gemfile.lock
25
+ - LICENSE.txt
26
+ - README.md
27
+ - Rakefile
28
+ - bin/console
29
+ - bin/setup
30
+ - lib/ruby3_backward_compatibility.rb
31
+ - lib/ruby3_backward_compatibility/compatibility/all.rb
32
+ - lib/ruby3_backward_compatibility/compatibility/erb.rb
33
+ - lib/ruby3_backward_compatibility/compatibility/object.rb
34
+ - lib/ruby3_backward_compatibility/compatibility/psych.rb
35
+ - lib/ruby3_backward_compatibility/compatibility/string.rb
36
+ - lib/ruby3_backward_compatibility/compatibility/uri.rb
37
+ - lib/ruby3_backward_compatibility/ruby3_keywords.rb
38
+ - lib/ruby3_backward_compatibility/version.rb
39
+ - ruby3-backward-compatibility.gemspec
20
40
  homepage: https://github.com/makandra/ruby3-backward-compatibility
21
41
  licenses:
22
42
  - MIT