Merge pull request #3 from luislavena/detect-multiple-params

Correctly build key from detected parameters
This commit is contained in:
Luis Lavena 2016-02-29 11:49:43 -03:00
commit 88863daab2
2 changed files with 31 additions and 1 deletions

View file

@ -340,6 +340,34 @@ module Radix
end
end
context "dealing with multiple named parameters" do
it "finds matching path" do
tree = Tree.new
tree.add "/", :root
tree.add "/:section/:page", :static_page
result = tree.find("/about/shipping")
result.found?.should be_true
result.key.should eq("/:section/:page")
result.payload.should eq(:static_page)
end
it "returns named parameters in result" do
tree = Tree.new
tree.add "/", :root
tree.add "/:section/:page", :static_page
result = tree.find("/about/shipping")
result.found?.should be_true
result.params.has_key?("section").should be_true
result.params["section"].should eq("about")
result.params.has_key?("page").should be_true
result.params["page"].should eq("shipping")
end
end
context "dealing with both catch all and named parameters" do
it "finds matching path" do
tree = Tree.new

View file

@ -257,7 +257,9 @@ module Radix
path_size = _detect_param_size(path_reader)
# obtain key and value using calculated sizes
name = key_reader.string.byte_slice(key_reader.pos + 1, key_size)
# for name: skip ':' by moving one character forward and compensate
# key size.
name = key_reader.string.byte_slice(key_reader.pos + 1, key_size - 1)
value = path_reader.string.byte_slice(path_reader.pos, path_size)
# add this information to result