lib/grape/api.rb in grape-2.0.0 vs lib/grape/api.rb in grape-2.1.0

- old
+ new

@@ -1,10 +1,7 @@ # frozen_string_literal: true -require 'grape/router' -require 'grape/api/instance' - module Grape # The API class is the primary entry point for creating Grape APIs. Users # should subclass this class in order to build an API. class API # Class methods that we want to call on the API rather than on the API object @@ -24,12 +21,12 @@ class << self attr_accessor :base_instance, :instances # Rather than initializing an object of type Grape::API, create an object of type Instance - def new(*args, &block) - base_instance.new(*args, &block) + def new(...) + base_instance.new(...) end # When inherited, will create a list of all instances (times the API was mounted) # It will listen to the setup required to mount that endpoint, and replicate it on any new instance def inherited(api) @@ -75,12 +72,12 @@ # This is the interface point between Rack and Grape; it accepts a request # from Rack and ultimately returns an array of three values: the status, # the headers, and the body. See [the rack specification] # (http://www.rubydoc.info/github/rack/rack/master/file/SPEC) for more. # NOTE: This will only be called on an API directly mounted on RACK - def call(*args, &block) - instance_for_rack.call(*args, &block) + def call(...) + instance_for_rack.call(...) end # Alleviates problems with autoloading by tring to search for the constant def const_missing(*args) if base_instance.const_defined?(*args) @@ -126,11 +123,10 @@ super end end def compile! - require 'grape/eager_load' instance_for_rack.compile! # See API::Instance.compile! end private @@ -148,9 +144,22 @@ @setup += [setup_step] last_response = nil @instances.each do |instance| last_response = replay_step_on(instance, setup_step) end + + # Updating all previously mounted classes in the case that new methods have been executed. + if method != :mount && @setup.any? + previous_mount_steps = @setup.select { |step| step[:method] == :mount } + previous_mount_steps.each do |mount_step| + refresh_mount_step = mount_step.merge(method: :refresh_mounted_api) + @setup += [refresh_mount_step] + @instances.each do |instance| + replay_step_on(instance, refresh_mount_step) + end + end + end + last_response end def replay_step_on(instance, setup_step) return if skip_immediate_run?(instance, setup_step[:args])