forked from malcolmgroves/FluentQuery
-
Notifications
You must be signed in to change notification settings - Fork 4
/
FluentQuery.GenericObjects.MethodFactories.pas
145 lines (130 loc) · 5.54 KB
/
FluentQuery.GenericObjects.MethodFactories.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
{****************************************************}
{ }
{ FluentQuery }
{ }
{ Copyright (C) 2013 Malcolm Groves }
{ }
{ http://www.malcolmgroves.com }
{ }
{****************************************************}
{ }
{ This Source Code Form is subject to the terms of }
{ the Mozilla Public License, v. 2.0. If a copy of }
{ the MPL was not distributed with this file, You }
{ can obtain one at }
{ }
{ http://mozilla.org/MPL/2.0/ }
{ }
{****************************************************}
unit FluentQuery.GenericObjects.MethodFactories;
interface
uses
FluentQuery.Core.MethodFactories, System.SysUtils, System.TypInfo, System.Rtti,
FluentQuery.Integers;
type
TGenericObjectMethodFactory<T : class> = class(TMethodFactory<T>)
public
// predicates
class function IsA(AClass : TClass): TPredicate<T>;
class function IsAssigned: TPredicate<T>;
class function PropertyNamedOfType(const Name: string; PropertyType: TTypeKind): TPredicate<T>;
class function IntegerPropertyNamedWithValue(const Name: string; Predicate: TPredicate<Integer>): TPredicate<T>;
class function StringPropertyNamedWithValue(const Name: string; Predicate : TPredicate<string>): TPredicate<T>;
class function BooleanPropertyNamedWithValue(const Name: string; const Value: Boolean): TPredicate<T>;
end;
implementation
{ TGenericObjectPredicateFactory<T> }
class function TGenericObjectMethodFactory<T>.BooleanPropertyNamedWithValue(
const Name: string; const Value: Boolean): TPredicate<T>;
begin
Result := function (ComponentValue : T) : boolean
var
LRTTIContext : TRTTIContext;
LClassType : TRTTIType;
LProperty : TRTTIProperty;
begin
Result := False;
LRTTIContext := TRttiContext.Create;
LClassType := LRTTIContext.GetType(ComponentValue.ClassInfo);
LProperty := LClassType.GetProperty(Name);
if Assigned(LProperty) then
if LProperty.PropertyType is TRttiEnumerationType then
if TRttiEnumerationType(LProperty.PropertyType).UnderlyingType.Handle = System.TypeInfo(Boolean) then
Result := LProperty.GetValue(TObject(ComponentValue)).AsBoolean = Value;
end;
end;
class function TGenericObjectMethodFactory<T>.IntegerPropertyNamedWithValue(
const Name: string; Predicate: TPredicate<Integer>): TPredicate<T>;
begin
Result := function (ComponentValue : T) : boolean
var
LRTTIContext : TRTTIContext;
LClassType : TRTTIType;
LProperty : TRTTIProperty;
begin
Result := False;
LRTTIContext := TRttiContext.Create;
LClassType := LRTTIContext.GetType(ComponentValue.ClassInfo);
LProperty := LClassType.GetProperty(Name);
if Assigned(LProperty) then
if LProperty.PropertyType.TypeKind = tkInteger then
begin
Result := Predicate(LProperty.GetValue(TObject(ComponentValue)).AsOrdinal);
end;
end;
end;
class function TGenericObjectMethodFactory<T>.IsA(
AClass: TClass): TPredicate<T>;
begin
Result := function (Value : T): boolean
begin
Result := Value is AClass;
end;
end;
class function TGenericObjectMethodFactory<T>.IsAssigned: TPredicate<T>;
begin
Result := function (Value : T): boolean
begin
Result := Assigned(Value);
end;
end;
class function TGenericObjectMethodFactory<T>.PropertyNamedOfType(
const Name: string; PropertyType: TTypeKind): TPredicate<T>;
begin
Result := function (Value : T) : boolean
var
LRTTIContext : TRTTIContext;
LClassType : TRTTIType;
LProperty : TRTTIProperty;
begin
Result := False;
LRTTIContext := TRttiContext.Create;
LClassType := LRTTIContext.GetType(Value.ClassInfo);
LProperty := LClassType.GetProperty(Name);
if Assigned(LProperty) then
Result := LProperty.PropertyType.TypeKind = PropertyType;
end;
end;
class function TGenericObjectMethodFactory<T>.StringPropertyNamedWithValue(
const Name : string; Predicate : TPredicate<string>): TPredicate<T>;
begin
Result := function (ComponentValue : T) : boolean
var
LRTTIContext : TRTTIContext;
LClassType : TRTTIType;
LProperty : TRTTIProperty;
LPropValue : string;
begin
Result := False;
LRTTIContext := TRttiContext.Create;
LClassType := LRTTIContext.GetType(ComponentValue.ClassInfo);
LProperty := LClassType.GetProperty(Name);
if Assigned(LProperty) then
if LProperty.PropertyType.TypeKind = tkUString then
begin
LPropValue := LProperty.GetValue(TObject(ComponentValue)).AsString;
Result := Predicate(LPropValue);
end;
end;
end;
end.