@@ -9,13 +9,13 @@ class API
9
9
NON_OVERRIDABLE = %I[ define_singleton_method instance_variable_set inspect class is_a? ! kind_of? respond_to? ] . freeze
10
10
11
11
class << self
12
- attr_accessor :base_instance
12
+ attr_accessor :base_instance , :instances
13
13
# When inherited, will create a list of all instances (times the API was mounted)
14
14
# It will listen to the setup required to mount that endpoint, and replicate it on any new instance
15
- def inherited ( remountable_class , base_instance_parent = Grape ::API ::Instance )
16
- remountable_class . initial_setup ( base_instance_parent )
17
- remountable_class . override_all_methods
18
- remountable_class . make_inheritable
15
+ def inherited ( api , base_instance_parent = Grape ::API ::Instance )
16
+ api . initial_setup ( base_instance_parent )
17
+ api . override_all_methods!
18
+ make_inheritable ( api )
19
19
end
20
20
21
21
# Initialize the instance variables on the remountable class, and the base_instance
@@ -27,19 +27,21 @@ def initial_setup(base_instance_parent)
27
27
@base_instance = mount_instance
28
28
end
29
29
30
- # Redefines all methods so that are forwarded to add_setup and recorded
31
- def override_all_methods
30
+ # Redefines all methods so that are forwarded to add_setup and be recorded
31
+ def override_all_methods!
32
32
( base_instance . methods - NON_OVERRIDABLE ) . each do |method_override |
33
33
define_singleton_method ( method_override ) do |*args , &block |
34
34
add_setup ( method_override , *args , &block )
35
35
end
36
36
end
37
37
end
38
38
39
- # When classes inheriting from this API child, we also want the instances to inherit from our instance
40
- def make_inheritable
41
- define_singleton_method ( :inherited ) do |sub_remountable |
42
- Grape ::API . inherited ( sub_remountable , base_instance )
39
+ # Allows an API to itself be inheritable:
40
+ def make_inheritable ( api )
41
+ # When a child API inherits from a parent API.
42
+ def api . inherited ( child_api )
43
+ # The instances of the child API inherit from the instances of the parent API
44
+ Grape ::API . inherited ( child_api , base_instance )
43
45
end
44
46
end
45
47
@@ -49,10 +51,9 @@ def make_inheritable
49
51
# too much, you may actually want to provide a new API rather than remount it.
50
52
def mount_instance ( opts = { } )
51
53
instance = Class . new ( @base_parent )
52
- instance . instance_variable_set ( :@ configuration, opts [ :configuration ] || { } )
53
- instance . define_singleton_method ( :configuration ) { @configuration }
54
+ instance . configuration = opts [ :configuration ] || { }
55
+ instance . base = self
54
56
replay_setup_on ( instance )
55
- @instances << instance
56
57
instance
57
58
end
58
59
0 commit comments