Code Monkey home page Code Monkey logo

class-helpers's Issues

Expand TByteHelper

Add methods:

  • GenerateBase64Code - Fake TByte Generator
  • Compress and Decompress bytes - using ZLib
  • CompareBytesTo (TBytes)
  • Zeroise
  • CopySector (aIndex, aLength): TBytes;
  • LoDashToChunks (aChunkSize): TArray;
  • LoDashDrop (aToDrop = 1): TBytes
  • LoDashDropWhile (aByteToLocate): TBytes;

More methods bellow: ...

Add TStream helper

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;

Refactor tests TDate2019_10_24_T_21_15_59

Refactor tests for record helper TDateTime (unit: Test.Helper.TDateTime)

Refactoring 1

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;

Refactoring 2

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;

New helper for TField

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;

Add TDataSet.AppendRows

function TDataSet.AppendRows (aRecordArray: TArray<TArray<Variant>>);

Verify and rise more precise exceptions when:

  • assign string to TIntegerField or any other numeric field
  • assign string to TBooleanField
  • assign string to TDateField

Fix mapping attribute name and unit

Change: Maped -> Mapped

  • unit: Attribute.MapedToField.pas => Attribute.MappedToField.pas
  • Class: MapedToFieldAttribute => MappedToFieldAttribute
  • Custom attribute: [MapedToField(..)] => [MappedToField(..)] in:
    • unit tests
    • sample playground

Add TForm class helper

  • TFormHelper:
    1. function SetInterval
    2. procedure SetTimeout
    3. OnFormReady(TProc)

Find garbage collector and code in Proxy Generator App tool in unit: Utils.Timer.Interval.pas

TPicture helper

TPictureHelper:

  1. AssignBytes (const aBytes: TBytes); details bellow
  2. AssignBlobField - tests format and creates proper TGraphics
    • check: Image1.Picture := fDataSet.ImageBlobField - comments bellow

Sample for TDataSetHelper.LoadData

Add demo code in playground project for helper method TDataSet.LoadData. Load any dataset into collection of objects and do some manipulations/calculations.

Add method TDataSet.LoadData: TObjectList

function TDataSet.LoadData<T:class>: TObjectList<T>;

Method steps:

  1. Creates TObjectList<T>
  2. Iterates through all rows of the dataset and for each row:
    1. Creates T class
    2. Fills class fields with mapped dataset fields (mapping is defined using Delphi Custom Attributes)
  • Custom Attributes should be defined in a separate unit
  • Consider to decompose this method with TFieldHelper
  • Consider this dependency Helper.TDataSet.pas => Helper.TField.pas

XMLDoc for TDataSet helper

  1. Add minimal XML Documentation (summary) fo class helper TDataSetHelper
  2. Expand documentation for method: LoadData

Expand helper library

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;

XML documentation for loading DBGrid columns from JSON

Add XMLDoc for class helper TDBGridHelper method LoadColumnsFromJson

type
  TDBGridHelper = class helper for TDBGrid
    // ...
    procedure LoadColumnsFromJson(aStoredColumns: TJSONArray);
    procedure LoadColumnsFromJsonString(const aJsonString: string);
  end;

Add TStreamHelper.ToHexString

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;

JSON datetime conversion into TDateTime

  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;

https://github.com/synopse/mORMot/blob/f2f0b0fe5987ab6c15a52bf5538e818e2ef48250/CrossPlatform/SynCrossPlatformJSON.pas#L758-L830

New helper for TBytes

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;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.