lib/roda.rb in roda-2.6.0 vs lib/roda.rb in roda-2.7.0

- old
+ new

@@ -7,37 +7,28 @@ # except for some constants. class Roda # Error class raised by Roda class RodaError < StandardError; end - if defined?(RUBY_ENGINE) && RUBY_ENGINE != 'ruby' - # :nocov: - # A thread safe cache class, offering only #[] and #[]= methods, - # each protected by a mutex. Used on non-MRI where Hash is not - # thread safe. - class RodaCache - # Create a new thread safe cache. - def initialize - @mutex = Mutex.new - @hash = {} - end + # A thread safe cache class, offering only #[] and #[]= methods, + # each protected by a mutex. + class RodaCache + # Create a new thread safe cache. + def initialize + @mutex = Mutex.new + @hash = {} + end - # Make getting value from underlying hash thread safe. - def [](key) - @mutex.synchronize{@hash[key]} - end + # Make getting value from underlying hash thread safe. + def [](key) + @mutex.synchronize{@hash[key]} + end - # Make setting value in underlying hash thread safe. - def []=(key, value) - @mutex.synchronize{@hash[key] = value} - end + # Make setting value in underlying hash thread safe. + def []=(key, value) + @mutex.synchronize{@hash[key] = value} end - # :nocov: - else - # Hashes are already thread-safe in MRI, due to the GVL, so they - # can safely be used as a cache. - RodaCache = Hash end # Base class used for Roda requests. The instance methods for this # class are added by Roda::RodaPlugins::Base::RequestMethods, the # class methods are added by Roda::RodaPlugins::Base::RequestClassMethods. @@ -231,11 +222,11 @@ # # request :: The instance of the request class related to this request. # This is the same object yielded by Roda.route. # response :: The instance of the response class related to this request. module InstanceMethods - # Create a request and response of the appopriate class + # Create a request and response of the appropriate class def initialize(env) klass = self.class @_request = klass::RodaRequest.new(self, env) @_response = klass::RodaResponse.new end @@ -880,23 +871,23 @@ @body.empty? end # Return the rack response array of status, headers, and body # for the current response. If the status has not been set, - # uses a 200 status if the body has been written to, otherwise - # uses a 404 status. Adds the Content-Length header to the - # size of the response body. + # uses the return value of default_status if the body has + # been written to, otherwise uses a 404 status. + # Adds the Content-Length header to the size of the response body. # # Example: # # response.finish # # => [200, # # {'Content-Type'=>'text/html', 'Content-Length'=>'0'}, # # []] def finish b = @body - s = (@status ||= b.empty? ? 404 : 200) + s = (@status ||= b.empty? ? 404 : default_status) set_default_headers h = @headers h[CONTENT_LENGTH] ||= @length.to_s [s, h, b] end @@ -905,10 +896,17 @@ # 200 response status unless status has been explicitly set, # and doesn't add the Content-Length header or use the existing # body. def finish_with_body(body) set_default_headers - [@status || 200, @headers, body] + [@status || default_status, @headers, body] + end + + # Return the default response status to be used when the body + # has been written to. This is split out to make overriding + # easier in plugins. + def default_status + 200 end # Show response class, status code, response headers, and response body def inspect "#<#{self.class.inspect} #{@status.inspect} #{@headers.inspect} #{@body.inspect}>"