Skip to content

Commit 431b841

Browse files
author
Joel Levin
authored
Merge pull request #101 from joeldev/2.1
JLRoutes 2.1
2 parents 930608d + d1920ae commit 431b841

File tree

13 files changed

+824
-153
lines changed

13 files changed

+824
-153
lines changed

JLRoutes.xcodeproj/project.pbxproj

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,22 @@
1717
5DA69C5D1DAB4C3A007C8E9C /* JLRParsingUtilities.h in Headers */ = {isa = PBXBuildFile; fileRef = 5DA69C541DAB4C3A007C8E9C /* JLRParsingUtilities.h */; };
1818
5DA69C5E1DAB4C3A007C8E9C /* JLRParsingUtilities.m in Sources */ = {isa = PBXBuildFile; fileRef = 5DA69C551DAB4C3A007C8E9C /* JLRParsingUtilities.m */; };
1919
5DA69C5F1DAB4C3A007C8E9C /* JLRParsingUtilities.m in Sources */ = {isa = PBXBuildFile; fileRef = 5DA69C551DAB4C3A007C8E9C /* JLRParsingUtilities.m */; };
20-
5DA69C601DAB4C3A007C8E9C /* JLRRouteDefinition.h in Headers */ = {isa = PBXBuildFile; fileRef = 5DA69C561DAB4C3A007C8E9C /* JLRRouteDefinition.h */; };
21-
5DA69C611DAB4C3A007C8E9C /* JLRRouteDefinition.h in Headers */ = {isa = PBXBuildFile; fileRef = 5DA69C561DAB4C3A007C8E9C /* JLRRouteDefinition.h */; };
20+
5DA69C601DAB4C3A007C8E9C /* JLRRouteDefinition.h in Headers */ = {isa = PBXBuildFile; fileRef = 5DA69C561DAB4C3A007C8E9C /* JLRRouteDefinition.h */; settings = {ATTRIBUTES = (Public, ); }; };
21+
5DA69C611DAB4C3A007C8E9C /* JLRRouteDefinition.h in Headers */ = {isa = PBXBuildFile; fileRef = 5DA69C561DAB4C3A007C8E9C /* JLRRouteDefinition.h */; settings = {ATTRIBUTES = (Public, ); }; };
2222
5DA69C621DAB4C3A007C8E9C /* JLRRouteDefinition.m in Sources */ = {isa = PBXBuildFile; fileRef = 5DA69C571DAB4C3A007C8E9C /* JLRRouteDefinition.m */; };
2323
5DA69C631DAB4C3A007C8E9C /* JLRRouteDefinition.m in Sources */ = {isa = PBXBuildFile; fileRef = 5DA69C571DAB4C3A007C8E9C /* JLRRouteDefinition.m */; };
24-
5DA69C641DAB4C3A007C8E9C /* JLRRouteRequest.h in Headers */ = {isa = PBXBuildFile; fileRef = 5DA69C581DAB4C3A007C8E9C /* JLRRouteRequest.h */; };
25-
5DA69C651DAB4C3A007C8E9C /* JLRRouteRequest.h in Headers */ = {isa = PBXBuildFile; fileRef = 5DA69C581DAB4C3A007C8E9C /* JLRRouteRequest.h */; };
24+
5DA69C641DAB4C3A007C8E9C /* JLRRouteRequest.h in Headers */ = {isa = PBXBuildFile; fileRef = 5DA69C581DAB4C3A007C8E9C /* JLRRouteRequest.h */; settings = {ATTRIBUTES = (Public, ); }; };
25+
5DA69C651DAB4C3A007C8E9C /* JLRRouteRequest.h in Headers */ = {isa = PBXBuildFile; fileRef = 5DA69C581DAB4C3A007C8E9C /* JLRRouteRequest.h */; settings = {ATTRIBUTES = (Public, ); }; };
2626
5DA69C661DAB4C3A007C8E9C /* JLRRouteRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = 5DA69C591DAB4C3A007C8E9C /* JLRRouteRequest.m */; };
2727
5DA69C671DAB4C3A007C8E9C /* JLRRouteRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = 5DA69C591DAB4C3A007C8E9C /* JLRRouteRequest.m */; };
28-
5DA69C681DAB4C3A007C8E9C /* JLRRouteResponse.h in Headers */ = {isa = PBXBuildFile; fileRef = 5DA69C5A1DAB4C3A007C8E9C /* JLRRouteResponse.h */; };
29-
5DA69C691DAB4C3A007C8E9C /* JLRRouteResponse.h in Headers */ = {isa = PBXBuildFile; fileRef = 5DA69C5A1DAB4C3A007C8E9C /* JLRRouteResponse.h */; };
28+
5DA69C681DAB4C3A007C8E9C /* JLRRouteResponse.h in Headers */ = {isa = PBXBuildFile; fileRef = 5DA69C5A1DAB4C3A007C8E9C /* JLRRouteResponse.h */; settings = {ATTRIBUTES = (Public, ); }; };
29+
5DA69C691DAB4C3A007C8E9C /* JLRRouteResponse.h in Headers */ = {isa = PBXBuildFile; fileRef = 5DA69C5A1DAB4C3A007C8E9C /* JLRRouteResponse.h */; settings = {ATTRIBUTES = (Public, ); }; };
3030
5DA69C6A1DAB4C3A007C8E9C /* JLRRouteResponse.m in Sources */ = {isa = PBXBuildFile; fileRef = 5DA69C5B1DAB4C3A007C8E9C /* JLRRouteResponse.m */; };
3131
5DA69C6B1DAB4C3A007C8E9C /* JLRRouteResponse.m in Sources */ = {isa = PBXBuildFile; fileRef = 5DA69C5B1DAB4C3A007C8E9C /* JLRRouteResponse.m */; };
32+
5DE775661EA1B15200375C1D /* JLRRouteHandler.h in Headers */ = {isa = PBXBuildFile; fileRef = 5DE775641EA1B15200375C1D /* JLRRouteHandler.h */; settings = {ATTRIBUTES = (Public, ); }; };
33+
5DE775671EA1B15200375C1D /* JLRRouteHandler.h in Headers */ = {isa = PBXBuildFile; fileRef = 5DE775641EA1B15200375C1D /* JLRRouteHandler.h */; settings = {ATTRIBUTES = (Public, ); }; };
34+
5DE775681EA1B15200375C1D /* JLRRouteHandler.m in Sources */ = {isa = PBXBuildFile; fileRef = 5DE775651EA1B15200375C1D /* JLRRouteHandler.m */; };
35+
5DE775691EA1B15200375C1D /* JLRRouteHandler.m in Sources */ = {isa = PBXBuildFile; fileRef = 5DE775651EA1B15200375C1D /* JLRRouteHandler.m */; };
3236
D0C20A3017061066007746A6 /* JLRoutes.h in Headers */ = {isa = PBXBuildFile; fileRef = 5D33681A16C6DC9300F983AA /* JLRoutes.h */; settings = {ATTRIBUTES = (Public, ); }; };
3337
/* End PBXBuildFile section */
3438

