Skip to content

Commit 2e61252

Browse files
committed
Rework spelling deprecation test
1 parent 39da0d9 commit 2e61252

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

lib/meilisearch.rb

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,23 @@ class << self
2020
# Softly deprecate the old spelling of the top level module
2121
# from MeiliSearch to Meilisearch
2222
def const_missing(const_name)
23-
return super if @warned
23+
return super if @warned && @constants_defined
2424

2525
_warn_about_deprecation
2626
_define_constants
2727

28-
@warned = true
29-
3028
# Now that all the proper constants have been set,
3129
# we can tell ruby to search for the const in MeiliSearch again.
3230
# If it's still not found, then it does not exist in
3331
# Meilisearch and the call to `super` will throw a normal error
3432
const_get(const_name)
3533
end
3634

37-
def method_missing(method_name)
38-
unless @warned
39-
_warn_about_deprecation
40-
_define_constants
41-
end
42-
43-
@warned = true
35+
def method_missing(method_name, *args, **kwargs)
36+
_warn_about_deprecation
37+
_define_constants
4438

45-
Meilisearch.send(method_name)
39+
Meilisearch.send(method_name, *args, **kwargs)
4640
end
4741

4842
def respond_to_missing?(method_name, *)
@@ -52,16 +46,24 @@ def respond_to_missing?(method_name, *)
5246
private
5347

5448
def _warn_about_deprecation
49+
return if @warned
50+
5551
Meilisearch::Utils.logger.warn <<~RENAMED_MODULE_WARNING
5652
[meilisearch-ruby] The top-level module of Meilisearch has been renamed.
5753
[meilisearch-ruby] Please update "MeiliSearch" to "Meilisearch".
5854
RENAMED_MODULE_WARNING
55+
56+
@warned = true
5957
end
6058

6159
def _define_constants
60+
return if @constants_defined
61+
6262
Meilisearch.constants.each do |constant|
6363
const_set(constant, Meilisearch.const_get(constant))
6464
end
65+
66+
@constants_defined = true
6567
end
6668
end
6769
end

spec/meilisearch_spec.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@
3838
end
3939

4040
RSpec.describe MeiliSearch do
41-
it 'relays constants & messages, warns about deprecation only once' do
41+
it 'using a constant warns about deprecation, returns correct constant' do
4242
logger = instance_double(Logger, warn: nil)
4343
Meilisearch::Utils.logger = logger
44+
MeiliSearch.instance_variable_set('@warned', false)
4445

4546
expect(MeiliSearch::Index).to equal(Meilisearch::Index)
4647
expect(MeiliSearch::Task).to equal(Meilisearch::Task)
@@ -52,4 +53,20 @@
5253

5354
Meilisearch::Utils.logger = nil
5455
end
56+
57+
it 'calling a method warns about deprecation, calls the right method' do
58+
logger = instance_double(Logger, warn: nil)
59+
Meilisearch::Utils.logger = logger
60+
MeiliSearch.instance_variable_set('@warned', false)
61+
62+
expect(MeiliSearch.qualified_version).to eq(Meilisearch.qualified_version)
63+
expect(MeiliSearch::Index).to equal(Meilisearch::Index)
64+
expect(MeiliSearch::Task).to equal(Meilisearch::Task)
65+
66+
expect(logger).to have_received(:warn)
67+
.with(a_string_including('The top-level module of Meilisearch has been renamed.'))
68+
.once
69+
70+
Meilisearch::Utils.logger = nil
71+
end
5572
end

0 commit comments

Comments
 (0)