bogdanpolak / class-helpers Goto Github PK
View Code? Open in Web Editor NEWDirectory of Delphi VCL/RTL class helpers
License: MIT License
Directory of Delphi VCL/RTL class helpers
License: MIT License
Change: Maped -> Mapped
Attribute.MapedToField.pas
=> Attribute.MappedToField.pas
MapedToFieldAttribute
=> MappedToFieldAttribute
[MapedToField(..)]
=> [MappedToField(..)]
in:
TDateTimeHelper = record helper for TDateTime
public
function AsStringDateISO: string;
function SetDateISO (const aDateISO: string);
Update TJSONObject helper:
function GetFieldIsoDate(const fieldName: string): TDateTime;
Implement more reliable function then: System.DateUtils
implementation DateToISO8601
and ISO8601ToDate
. Check is iso date has non zero time value
Ask Arnaud for reuse functions:
function DateTimeToIso8601(Value: TDateTime): string;
function Iso8601ToDateTime(const Value: string): TDateTime;
Add XMLDoc for class helper TDBGridHelper
method LoadColumnsFromJson
type
TDBGridHelper = class helper for TDBGrid
// ...
procedure LoadColumnsFromJson(aStoredColumns: TJSONArray);
procedure LoadColumnsFromJsonString(const aJsonString: string);
end;
type
TBytesHelper = record helper for TBytes
// ---------------------
// Base / utility methods:
function GetSize: Integer;
procedure SetSize(aSize: Integer);
property Size: Integer read GetSize write SetSize;
// ---------------------
// Load / Save
procedure LoadFromStream(const aStream: TStream);
procedure LoadFromFile(const aFileName: string);
procedure SaveToStream(const aStream: TStream);
procedure LoadFromBase64String(const aBase64Str: String);
// ---------------------
// Getters
function GetSectorAsHex(aIndex: Integer = 0;
aLength: Integer = 100): string;
function GetSectorAsString(aIndex: Integer = 0;
aLength: Integer = 100): string;
function GetLongWord(aIndex: Integer = 0): LongWord;
function GetReverseLongWord(aIndex: Integer = 0): LongWord;
function GetSectorCRC32(aIndex: Integer; aLength: Integer): LongWord;
// ---------------------
// Fake generator
function GenerateBase64Code(aLineLength: Integer = 60): string;
end;
TDataSetHelper
LoadData
Add xml doc for all methods in TBytesHelper
See article:
http://blogs.conceptfirst.com/2006/05/08/class-helpers-good-or-bad.html
TCanvasHelper = class helper for TCanvas
procedure FillGradient( Bounds: TRect; StartColour, EndColour: TColor; IsHorizontal: boolean );
end;
TListHelper = class helper for TList
procedure FreeSelfAndContainedObjects;
end;
TDatasetHelper = class helper for TDataSet
public
procedure InsertIntoStrings( Strings: TStrings; NameField: string; IndexField: string = '' );
function HaveFieldsChanged: boolean;
procedure PostIfEditing;
end;
TFieldHelper = class helper for TField
public
function HasChanged: boolean;
end;
TTreeNodesHelper = class helper for TTreeNodes
public
procedure ExpandToLevel( Level: integer );
function FindOrCreateNode( NodePath: string ): TTreeNode;
end;
TRectHelper = record helper for TRect
function ContainsPoint( Point: TPoint ): boolean;
end;
examples/01-playground
In GetFieldIsoDate there is time value even when provided ISO date string has no time.
For example:
js := TJSONObject.Create(TJSONPair.Create('visited','2018-11-25'));
dt := js.GetFieldIsoDate('visited');
// dt - expected 43429 but got 43429,0416666667
type
TFieldHelper = class helper for TField
function SetBlobFromBase64String(const aBase64Str: String): TBlobField;
end;
function TFieldHelper. SetBlobFromBase64String(const aBase64Str: String)
: TBlobField;
begin
if not(Self is TBlobField) then
raise EDatabaseError.Create('Invalid field type.' +
' Method LoadFromBase64String is supported only for blob fields');
Result := (Self as TBlobField);
Result.Value := TNetEncoding.Base64.DecodeStringToBytes(aBase64Str);
end;
Sample usage:
with aDataSet do
begin
Edit;
FieldByName('PngIcon'). SetBlobFromBase64String
('iVBORw0KGgoAAAANSUhEUgAAAHwAAAAbCAMAAACJMRtuAAAAclBMVEX////w' +
'8PDZ2dn/248AAADb/v85OY85AwCP2////bYAOZDbjzm2/v9mtv9mCAA5j9vb' +
'248BDmZmDDlmFGb//tv/tmbb/LYABDkAZra22484EGaQOgA4ZWW2ZgC2ZmaP' +
'OTiPZQC2/LaPZo86OmZmOTk6Bznxkw7DAAABV0lEQVRIx+1W7W6DMAzMSsKA' +
'hAIbtKys+977v+IOX5WBWDfWPxEqVpWcE/sOOylC3QS0axbfBDKKqyAm4ptA' +
'4uErX6Z4FJuLw6fiVmtdzCagE90iKYdXd5kZczSA7Vxxi+z6zfxPHAMeIAd4' +
'/0jIkW5F3VZOqSc3U/wuwSC17JMofkFBHlfOaqC6Q1myJhVnFOdgi2OhUuGQ' +
'tfrZ01XuFM7UH8X5ZOm9Q3aJYpps57G0se1pub/b5l5XcqDVgB2euNg4OKFD' +
'oG1TCSedOSOOyrKyP8TqITaI85hnK+SRXxuJY0bZA3FCHsthSOfOth0RsWHO' +
'GH8m/cyf3x+03WpYMWg74EQc428Xjg1lzhibfh+97DtIavjcxFLdYRV9n1y4' +
'V2k7w0H3x1+NDS4p6LE6av0YG0wFD6ChL9H7xBcrHPk3XXu6cAxn6vLfcKv4' +
'Kr6oLxmYWi2EfQE93BbPGkadYAAAAABJRU5ErkJggg==');
Post;
end;
function TStreamHelper.ToHexString: string;
var
aBytes: TBytes;
idx: Integer;
begin
SetLength(aBytes, Self.Size);
Self.Position := 0;
fStream.Read(aBytes[0], Self.Size);
for idx := 0 to High(aBytes) do
if idx = 0 then
Result := IntToHex(aBytes[0], 2)
else
Result := Result + ' ' + IntToHex(aBytes[i], 2);
end;
Fields in JSON object defining column format is now case sensitive and this fields are not supported:
fDBGrid.LoadColumnsFromJsonString(
'[ {"fieldName":"id", "TITLE":"CityID", "Witdth":90} ]'
);
ReleaseDate
ReleaseVersion
==> Version
version_bumper
toolAdd project using class helpers in action
TStreamHelper = class helper for TStream
public
procedure SaveToFile(aFileName: string);
function SaveToTempFile: string;
function AsString(aEncoding: TEncoding = FUTF8): string;
procedure WriteString(const aText: string; aEncoding: TEncoding = FUTF8);
procedure WriteLine(const aText: string; aEncoding: TEncoding = FUTF8);
end;
function TDataSet.LoadData<T:class>: TObjectList<T>;
Method steps:
TObjectList<T>
TFieldHelper
Helper.TDataSet.pas
=> Helper.TField.pas
Method TDBGridHelper.AutoSizeColumns
calculates to narrow columns currency fields (TCurrencyField
).
Helper method TDBGridHelper.AutoSizeColumns
using Text
value of this field which is not including full currency format width. See image bellow
Add method:
TDBGridHelper.LoadColumnsFromJson (aStoredColumns:TJSONArray)
JSON array schema needs TBD
Add demo code in playground project for helper method TDataSet.LoadData
. Load any dataset into collection of objects and do some manipulations/calculations.
Write in comments bellow proposals for good practices
Update "dark side" section: http://www.tindex.net/Language/classhelper.html
function TDBGridHelper.SaveColumnsToJson: TJSONArray;
// or
function TDBGridHelper.SaveColumnsToJson (aJArray: TJSONArray): TJSONArray;
Add XML documentation for:
type
TDBGridHelper = class helper for TDBGrid
...
function SaveColumnsToString: string;
end;
Update vesion_bumper
tool with TCommand Patter clone:
https://github.com/bogdanpolak/command-delphi
Refactor tests for record helper TDateTime (unit: Test.Helper.TDateTime
)
IS
procedure TDate2019_10_24_T_21_15_59.Test_AsYear;
begin
Assert.AreEqual(2019, fDate.AsYear);
end;
SHOULD BE
procedure TDateHelper.AsYear_2019;
var
actualYear: word;
begin
fDate := EncodeDate(2019, 10, 24);
actualYear := fDate.AsYear;
Assert.AreEqual(2019, actualYear);
end;
REMOVE from test setup
FormatSettings.DateSeparator := '-';
FormatSettings.ShortDateFormat := 'yyyy/mm/dd';
ADD where it's required
original := FormatSettings.DateSeparator;
FormatSettings.DateSeparator := '-';
FormatSettings.ShortDateFormat := 'yyyy/mm/dd';
// ...
// ...
FormatSettings.DateSeparator := original;
Update "dark side" section: http://www.tindex.net/Language/classhelper.html
Add methods:
More methods bellow: ...
function TDataSet.AppendRows (aRecordArray: TArray<TArray<Variant>>);
Verify and rise more precise exceptions when:
Look for:
helpers/Helper.TFDCustomManager
TFDConnection
helperTPictureHelper:
AssignBytes (const aBytes: TBytes);
details bellowAssignBlobField
- tests format and creates proper TGraphics
Image1.Picture := fDataSet.ImageBlobField
- comments bellowCompress and Decompress bytes - using ZLib
ZLibCompression
Review other potential extensions for TBytes
Add version
Helper.TBytes.md
Helper.TDataSet.md
Helper.TJSONObject.md
Helper.TStream.md
Sample methods:
Review https://lodash.com for more ideas
function SetInterval
procedure SetTimeout
OnFormReady(TProc)
Find garbage collector and code in Proxy Generator App tool in unit: Utils.Timer.Interval.pas
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.