@@ -61,6 +65,8 @@
6165
5DA69C591DAB4C3A007C8E9C /* JLRRouteRequest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JLRRouteRequest.m; sourceTree = "<group>"; };
6266
5DA69C5A1DAB4C3A007C8E9C /* JLRRouteResponse.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JLRRouteResponse.h; sourceTree = "<group>"; };
6367
5DA69C5B1DAB4C3A007C8E9C /* JLRRouteResponse.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JLRRouteResponse.m; sourceTree = "<group>"; };
68+
5DE775641EA1B15200375C1D /* JLRRouteHandler.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JLRRouteHandler.h; sourceTree = "<group>"; };
69+
5DE775651EA1B15200375C1D /* JLRRouteHandler.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JLRRouteHandler.m; sourceTree = "<group>"; };
6470
/* End PBXFileReference section */
6571

6672
/* Begin PBXFrameworksBuildPhase section */
@@ -144,6 +150,8 @@
144150
children = (
145151
5DA69C561DAB4C3A007C8E9C /* JLRRouteDefinition.h */,
146152
5DA69C571DAB4C3A007C8E9C /* JLRRouteDefinition.m */,
153+
5DE775641EA1B15200375C1D /* JLRRouteHandler.h */,
154+
5DE775651EA1B15200375C1D /* JLRRouteHandler.m */,
147155
5DA69C581DAB4C3A007C8E9C /* JLRRouteRequest.h */,
148156
5DA69C591DAB4C3A007C8E9C /* JLRRouteRequest.m */,
149157
5DA69C5A1DAB4C3A007C8E9C /* JLRRouteResponse.h */,
@@ -163,6 +171,7 @@
163171
files = (
164172
5DA69C691DAB4C3A007C8E9C /* JLRRouteResponse.h in Headers */,
165173
5DA69C5D1DAB4C3A007C8E9C /* JLRParsingUtilities.h in Headers */,
174+
5DE775671EA1B15200375C1D /* JLRRouteHandler.h in Headers */,
166175
5DA69C611DAB4C3A007C8E9C /* JLRRouteDefinition.h in Headers */,
167176
5C5AD9B51B45C07300ED25A3 /* JLRoutes.h in Headers */,
168177
5DA69C651DAB4C3A007C8E9C /* JLRRouteRequest.h in Headers */,
@@ -175,6 +184,7 @@
175184
files = (
176185
5DA69C681DAB4C3A007C8E9C /* JLRRouteResponse.h in Headers */,
177186
5DA69C5C1DAB4C3A007C8E9C /* JLRParsingUtilities.h in Headers */,
187+
5DE775661EA1B15200375C1D /* JLRRouteHandler.h in Headers */,
178188
5DA69C601DAB4C3A007C8E9C /* JLRRouteDefinition.h in Headers */,
179189
D0C20A3017061066007746A6 /* JLRoutes.h in Headers */,
180190
5DA69C641DAB4C3A007C8E9C /* JLRRouteRequest.h in Headers */,
@@ -301,6 +311,7 @@
301311
5C5AD9B61B45C07800ED25A3 /* JLRoutes.m in Sources */,
302312
5DA69C5F1DAB4C3A007C8E9C /* JLRParsingUtilities.m in Sources */,
303313
5DA69C631DAB4C3A007C8E9C /* JLRRouteDefinition.m in Sources */,
314+
5DE775691EA1B15200375C1D /* JLRRouteHandler.m in Sources */,
304315
);
305316
runOnlyForDeploymentPostprocessing = 0;
306317
};
@@ -313,6 +324,7 @@
313324
5D33681D16C6DC9300F983AA /* JLRoutes.m in Sources */,
314325
5DA69C5E1DAB4C3A007C8E9C /* JLRParsingUtilities.m in Sources */,
315326
5DA69C621DAB4C3A007C8E9C /* JLRRouteDefinition.m in Sources */,
327+
5DE775681EA1B15200375C1D /* JLRRouteHandler.m in Sources */,
316328
);
317329
runOnlyForDeploymentPostprocessing = 0;
318330
};

