-
-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support paths with different variable names in same path location (#273)
--------- Co-authored-by: Kyle Plump <[email protected]>
- Loading branch information
Showing
8 changed files
with
307 additions
and
73 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# frozen_string_literal: true | ||
|
||
require "mustermann/rails" | ||
|
||
module Hanami | ||
class Router | ||
class Leaf | ||
# Trie Leaf | ||
# | ||
# @api private | ||
# @since 2.2.0 | ||
attr_reader :to, :params | ||
|
||
# @api private | ||
# @since 2.2.0 | ||
def initialize(route, to, constraints) | ||
@route = route | ||
@to = to | ||
@constraints = constraints | ||
@params = nil | ||
end | ||
|
||
# @api private | ||
# @since 2.2.0 | ||
def match(path) | ||
match = matcher.match(path) | ||
|
||
@params = match.named_captures if match | ||
|
||
match | ||
end | ||
|
||
private | ||
|
||
# @api private | ||
# @since 2.2.0 | ||
def matcher | ||
@matcher ||= Mustermann.new(@route, type: :rails, version: "5.0", capture: @constraints) | ||
end | ||
end | ||
end | ||
end |
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# frozen_string_literal: true | ||
|
||
require "hanami/router/leaf" | ||
|
||
RSpec.describe Hanami::Router::Leaf do | ||
let(:subject) { described_class.new(route, to, constraints) } | ||
let(:route) { "/test/route" } | ||
let(:to) { "test proc" } | ||
let(:constraints) { {} } | ||
|
||
describe "#initialize" do | ||
it "returns a #{described_class} instance" do | ||
expect(subject).to be_kind_of(described_class) | ||
end | ||
end | ||
|
||
describe "#to" do | ||
it "returns the endpoint passed as 'to' when initialized" do | ||
expect(subject.to).to eq(to) | ||
end | ||
end | ||
|
||
describe "#match" do | ||
context "when path matches route" do | ||
let(:matching_path) { route } | ||
|
||
it "returns true" do | ||
expect(subject.match(matching_path)).to be_truthy | ||
end | ||
end | ||
|
||
context "when path doesn't match route" do | ||
let(:non_matching_path) { "/bad/path" } | ||
|
||
it "returns true" do | ||
expect(subject.match(non_matching_path)).to be_falsey | ||
end | ||
end | ||
end | ||
|
||
describe "#params" do | ||
context "without previously calling #match(path)" do | ||
it "returns nil" do | ||
params = subject.params | ||
|
||
expect(params).to be_nil | ||
end | ||
end | ||
|
||
context "with variable path" do | ||
let(:route) { "test/:route" } | ||
let(:matching_path) { "test/path" } | ||
let(:matching_params) { {"route" => "path"} } | ||
|
||
it "returns captured params" do | ||
subject.match(matching_path) | ||
params = subject.params | ||
|
||
expect(params).to eq(matching_params) | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.