#2 ✓resolved
Matt Todd

[PATCH] Rack::Request Subclassing and Memoization Issues

Reported by Matt Todd | December 28th, 2008 @ 07:11 AM | in 1.0

Creating a ticket for the discussion at:

http://groups.google.com/group/r...

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

  • Ryan Tomayko

    Ryan Tomayko December 28th, 2008 @ 09:47 AM

    Something like this is necessary at minimum, IMO:

    http://groups.google.com/group/r...

    
    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.

  • Matt Todd

    Matt Todd December 28th, 2008 @ 09:57 AM

    • 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.

    http://github.com/mtodd/rack/com...

    ed32e29176c24043d666ca56d1127a714a95d811 8acbf2b6f875f20893083b451d34ac72912b7a71

    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.

  • Ryan Tomayko

    Ryan Tomayko December 29th, 2008 @ 10:43 AM

    I guess we're still in the process of ironing out how and when Rack::Request should be used vs. the env hash but I'm a big +1 for mtodd's patch.

  • josh

    josh February 1st, 2009 @ 03:59 AM

    • Milestone set to 1.0
    • Assigned user set to “josh”
    • State changed from “new” to “resolved”

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

People watching this ticket

Pages