JLRoutes/Classes/JLRRouteDefinition.h

Lines changed: 90 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ NS_ASSUME_NONNULL_BEGIN
2020
/**
2121
JLRRouteDefinition is a model object representing a registered route, including the URL scheme, route pattern, and priority.
2222
23-
This class can be subclassed to customize route parsing behavior by overriding -routeResponseForRequest:decodePlusSymbols:.
23+
This class can be subclassed to customize route parsing behavior by overriding -routeResponseForRequest:.
2424
-callHandlerBlockWithParameters can also be overriden to customize the parameters passed to the handlerBlock.
2525
*/
2626

27-
@interface JLRRouteDefinition : NSObject
27+
@interface JLRRouteDefinition : NSObject <NSCopying>
2828

2929
/// The URL scheme for which this route applies, or JLRoutesGlobalRoutesScheme if global.
3030
@property (nonatomic, copy, readonly) NSString *scheme;
@@ -35,28 +35,33 @@ NS_ASSUME_NONNULL_BEGIN
3535
/// The priority of this route pattern.
3636
@property (nonatomic, assign, readonly) NSUInteger priority;
3737

38+
/// The route pattern path components.
39+
@property (nonatomic, copy, readonly) NSArray <NSString *> *patternPathComponents;
40+
3841
/// The handler block to invoke when a match is found.
3942
@property (nonatomic, copy, readonly) BOOL (^handlerBlock)(NSDictionary *parameters);
4043

44+
/// Check for route definition equality.
45+
- (BOOL)isEqualToRouteDefinition:(JLRRouteDefinition *)routeDefinition;
4146

42-
///---------------------------------
47+
48+
///----------------------------------
4349
/// @name Creating Route Definitions
44-
///---------------------------------
50+
///----------------------------------
4551

4652

4753
/**
4854
Creates a new route definition. The created definition can be directly added to an instance of JLRoutes.
4955
5056
This is the designated initializer.
5157
52-
@param scheme The URL scheme this route applies for, or JLRoutesGlobalRoutesScheme if global.
5358
@param pattern The full route pattern ('/foo/:bar')
5459
@param priority The route priority, or 0 if default.
5560
@param handlerBlock The handler block to call when a successful match is found.
5661
5762
@returns The newly initialized route definition.
5863
*/
59-
- (instancetype)initWithScheme:(NSString *)scheme pattern:(NSString *)pattern priority:(NSUInteger)priority handlerBlock:(BOOL (^)(NSDictionary *parameters))handlerBlock NS_DESIGNATED_INITIALIZER;
64+
- (instancetype)initWithPattern:(NSString *)pattern priority:(NSUInteger)priority handlerBlock:(BOOL (^)(NSDictionary *parameters))handlerBlock NS_DESIGNATED_INITIALIZER;
6065

