Created
January 22, 2009 21:55
-
-
Save rue/50742 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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