lib/grape/api/instance.rb in grape-1.2.4 vs lib/grape/api/instance.rb in grape-1.2.5
- old
+ new
@@ -11,16 +11,15 @@
attr_reader :instance
attr_reader :base
attr_accessor :configuration
def given(conditional_option, &block)
- evaluate_as_instance_with_configuration(block) if conditional_option && block_given?
+ evaluate_as_instance_with_configuration(block, lazy: true) if conditional_option && block_given?
end
def mounted(&block)
- return if base_instance?
- evaluate_as_instance_with_configuration(block)
+ evaluate_as_instance_with_configuration(block, lazy: true)
end
def base=(grape_api)
@base = grape_api
grape_api.instances << self
@@ -59,11 +58,11 @@
# 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.
def call(env)
- LOCK.synchronize { compile } unless instance
+ compile!
call!(env)
end
# A non-synchronized version of ::call.
def call!(env)
@@ -77,13 +76,18 @@
else
namespace_inheritable(:cascade, value)
end
end
+ def compile!
+ return if instance
+ LOCK.synchronize { compile unless instance }
+ end
+
# see Grape::Router#recognize_path
def recognize_path(path)
- LOCK.synchronize { compile } unless instance
+ compile!
instance.router.recognize_path(path)
end
protected
@@ -103,17 +107,24 @@
else
instance_eval(&block)
end
end
- def evaluate_as_instance_with_configuration(block)
- value_for_configuration = configuration
- if value_for_configuration.respond_to?(:lazy?) && value_for_configuration.lazy?
- self.configuration = value_for_configuration.evaluate
+ def evaluate_as_instance_with_configuration(block, lazy: false)
+ lazy_block = Grape::Util::LazyBlock.new do |configuration|
+ value_for_configuration = configuration
+ if value_for_configuration.respond_to?(:lazy?) && value_for_configuration.lazy?
+ self.configuration = value_for_configuration.evaluate
+ end
+ response = instance_eval(&block)
+ self.configuration = value_for_configuration
+ response
end
- response = instance_eval(&block)
- self.configuration = value_for_configuration
- response
+ if base_instance? && lazy
+ lazy_block
+ else
+ lazy_block.evaluate_from(configuration)
+ end
end
def inherited(subclass)
subclass.reset!
subclass.logger = logger.clone