11# frozen_string_literal: true
22
33# 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
55
66# rubocop:disable Style/ClassVars
77module 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.
859 module Deprecator
960 def self . included ( base )
1061 extension = Module . new do
1162 def const_missing ( missing_const_name )
1263 if class_variable_defined? ( :@@_deprecated_constants ) && ( replacement = class_variable_get ( :@@_deprecated_constants ) [ missing_const_name . to_s ] )
1364 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 } " )
1568 end
1669
1770 return replacement [ :new_constant ]
@@ -22,13 +75,25 @@ def const_missing(missing_const_name)
2275
2376 def deprecate_generator ( old_generator_name , new_generator_constant )
2477 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+ }
2682 end
2783 end
2884
2985 base . singleton_class . prepend extension
3086 end
3187
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
3297 def self . skip_warning
3398 original = Faker ::Deprecator . skip
3499 Faker ::Deprecator . skip = true
0 commit comments