1
1
# frozen_string_literal: true
2
2
3
3
# Based on Rails ActiveSupport Deprecator
4
- # https://github.com/rails/rails/blob/6f0d1ad14b92b9f5906e44740fce8b4f1c7075dc /activesupport/lib/active_support/deprecation/constant_accessor.rb
4
+ # https://github.com/rails/rails/blob/main /activesupport/lib/active_support/deprecation/constant_accessor.rb
5
5
6
6
# rubocop:disable Style/ClassVars
7
7
module Faker
8
+ # Provides a way to rename generators, including their namespaces, with a deprecation cycle in which
9
+ # both the old and new names work, but using the old one prints a deprecation message.
10
+ #
11
+ # Deprecator provides a deprecate_generator method to be used when
12
+ # renaming a generator. For example, let's say we want to change the following Generator's
13
+ # name to <tt>Faker::NewGenerator</tt>:
14
+ #
15
+ # module Faker
16
+ # class Generator
17
+ # def self.generate
18
+ # "be kind"
19
+ # end
20
+ # end
21
+ # end
22
+ #
23
+ # To rename it, you need to do the update the name and declare the deprecation by
24
+ # including the <tt>Deprecator</tt> module and using the deprecate_generator method:
25
+ #
26
+ # module Faker
27
+ # class NewGenerator
28
+ # def self.generate
29
+ # "be kind"
30
+ # end
31
+ # end
32
+ #
33
+ # include Deprecator
34
+ # deprecate_generator('DeprecatedGenerator', NewGenerator)
35
+ # end
36
+ #
37
+ # The first argument is a constant name (no colons) as a string. It is the name of
38
+ # the constant you want to deprecate.
39
+ #
40
+ # The second argument is the constant path of the replacement (no colons) as a constant.
41
+ #
42
+ # For this to work, a +const_missing+ hook is installed. When users
43
+ # reference the deprecated constant, the callback prints the
44
+ # message and constantizes the replacement.
45
+ #
46
+ # With that in place, references to <tt>Faker::Deprecator</tt> still work, they
47
+ # evaluate to <tt>Faker::NewGenerator</tt> now, and trigger a deprecation warning:
48
+ #
49
+ # Faker::Generator.generate
50
+ # # DEPRECATION WARNING: Faker::Generator is deprecated. Use Faker::NewGenerator instead
51
+ # # "be kind"
52
+ #
53
+ # For testing the deprecations, we provide <tt>assert_deprecated</tt>
54
+ # and <tt>assert_not_deprecated</tt> matchers.
55
+ #
56
+ # There's also a <tt>Faker::Deprecator.skip_warning</tt> helper to silence
57
+ # the deprecation messages in the *test* output. Use it for generators that have lots of tests
58
+ # to avoid too many noise when running the tests.
8
59
module Deprecator
9
60
def self . included ( base )
10
61
extension = Module . new do
11
62
def const_missing ( missing_const_name )
12
63
if class_variable_defined? ( :@@_deprecated_constants ) && ( replacement = class_variable_get ( :@@_deprecated_constants ) [ missing_const_name . to_s ] )
13
64
unless Faker ::Deprecator . skip_warning?
14
- $stdout. puts ( "DEPRECATION WARNING: #{ name } ::#{ replacement [ :old_generator ] } is deprecated. Use #{ replacement [ :new_constant ] } instead." )
65
+ deprecated_message = "#{ name } ::#{ replacement [ :old_generator ] } is deprecated."
66
+ replacement_message = "Use #{ replacement [ :new_constant ] } instead."
67
+ $stdout. puts ( "DEPRECATION WARNING: #{ deprecated_message } #{ replacement_message } " )
15
68
end
16
69
17
70
return replacement [ :new_constant ]
@@ -22,13 +75,25 @@ def const_missing(missing_const_name)
22
75
23
76
def deprecate_generator ( old_generator_name , new_generator_constant )
24
77
class_variable_set ( :@@_deprecated_constants , { } ) unless class_variable_defined? ( :@@_deprecated_constants )
25
- class_variable_get ( :@@_deprecated_constants ) [ old_generator_name ] = { new_constant : new_generator_constant , old_generator : old_generator_name }
78
+ class_variable_get ( :@@_deprecated_constants ) [ old_generator_name ] = {
79
+ new_constant : new_generator_constant ,
80
+ old_generator : old_generator_name
81
+ }
26
82
end
27
83
end
28
84
29
85
base . singleton_class . prepend extension
30
86
end
31
87
88
+ # Silence deprecation warnings within the block.
89
+ #
90
+ # Faker::Generator.generate
91
+ # # => DEPRECATION WARNING: Faker::Generator is deprecated. Use Faker::NewGenerator instead.
92
+ #
93
+ # Faker::Deprecator.skip_warning do
94
+ # Faker::Generator.generate
95
+ # end
96
+ # # => nil
32
97
def self . skip_warning
33
98
original = Faker ::Deprecator . skip
34
99
Faker ::Deprecator . skip = true
0 commit comments