Skip to content

Commit 39da0d9

Browse files
committed
Refactor old spelling deprecation
Makes rubocop happy, if not me...
1 parent 59549c7 commit 39da0d9

File tree

1 file changed

+34
-25
lines changed

1 file changed

+34
-25
lines changed

lib/meilisearch.rb

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,43 +16,52 @@ module Meilisearch
1616
end
1717

1818
module MeiliSearch
19-
# Softly deprecate the old spelling of the top level module
20-
# from MeiliSearch to Meilisearch
21-
def self.const_missing(const_name)
22-
return super if @warned
23-
24-
Meilisearch::Utils.logger.warn <<~RENAMED_MODULE_WARNING
25-
[meilisearch-ruby] The top-level module of Meilisearch has been renamed.
26-
[meilisearch-ruby] Please update "MeiliSearch" to "Meilisearch".
27-
RENAMED_MODULE_WARNING
28-
29-
Meilisearch.constants.each do |constant|
30-
const_set(constant, Meilisearch.const_get(constant))
19+
class << self
20+
# Softly deprecate the old spelling of the top level module
21+
# from MeiliSearch to Meilisearch
22+
def const_missing(const_name)
23+
return super if @warned
24+
25+
_warn_about_deprecation
26+
_define_constants
27+
28+
@warned = true
29+
30+
# Now that all the proper constants have been set,
31+
# we can tell ruby to search for the const in MeiliSearch again.
32+
# If it's still not found, then it does not exist in
33+
# Meilisearch and the call to `super` will throw a normal error
34+
const_get(const_name)
3135
end
3236

33-
@warned = true
37+
def method_missing(method_name)
38+
unless @warned
39+
_warn_about_deprecation
40+
_define_constants
41+
end
3442

35-
# Now that all the proper constants have been set,
36-
# we can tell ruby to search for the const in MeiliSearch again.
37-
# If it's still not found, then it does not exist in
38-
# Meilisearch and the call to `super` will throw a normal error
39-
const_get(const_name)
40-
end
43+
@warned = true
44+
45+
Meilisearch.send(method_name)
46+
end
4147

42-
def self.method_missing(method_name)
43-
unless @warned
48+
def respond_to_missing?(method_name, *)
49+
Meilisearch.respond_to?(method_name) || super
50+
end
51+
52+
private
53+
54+
def _warn_about_deprecation
4455
Meilisearch::Utils.logger.warn <<~RENAMED_MODULE_WARNING
4556
[meilisearch-ruby] The top-level module of Meilisearch has been renamed.
4657
[meilisearch-ruby] Please update "MeiliSearch" to "Meilisearch".
4758
RENAMED_MODULE_WARNING
59+
end
4860

61+
def _define_constants
4962
Meilisearch.constants.each do |constant|
5063
const_set(constant, Meilisearch.const_get(constant))
5164
end
5265
end
53-
54-
@warned = true
55-
56-
Meilisearch.send(method_name)
5766
end
5867
end

0 commit comments

Comments
 (0)