6166
/// Unavailable, use initWithScheme:pattern:priority:handlerBlock: instead.
6267
- (instancetype)init NS_UNAVAILABLE;
@@ -65,6 +70,19 @@ NS_ASSUME_NONNULL_BEGIN
6570
+ (instancetype)new NS_UNAVAILABLE;
6671

6772

73+
///----------------------------------
74+
/// @name Responding To Registration
75+
///----------------------------------
76+
77+
78+
/**
79+
Called when the route has been registered for the given scheme.
80+
81+
@param scheme The scheme this route has become active for.
82+
*/
83+
- (void)didBecomeRegisteredForScheme:(NSString *)scheme;
84+
85+
6886
///-------------------------------
6987
/// @name Matching Route Requests
7088
///-------------------------------
@@ -74,11 +92,10 @@ NS_ASSUME_NONNULL_BEGIN
7492
Creates and returns a JLRRouteResponse for the provided JLRRouteRequest. The response specifies if there was a match or not.
7593
7694
@param request The JLRRouteRequest to create a response for.
77-
@param decodePlusSymbols The global plus symbol decoding option value.
7895
7996
@returns An JLRRouteResponse instance representing the result of attempting to match request to thie route definition.
8097
*/
81-
- (JLRRouteResponse *)routeResponseForRequest:(JLRRouteRequest *)request decodePlusSymbols:(BOOL)decodePlusSymbols;
98+
- (JLRRouteResponse *)routeResponseForRequest:(JLRRouteRequest *)request;
8299

83100

84101
/**
@@ -90,6 +107,71 @@ NS_ASSUME_NONNULL_BEGIN
90107
*/
91108
- (BOOL)callHandlerBlockWithParameters:(NSDictionary *)parameters;
92109

110+
111+
///---------------------------------
112+
/// @name Creating Match Parameters
113+
///---------------------------------
114+
115+
116+
/**
117+
Creates and returns the full set of match parameters to be passed as part of a valid match.
118+
Subclasses can override this method to mutate the match parameters, or simply call it to generate the expected value.
119+
120+
@param request The request being routed.
121+
@param routeVariables The parsed route variables (aka a route of '/route/:param' being routed with '/foo/bar' would create [ 'param' : 'bar' ])
122+
123+
@returns The full set of match parameters to be passed as part of a valid match.
124+
@see defaultMatchParametersForRequest:
125+
@see routeVariablesForRequest:
126+
*/
127+
- (NSDictionary *)matchParametersForRequest:(JLRRouteRequest *)request routeVariables:(NSDictionary <NSString *, NSString *> *)routeVariables;
128+
129+
130+
/**
131+
Creates and returns the default base match parameters for a given request. Does not include any parsed fields.
132+
133+
@param request The request being routed.
134+
135+
@returns The default match parameters for a given request. Only includes key/value pairs for JLRoutePatternKey, JLRouteURLKey, and JLRouteSchemeKey.
136+
*/
137+
- (NSDictionary *)defaultMatchParametersForRequest:(JLRRouteRequest *)request;
138+
139+
140+
///-------------------------------
141+
/// @name Parsing Route Variables
142+
///-------------------------------
143+
144+
145+
/**
146+
Parses and returns route variables for the given request.
147+
148+
@param request The request to parse variable values from.
149+
150+
@returns The parsed route variables if there was a match, or nil if it was not a match.
151+
*/
152+
- (nullable NSDictionary <NSString *, NSString *> *)routeVariablesForRequest:(JLRRouteRequest *)request;
153+
154+
155+
/**
156+
Parses value into a variable name, including stripping out any extra characters if needed.
157+
158+
@param value The raw string value that should be parsed into a variable name.
159+
160+
@returns The variable name to use as the key of a key/value pair in the parsed route variables.
161+
*/
162+
- (NSString *)routeVariableNameForValue:(NSString *)value;
163+
164+
165+
/**
166+
Parses value into a variable value, including stripping out any extra characters if needed.
167+
168+
@param value The raw string value that should be parsed into a variable value.
169+
170+
@returns The variable value to use as the value of a key/value pair in the parsed route variables.
171+
*/
172+
- (NSString *)routeVariableValueForValue:(NSString *)value;
173+
174+
93175
@end
94176

95177

0 commit comments

Comments
 (0)