[PATCH] Rack::Request Subclassing and Memoization Issues
Creating a ticket for the discussion at:
The problem boils down to subclasses of Rack::Request and the recently added memoization that occurs when an instance of Rack::Request is created. Specifically, if the original Rack::Request is memoized before a subclass is, the subclass will not be created and instead the parent will remain.
Comments and changes to this ticket
Something like this is necessary at minimum, IMO:
def Rack::Request.new(env) unless env["rack.request"] and env["rack.request"].is_a?(self) env["rack.request"] = super(env) end env["rack.request"] end
Another option would be back off of using new or using alias_method to keep the non-memoizing new around:
class Rack::Request class << self ; alias_method :new!, :new ; end def self.new(env) self['rack.request'] ||= super end end
People would have to be careful to call new! on subclasses if they don't want memoization, though.
- Title changed from Rack::Request Subclassing and Memoization Issues to [PATCH] Rack::Request Subclassing and Memoization Issues
Changed the method to test for the existence and for the class equality.
This is mostly useful for subclasses that define the same self.new method.
I like this implementation more than adding a new! method because it behaves normally for all classes that expect to leave its mark on the object.
Plus, subclasses should behave mostly the same everywhere else env['rack.input'] is expected to behave like Rack::Request.