6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/wikidotrb/common/decorators.rb', line 6
def login_required(*methods)
methods.each do |method|
if singleton_methods.include?(method)
singleton_class.class_eval do
alias_method "#{method}_without_login_check", method
define_method(method) do |*args, **kwargs|
client = nil
client ||= self.client if respond_to?(:client)
client = kwargs[:client] if kwargs.key?(:client)
unless client
args.each do |arg|
if arg.is_a?(Wikidotrb::Module::Client)
client = arg
break
end
end
end
raise ArgumentError, "Client is not found" if client.nil?
client.login_check
send("#{method}_without_login_check", *args, **kwargs)
end
end
elsif instance_methods.include?(method)
alias_method "#{method}_without_login_check", method
define_method(method) do |*args, **kwargs|
client = nil
client ||= self.client if respond_to?(:client)
client = kwargs[:client] if kwargs.key?(:client)
unless client
args.each do |arg|
if arg.is_a?(Wikidotrb::Module::Client)
client = arg
break
end
end
end
raise ArgumentError, "Client is not found" if client.nil?
client.login_check
send("#{method}_without_login_check", *args, **kwargs)
end
else
raise NameError, "Undefined method `#{method}` for class `#{self}`"
end
end
end
|