Skip to content

Instantly share code, notes, and snippets.

@rue
Created January 22, 2009 21:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rue/50742 to your computer and use it in GitHub Desktop.
Save rue/50742 to your computer and use it in GitHub Desktop.
def maps(); @maps ||= Hash.new {|h, k| h[k] = {} }; end
def on(methods, *pathspec, &block)
methods = [:get, :put, :post, :head] if methods == true
methods.each {|m|
tier = pathspec.inject(maps[m]) {|current, component|
current[component] ||= {}
}
raise "#{maps.inspect}: #{path.inspect} at #{maps[m].inspect}" if tier[Proc]
tier[Proc] = block
}
end
# ... Later ...
def get(request)
tier = extract_path(request).inject(@maps[:get]) {|current, component|
slot = current[component]
raise "No #{component.inspect} in #{current.inspect} of #{@maps[:get].inspect}" unless slot
slot
}
block = tier[Proc] or raise "No block in #{@maps[:get].inspect} for #{request.path}"
block.call
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment