Skip to content

Instantly share code, notes, and snippets.

@jferris
Created February 17, 2010 15:28
Show Gist options
  • Select an option

  • Save jferris/306712 to your computer and use it in GitHub Desktop.

Select an option

Save jferris/306712 to your computer and use it in GitHub Desktop.
class HashWithLazy < Hash
attr_reader :lazy
def initialize(*args, &block)
@lazy = {}
super(*args, &block)
end
def [](key)
if @lazy.key?(key)
self[key] = @lazy.call
@lazy.delete(key)
end
super(key)
end
end
class RackLazy
def initialize(app)
@app = app
end
def call(env)
@app.call(HashWithLazy.new.update(env))
end
end
class XmlDocumentParser
def initialize(app)
@app = app
end
def call(env)
if env['Content-Type'] == 'text/xml'
env.lazy[:doc] = lambda { Nokogiri::XML.parse(env[:input]) }
end
@app.call(env)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment