-
Notifications
You must be signed in to change notification settings - Fork 52
Route accepts optional list of guard blocks #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -14,24 +14,54 @@ class Route | |
| # used to define this route (see #initialize). | ||
| attr_reader :path_spec | ||
|
|
||
| # @return [Array<Proc>] the list of guard blocks used to define this | ||
| # route (see #initialize). | ||
| attr_reader :guards | ||
|
|
||
| # When used in a path specification, will match all remaining | ||
| # segments | ||
| MATCH_ALL = '*'.freeze | ||
|
|
||
| # Creates a new Route that will associate a pattern to a | ||
| # {Resource}. | ||
| # @param [Array<String|Symbol>] path_spec a list of path | ||
| # segments (String) and identifiers (Symbol) to bind. | ||
| # Strings will be simply matched for equality. Symbols in | ||
| # the path spec will be extracted into {Request#path_info} for use | ||
| # inside your {Resource}. The special segment {MATCH_ALL} will match | ||
| # all remaining segments. | ||
| # @param [Class] resource the {Resource} to dispatch to | ||
| # @param [Hash] bindings additional information to add to | ||
| # {Request#path_info} when this route matches | ||
| # | ||
| # @example Standard route | ||
| # Route.new(["*"], MyResource) | ||
| # | ||
| # @example Guarded route | ||
| # Route.new ["/notes"], | ||
| # ->(request) { request.method == "POST" }, | ||
| # Resources::Note | ||
| # Route.new ["/notes"], Resources::NoteList | ||
| # Route.new ["/notes", :id], Resources::Note | ||
| # | ||
| # @overload initialize(path_spec, *guards, resource, bindings = {}) | ||
| # @param [Array<String|Symbol>] path_spec a list of path | ||
| # segments (String) and identifiers (Symbol) to bind. | ||
| # Strings will be simply matched for equality. Symbols in | ||
| # the path spec will be extracted into {Request#path_info} for use | ||
| # inside your {Resource}. The special segment {MATCH_ALL} will match | ||
| # all remaining segments. | ||
| # @param [Proc] guards optional guard blocks called with the request. | ||
| # @param [Class] resource the {Resource} to dispatch to | ||
| # @param [Hash] bindings additional information to add to | ||
| # {Request#path_info} when this route matches | ||
| # @see Dispatcher#add_route | ||
| def initialize(path_spec, resource, bindings={}) | ||
| @path_spec, @resource, @bindings = path_spec, resource, bindings | ||
| def initialize(path_spec, *args) | ||
| if args.last.is_a? Hash | ||
| bindings = args.pop | ||
| else | ||
| bindings = {} | ||
| end | ||
|
|
||
| resource = args.pop | ||
| guards = args | ||
|
|
||
| @path_spec = path_spec | ||
| @guards = guards | ||
| @resource = resource | ||
| @bindings = bindings | ||
|
|
||
| raise ArgumentError, t('not_resource_class', :class => resource.name) unless resource < Resource | ||
| end | ||
|
|
||
|
|
@@ -40,7 +70,7 @@ def initialize(path_spec, resource, bindings={}) | |
| # @param [Reqeust] request the request object | ||
| def match?(request) | ||
| tokens = request.uri.path.match(/^\/(.*)/)[1].split('/') | ||
| bind(tokens, {}) | ||
| guards.all? { |guard| guard[request] } && bind(tokens, {}) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would logically reverse this, that is, put the call to |
||
| end | ||
|
|
||
| # Decorates the request with information about the dispatch | ||
|
|
||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm personally not a fan of slurping args in an internal interface like this. I would propose either 1) use a signature that can work without the slurp in the public API (eg., put
resourcebeforeguardsand require an empty list be passed forguardsifbindingis provided:def initialize(path_spec, resource, guards, bindings = {})), or 2) implement#initializeas described in (1) and support the flexible (slurp-based) public API via a factory method that encapsulates this args processing and not burden#initializewith this logic (eg.,def self.build(path_spec, *args)... new(path_spec, resource, guards, bindings)).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Anything that can reduce the complexity here would be helpful. I've done many splat-argument methods and they are difficult to maintain and understand. Definitely one place where I wish for pattern-matched function headers like Erlang has!