deprecations 2.2.2 → 2.6.0
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 +4 -4
- data/README.md +10 -12
- data/lib/deprecations/version.rb +1 -1
- data/lib/deprecations.rb +159 -9
- metadata +5 -10
- data/lib/deprecations/behavior.rb +0 -65
- data/lib/deprecations/extension.rb +0 -95
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8a9290e6b74cd3d108623f9b726b6f71b02a5108feb534114fb2d63160dfbfab
|
4
|
+
data.tar.gz: 9a554fb7d11c6d6e9c741f43c846c1d140705be8621540683ab94b3bb2bdc0bd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 66817f08bf5641c9c09404615a1290ec8fe037751e8f0157b91fb7d577f2f24c1818febc6920fc36216e2386cbf40e47d275f048a12fd31a0f20cdfe5129f075
|
7
|
+
data.tar.gz: a57b529946baec215bdb4998151a7dc695166eb629dbbc232a6296a24b31eb60f45a84fb68d82f67632bb256293536197b11328e8901d2a25d0ef3b19219e4fe
|
data/README.md
CHANGED
@@ -14,13 +14,13 @@ gem 'deprecations'
|
|
14
14
|
|
15
15
|
and install it by running Bundler:
|
16
16
|
|
17
|
-
```
|
18
|
-
$ bundle
|
17
|
+
```shell
|
18
|
+
$ bundle add deprecations
|
19
19
|
```
|
20
20
|
|
21
21
|
To install the gem globally use:
|
22
22
|
|
23
|
-
```
|
23
|
+
```shell
|
24
24
|
$ gem install deprecations
|
25
25
|
```
|
26
26
|
|
@@ -36,7 +36,6 @@ you can specify which methods and classes are deprecated. To mark a method as de
|
|
36
36
|
|
37
37
|
```ruby
|
38
38
|
class MySample
|
39
|
-
|
40
39
|
def clear
|
41
40
|
# something here
|
42
41
|
end
|
@@ -46,7 +45,6 @@ class MySample
|
|
46
45
|
end
|
47
46
|
|
48
47
|
deprecated :clean, :clear, 'next version'
|
49
|
-
|
50
48
|
end
|
51
49
|
```
|
52
50
|
|
@@ -72,16 +70,18 @@ Deprecations.behavior = :raise
|
|
72
70
|
|
73
71
|
There are 3 pre-defined behaviors:
|
74
72
|
|
75
|
-
- `:raise` will raise an `
|
73
|
+
- `:raise` will raise an `Deprecations::Exception` when a deprecated method is called
|
76
74
|
- `:silence` will do nothing (ignore the deprecation)
|
77
75
|
- `:warn` will print a warning (default behavior)
|
76
|
+
- `:deprecated` will print a warning when Ruby's warning category 'deprecated' is enabled
|
78
77
|
|
79
78
|
Besides this you can implement your own:
|
80
79
|
|
81
80
|
```ruby
|
82
|
-
Deprecations.behavior =
|
83
|
-
|
84
|
-
|
81
|
+
Deprecations.behavior =
|
82
|
+
proc do |subject, _alternative, _outdated|
|
83
|
+
SuperLogger.warning "deprecated: #{subject}"
|
84
|
+
end
|
85
85
|
```
|
86
86
|
|
87
87
|
Any object responding to `#call` will be accepted as a valid handler.
|
@@ -89,9 +89,7 @@ Any object responding to `#call` will be accepted as a valid handler.
|
|
89
89
|
Whenever you need to temporary change the standard behavior (like e.g. in your specs) you can do this like
|
90
90
|
|
91
91
|
```ruby
|
92
|
-
Deprecations.with_behavior(:silent)
|
93
|
-
MyDeprecatedClass.new.do_some_magic
|
94
|
-
end
|
92
|
+
Deprecations.with_behavior(:silent) { MyDeprecatedClass.new.do_some_magic }
|
95
93
|
```
|
96
94
|
|
97
95
|
Please have a look at the [specs](https://github.com/mblumtritt/deprecations/blob/master/spec/deprecations_spec.rb) for detailed information and more samples.
|
data/lib/deprecations/version.rb
CHANGED
data/lib/deprecations.rb
CHANGED
@@ -1,18 +1,168 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Deprecations
|
4
|
-
require_relative 'deprecations/version'
|
5
|
-
require_relative 'deprecations/extension'
|
6
|
-
require_relative 'deprecations/behavior'
|
7
|
-
|
8
4
|
Error = Class.new(ScriptError)
|
9
5
|
|
10
|
-
|
11
|
-
|
12
|
-
|
6
|
+
class << self
|
7
|
+
def behavior = BEHAVIOR.key(@behavior) || @behavior
|
8
|
+
|
9
|
+
def behavior=(value)
|
10
|
+
@behavior = as_behavior(value)
|
11
|
+
end
|
12
|
+
|
13
|
+
def with_behavior(behavior)
|
14
|
+
current_behavior = @behavior
|
15
|
+
@behavior = as_behavior(behavior)
|
16
|
+
raise(ArgumentError, 'block expected') unless block_given?
|
17
|
+
yield
|
18
|
+
ensure
|
19
|
+
@behavior = current_behavior
|
20
|
+
end
|
21
|
+
|
22
|
+
alias set_behavior with_behavior
|
23
|
+
|
24
|
+
def call(subject, alternative, outdated)
|
25
|
+
@behavior.call(subject, alternative, outdated)
|
26
|
+
self
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def as_behavior(arg)
|
32
|
+
return arg if defined?(arg.call)
|
33
|
+
BEHAVIOR.fetch(arg) do
|
34
|
+
raise(
|
35
|
+
ArgumentError,
|
36
|
+
"invalid parameter - behavior has to be #{
|
37
|
+
BEHAVIOR.keys.map(&:inspect).join(' | ')
|
38
|
+
} or need to respond to `#call`"
|
39
|
+
)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def name_error(exc)
|
44
|
+
exc.set_backtrace(exc.backtrace.drop_while { _1.start_with?(__FILE__) })
|
45
|
+
exc
|
46
|
+
end
|
47
|
+
|
48
|
+
module Raise
|
49
|
+
def self.call(subject, alternative, _outdated)
|
50
|
+
error =
|
51
|
+
Error.new(
|
52
|
+
"`#{subject}` is deprecated#{
|
53
|
+
" - use #{alternative} instead" if alternative
|
54
|
+
}"
|
55
|
+
)
|
56
|
+
error.set_backtrace(caller(3))
|
57
|
+
raise(error)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
module WarnMessage
|
62
|
+
def message(subject, alternative, outdated)
|
63
|
+
"`#{subject}` is deprecated#{
|
64
|
+
outdated ? " and will be outdated #{outdated}." : '.'
|
65
|
+
}#{" Please use `#{alternative}` instead." if alternative}"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
module Warn
|
70
|
+
extend WarnMessage
|
71
|
+
def self.call(*args) = ::Kernel.warn(message(*args), uplevel: 3)
|
72
|
+
end
|
73
|
+
|
74
|
+
module Deprecated
|
75
|
+
extend WarnMessage
|
76
|
+
def self.call(*args)
|
77
|
+
::Kernel.warn(message(*args), uplevel: 3, category: :deprecated)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
BEHAVIOR = {
|
82
|
+
silence: proc {},
|
83
|
+
raise: Raise,
|
84
|
+
warn: Warn,
|
85
|
+
deprecated: Deprecated
|
86
|
+
}.freeze
|
87
|
+
|
88
|
+
module ClassMethods
|
89
|
+
private
|
90
|
+
|
91
|
+
def deprecated(name, alt = nil, outdated = nil)
|
92
|
+
alias_name = "__deprecated__singleton_method__#{name}__"
|
93
|
+
return if private_method_defined?(alias_name)
|
94
|
+
begin
|
95
|
+
alias_method(alias_name, name)
|
96
|
+
rescue NameError => e
|
97
|
+
raise ::Deprecations.__send__(:name_error, e)
|
98
|
+
end
|
99
|
+
private(alias_name)
|
100
|
+
if alt.is_a?(Symbol)
|
101
|
+
begin
|
102
|
+
alt = instance_method(alt)
|
103
|
+
rescue NameError => e
|
104
|
+
raise ::Deprecations.__send__(:name_error, e)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
define_method(name) do |*args, **kw_args, &b|
|
108
|
+
::Deprecations.call(
|
109
|
+
"#{self}.#{::Kernel.__method__}",
|
110
|
+
(alt.is_a?(UnboundMethod) ? "#{self}.#{alt.name}" : alt),
|
111
|
+
outdated
|
112
|
+
)
|
113
|
+
__send__(alias_name, *args, **kw_args, &b)
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
module InstanceMethods
|
119
|
+
private
|
120
|
+
|
121
|
+
def deprecated(name, alt = nil, outdated = nil)
|
122
|
+
alias_name = "__deprecated__instance_method__#{name}__"
|
123
|
+
return if private_method_defined?(alias_name)
|
124
|
+
begin
|
125
|
+
alias_method(alias_name, name)
|
126
|
+
rescue NameError => e
|
127
|
+
::Deprecations.__send__(:name_error, e)
|
128
|
+
return singleton_class.__send__(:deprecated, name, alt, outdated)
|
129
|
+
end
|
130
|
+
private(alias_name)
|
131
|
+
if alt.is_a?(Symbol)
|
132
|
+
begin
|
133
|
+
alt = instance_method(alt)
|
134
|
+
rescue NameError => e
|
135
|
+
raise ::Deprecations.__send__(:name_error, e)
|
136
|
+
end
|
137
|
+
end
|
138
|
+
define_method(name) do |*args, **kw_args, &b|
|
139
|
+
pref =
|
140
|
+
if defined?(self.class.name)
|
141
|
+
self.class.name
|
142
|
+
else
|
143
|
+
::Kernel.instance_method(:class).bind(self).call
|
144
|
+
end
|
145
|
+
::Deprecations.call(
|
146
|
+
"#{pref}##{::Kernel.__method__}",
|
147
|
+
(alt.is_a?(UnboundMethod) ? "#{pref}##{alt.name}" : alt),
|
148
|
+
outdated
|
149
|
+
)
|
150
|
+
__send__(alias_name, *args, **kw_args, &b)
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
def deprecated!(alternative = nil, outdated = nil)
|
155
|
+
org = method(:new)
|
156
|
+
define_singleton_method(:new) do |*args, **kw_args, &b|
|
157
|
+
::Deprecations.call(name, alternative, outdated)
|
158
|
+
org.call(*args, **kw_args, &b)
|
159
|
+
end
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
Module.extend(ClassMethods)
|
164
|
+
Module.include(InstanceMethods)
|
13
165
|
end
|
14
166
|
|
15
167
|
self.behavior = :warn
|
16
168
|
end
|
17
|
-
|
18
|
-
DeprecationError = Deprecations::Error
|
metadata
CHANGED
@@ -1,19 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: deprecations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike Blumtritt
|
8
|
-
autorequire:
|
9
8
|
bindir: bin
|
10
9
|
cert_chain: []
|
11
|
-
date:
|
10
|
+
date: 2025-01-30 00:00:00.000000000 Z
|
12
11
|
dependencies: []
|
13
12
|
description: |
|
14
13
|
This gem provides transparent declaration of deprecated methods and classes.
|
15
14
|
It's easy, small, has no dependencies and no overhead.
|
16
|
-
email:
|
17
15
|
executables: []
|
18
16
|
extensions: []
|
19
17
|
extra_rdoc_files:
|
@@ -21,15 +19,13 @@ extra_rdoc_files:
|
|
21
19
|
files:
|
22
20
|
- README.md
|
23
21
|
- lib/deprecations.rb
|
24
|
-
- lib/deprecations/behavior.rb
|
25
|
-
- lib/deprecations/extension.rb
|
26
22
|
- lib/deprecations/version.rb
|
27
23
|
homepage: https://github.com/mblumtritt/deprecations
|
28
24
|
licenses: []
|
29
25
|
metadata:
|
30
26
|
source_code_uri: https://github.com/mblumtritt/deprecations
|
31
27
|
bug_tracker_uri: https://github.com/mblumtritt/deprecations/issues
|
32
|
-
|
28
|
+
rubygems_mfa_required: 'true'
|
33
29
|
rdoc_options: []
|
34
30
|
require_paths:
|
35
31
|
- lib
|
@@ -37,15 +33,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
37
33
|
requirements:
|
38
34
|
- - ">="
|
39
35
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
36
|
+
version: 3.0.0
|
41
37
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
38
|
requirements:
|
43
39
|
- - ">="
|
44
40
|
- !ruby/object:Gem::Version
|
45
41
|
version: '0'
|
46
42
|
requirements: []
|
47
|
-
rubygems_version: 3.3
|
48
|
-
signing_key:
|
43
|
+
rubygems_version: 3.6.3
|
49
44
|
specification_version: 4
|
50
45
|
summary: Deprecation support for your project.
|
51
46
|
test_files: []
|
@@ -1,65 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Deprecations
|
4
|
-
class << self
|
5
|
-
def behavior
|
6
|
-
BEHAVIOR.key(@behavior) || @behavior
|
7
|
-
end
|
8
|
-
|
9
|
-
def behavior=(value)
|
10
|
-
@behavior = as_behavior(value)
|
11
|
-
end
|
12
|
-
|
13
|
-
def with_behavior(behavior)
|
14
|
-
behavior = as_behavior(behavior)
|
15
|
-
raise(ArgumentError, 'block expected') unless block_given?
|
16
|
-
current_behavior = @behavior
|
17
|
-
begin
|
18
|
-
@behavior = behavior
|
19
|
-
yield
|
20
|
-
ensure
|
21
|
-
@behavior = current_behavior
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
alias set_behavior with_behavior
|
26
|
-
|
27
|
-
private
|
28
|
-
|
29
|
-
def as_behavior(arg)
|
30
|
-
return arg if defined?(arg.call)
|
31
|
-
BEHAVIOR.fetch(arg) do
|
32
|
-
raise(
|
33
|
-
ArgumentError,
|
34
|
-
'invalid parameter - behavior has to be ' \
|
35
|
-
"#{valid_behaviors} or need to respond to `call`"
|
36
|
-
)
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
def valid_behaviors
|
41
|
-
BEHAVIOR.keys.map(&:inspect).join(' | ')
|
42
|
-
end
|
43
|
-
|
44
|
-
module Raise
|
45
|
-
def self.call(subject, alternative, _outdated)
|
46
|
-
msg = "`#{subject}` is deprecated"
|
47
|
-
msg << " - use #{alternative} instead" if alternative
|
48
|
-
ex = Error.new(msg)
|
49
|
-
ex.set_backtrace(caller(3))
|
50
|
-
raise(ex)
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
module Warn
|
55
|
-
def self.call(subject, alternative, outdated)
|
56
|
-
msg = "`#{subject}` is deprecated"
|
57
|
-
msg << (outdated ? " and will be outdated #{outdated}." : '.')
|
58
|
-
msg << " Please use `#{alternative}` instead." if alternative
|
59
|
-
::Kernel.warn(msg, uplevel: 3)
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
BEHAVIOR = { silence: proc {}, raise: Raise, warn: Warn }.freeze
|
64
|
-
end
|
65
|
-
end
|
@@ -1,95 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Deprecations
|
4
|
-
class << self
|
5
|
-
private
|
6
|
-
|
7
|
-
def infect(mod)
|
8
|
-
mod.extend(ClassMethods)
|
9
|
-
mod.send(:include, InstanceMethods)
|
10
|
-
end
|
11
|
-
|
12
|
-
module Helper
|
13
|
-
private
|
14
|
-
|
15
|
-
def __method(method_name)
|
16
|
-
begin
|
17
|
-
instance_method(method_name)
|
18
|
-
rescue StandardError
|
19
|
-
nil
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
def __method_deprecated!(method, alternative, outdated)
|
24
|
-
defining_context = self
|
25
|
-
undef_method(method.name)
|
26
|
-
define_method(method.name) do |*a, &b|
|
27
|
-
decorated = Class === self ? "#{self}." : "#{defining_context}#"
|
28
|
-
alternative = "#{decorated}#{alternative.name}" if UnboundMethod ===
|
29
|
-
alternative
|
30
|
-
Deprecations.call(
|
31
|
-
"#{decorated}#{::Kernel.__method__}",
|
32
|
-
alternative,
|
33
|
-
outdated
|
34
|
-
)
|
35
|
-
method.bind(self).call(*a, &b)
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
def __method_checked(method_name)
|
40
|
-
__method(method_name) or
|
41
|
-
raise(
|
42
|
-
NameError,
|
43
|
-
"undefined method `#{method_name}` for class `#{self}`"
|
44
|
-
)
|
45
|
-
end
|
46
|
-
|
47
|
-
def __method_alternative(alternative)
|
48
|
-
Symbol === alternative ? __method_checked(alternative) : alternative
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
module ClassMethods
|
53
|
-
private
|
54
|
-
|
55
|
-
include Helper
|
56
|
-
|
57
|
-
def deprecated(method_name, alternative = nil, outdated = nil)
|
58
|
-
__method_deprecated!(
|
59
|
-
__method_checked(method_name),
|
60
|
-
__method_alternative(alternative),
|
61
|
-
outdated
|
62
|
-
)
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
module InstanceMethods
|
67
|
-
private
|
68
|
-
|
69
|
-
include Helper
|
70
|
-
|
71
|
-
def deprecated(method_name, alternative = nil, outdated = nil)
|
72
|
-
m = __method(method_name) or
|
73
|
-
return(
|
74
|
-
singleton_class.send(
|
75
|
-
:deprecated,
|
76
|
-
method_name,
|
77
|
-
alternative,
|
78
|
-
outdated
|
79
|
-
)
|
80
|
-
)
|
81
|
-
__method_deprecated!(m, __method_alternative(alternative), outdated)
|
82
|
-
end
|
83
|
-
|
84
|
-
def deprecated!(alternative = nil, outdated = nil)
|
85
|
-
m = method(:new)
|
86
|
-
define_singleton_method(:new) do |*a, &b|
|
87
|
-
Deprecations.call(self, alternative, outdated)
|
88
|
-
m.call(*a, &b)
|
89
|
-
end
|
90
|
-
end
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
|
-
infect(Module)
|
95
|
-
end
|