@@ -11,6 +11,7 @@ interface
1111type
1212 TRALRoutes = class ;
1313 TRALOnReply = procedure(ARequest: TRALRequest; AResponse: TRALResponse) of object ;
14+ TRALOnReplyGen = procedure(ARequest: TRALRequest; AResponse: TRALResponse);
1415
1516 // swagger defines
1617 // array, boolean, integer, number, object, string
@@ -23,7 +24,7 @@ TRALRouteParam = class(TCollectionItem)
2324 FDescription: TStrings;
2425 FParamName: StringRAL;
2526 FParamType: TRALRouteParamType;
26- FRequired : boolean;
27+ FRequired: boolean;
2728 protected
2829 procedure AssignTo (Dest: TPersistent); override;
2930 function GetDisplayName : string; override;
@@ -44,7 +45,7 @@ TRALRouteParam = class(TCollectionItem)
4445 TRALRouteParams = class (TOwnedCollection)
4546 public
4647 constructor Create(AOwner: TPersistent);
47- function IndexOf (AName : StringRAL) : IntegerRAL;
48+ function IndexOf (AName: StringRAL): IntegerRAL;
4849 end ;
4950
5051 { TRALBaseRoute }
@@ -56,13 +57,14 @@ TRALBaseRoute = class(TCollectionItem)
5657 FAllowURIParams: boolean;
5758 FCallback: boolean;
5859 FDescription: TStrings;
59- FInputParams : TRALRouteParams;
60+ FInputParams: TRALRouteParams;
6061 FName: StringRAL;
6162 FRoute: StringRAL;
6263 FSkipAuthMethods: TRALMethods;
63- FURIParams : TRALRouteParams;
64+ FURIParams: TRALRouteParams;
6465
6566 FOnReply: TRALOnReply;
67+ FOnReplyGen: TRALOnReplyGen;
6668 protected
6769 procedure AssignTo (Dest: TPersistent); override;
6870 function GetDisplayName : string; override;
@@ -95,6 +97,7 @@ TRALBaseRoute = class(TCollectionItem)
9597 property URIParams: TRALRouteParams read FURIParams write FURIParams;
9698
9799 property OnReply: TRALOnReply read FOnReply write FOnReply;
100+ property OnReplyGen: TRALOnReplyGen read FOnReplyGen write FOnReplyGen;
98101 published
99102 property Description: TStrings read FDescription write SetDescription;
100103 property InputParams: TRALRouteParams read FInputParams write FInputParams;
@@ -114,12 +117,26 @@ TRALRoute = class(TRALBaseRoute)
114117 property URIParams;
115118
116119 property OnReply;
120+ public
121+ property OnReplyGen;
117122 end ;
118123
119124 { TRALRoutes }
120125
121126 // / Collection class to store all route definitions
122127 TRALRoutes = class (TOwnedCollection)
128+ public type
129+ // / Support enumeration of values in TRALParams.
130+ TEnumerator = class
131+ private
132+ FIndex: Integer;
133+ FArray: TRALRoutes;
134+ public
135+ constructor Create(const AArray: TRALRoutes);
136+ function GetCurrent : TRALRoute; inline;
137+ function MoveNext : Boolean; inline;
138+ property Current: TRALRoute read GetCurrent;
139+ end ;
123140 private
124141 function CompareRoutes (ARoute: TRALRoute; AQuery: StringRAL;
125142 var AWeight: IntegerRAL; AURI: TStringList): boolean;
@@ -128,7 +145,10 @@ TRALRoutes = class(TOwnedCollection)
128145 constructor Create(AOwner: TPersistent);
129146 // / Returns a list of routes separated by sLineBreak
130147 function AsString : StringRAL;
148+ // / Method that will check if the request finds a matching route
131149 function CanAnswerRoute (ARequest: TRALRequest): TRALRoute;
150+ // / Retuns the internal Enumerator type to allow for..in loops
151+ function GetEnumerator : TEnumerator; inline;
132152
133153 property Find[const ARoute: StringRAL]: TRALRoute read GetRoute;
134154 end ;
@@ -170,6 +190,8 @@ procedure TRALBaseRoute.Execute(ARequest: TRALRequest; AResponse: TRALResponse);
170190
171191 if Assigned(OnReply) then
172192 OnReply(ARequest, AResponse)
193+ else if Assigned(OnReplyGen) then
194+ OnReplyGen(ARequest, AResponse)
173195 else
174196 AResponse.Answer(HTTP_NotFound);
175197end ;
@@ -448,6 +470,11 @@ function TRALRoutes.CompareRoutes(ARoute: TRALRoute; AQuery: StringRAL;
448470 end ;
449471end ;
450472
473+ function TRALRoutes.GetEnumerator : TEnumerator;
474+ begin
475+ Result := TEnumerator.Create(Self);
476+ end ;
477+
451478function TRALRoutes.GetRoute (const ARoute: StringRAL): TRALRoute;
452479var
453480 I: integer;
@@ -495,7 +522,8 @@ function TRALRoutes.CanAnswerRoute(ARequest: TRALRequest): TRALRoute;
495522 for vInt := 0 to Pred(Self.Count) do
496523 begin
497524 vRoute := TRALRoute(Items[vInt]);
498- if CompareRoutes(vRoute, vQuery, vTempWeight, vTempUriRoute) then
525+
526+ if vRoute.IsMethodAllowed(ARequest.Method) and CompareRoutes(vRoute, vQuery, vTempWeight, vTempUriRoute) then
499527 begin
500528 if vTempWeight < vRouteWeight then
501529 begin
@@ -585,4 +613,25 @@ function TRALRouteParams.IndexOf(AName: StringRAL): IntegerRAL;
585613 end ;
586614end ;
587615
616+ { TRALRoutes.TEnumerator }
617+
618+ constructor TRALRoutes.TEnumerator.Create(const AArray: TRALRoutes);
619+ begin
620+ inherited Create;
621+ FIndex := -1 ;
622+ FArray := AArray;
623+ end ;
624+
625+ function TRALRoutes.TEnumerator .GetCurrent: TRALRoute;
626+ begin
627+ Result := TRALRoute(FArray.Items[FIndex]);
628+ end ;
629+
630+ function TRALRoutes.TEnumerator .MoveNext: Boolean;
631+ begin
632+ Result := FIndex < FArray.Count - 1 ;
633+ if Result then
634+ Inc(FIndex);
635+ end ;
636+
588637end .
0 commit comments