Skip to content

Commit 1421517

Browse files
committed
Refactors and comments
1 parent 1c6a9dc commit 1421517

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

lib/grape/api.rb

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ class API
99
NON_OVERRIDABLE = %I[define_singleton_method instance_variable_set inspect class is_a? ! kind_of? respond_to?].freeze
1010

1111
class << self
12-
attr_accessor :base_instance
12+
attr_accessor :base_instance, :instances
1313
# When inherited, will create a list of all instances (times the API was mounted)
1414
# 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)
1919
end
2020

2121
# Initialize the instance variables on the remountable class, and the base_instance
@@ -27,19 +27,21 @@ def initial_setup(base_instance_parent)
2727
@base_instance = mount_instance
2828
end
2929

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!
3232
(base_instance.methods - NON_OVERRIDABLE).each do |method_override|
3333
define_singleton_method(method_override) do |*args, &block|
3434
add_setup(method_override, *args, &block)
3535
end
3636
end
3737
end
3838

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)
4345
end
4446
end
4547

@@ -49,10 +51,9 @@ def make_inheritable
4951
# too much, you may actually want to provide a new API rather than remount it.
5052
def mount_instance(opts = {})
5153
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
5456
replay_setup_on(instance)
55-
@instances << instance
5657
instance
5758
end
5859

lib/grape/api/instance.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ class Instance
99

1010
class << self
1111
attr_reader :instance
12+
attr_reader :base
13+
attr_accessor :configuration
14+
15+
def base=(grape_api)
16+
@base = grape_api
17+
grape_api.instances << self
18+
end
1219

1320
# A class-level lock to ensure the API is not compiled by multiple
1421
# threads simultaneously within the same process.

0 commit comments

Comments
 (0)