Commits

Anonymous committed 2dcdb4f

Added check for the stored flag on published properties.
Don't have exclude/include on storage class.
Added tests for checkstored flag.

  • Participants
  • Parent commits 4e59186

Comments (0)

Files changed (2)

File Src/ASConfig.pas

 
     FIncludeRegExs: TPerlRegExList;
     FExcludeRegExs: TPerlRegExList;
+    FCheckIsStored: Boolean;
 
     function GetStorePublic: Boolean;
     function GetStorePublished: Boolean;
     procedure Clear(ClearParents: Boolean = False); virtual;
 
 
+    property CheckIsStored: Boolean read FCheckIsStored write FCheckIsStored default True;
     property DeclaredOnly: Boolean read FDeclaredOnly write FDeclaredOnly default True;
     property IncludePropertyNames: TStrings read FIncludePropertyNames write SetIncludePropertyNames;
     property ExcludePropertyNames: TStrings read FExcludePropertyNames write SetExcludePropertyNames;
 
   TASConfigIniFile = class(TASCustomConfigIniFile)
   published
+    property CheckIsStored;
     property DeclaredOnly;
     property IncludePropertyNames;
     property ExcludePropertyNames;
     procedure SetRootKeyName(const Value: string);
     function GetRootKeyNameStored: Boolean;
   published
+    property CheckIsStored;
     property DeclaredOnly;
     property IncludePropertyNames;
     property ExcludePropertyNames;
   private
     FConfig: TASCustomConfig;
     FOnConfigChange: TNotifyEvent;
-    FIncludePropertyNames: TStrings;
-    FExcludePropertyNames: TStrings;
     procedure DoOnConfigChange;
     procedure SetConfig(const Value: TASCustomConfig);
-    procedure SetExcludePropertyNames(const Value: TStrings);
-    procedure SetIncludePropertyNames(const Value: TStrings);
   public
-    constructor Create(AOwner: TComponent); override;
-    destructor Destroy; override;
-
     procedure Save(const Obj: TComponent); overload;
     procedure Save(const Obj: TObject; const Path: string = 'Default'); overload;
 
   published
     property Config: TASCustomConfig read FConfig write SetConfig;
     property OnConfigChange: TNotifyEvent read FOnConfigChange write FOnConfigChange;
-    property IncludePropertyNames: TStrings read FIncludePropertyNames write SetIncludePropertyNames;
-    property ExcludePropertyNames: TStrings read FExcludePropertyNames write SetExcludePropertyNames;
   end;
 
 procedure Register;
 constructor TASCustomConfig.Create(AOwner: TComponent);
 begin
   inherited;
+  FCheckIsStored := True;
   FIncludePropertyNames := StringsClass.Create;
   FExcludePropertyNames := StringsClass.Create;
   FSearchPath := StringsClass.Create;
           Continue;
       end;
 
+      if CheckIsStored
+      and (P.Visibility = mvPublished)
+      and not IsStoredProp(Obj, P.Name) then
+        Continue;
+
       L := LoadMethodMap[P.PropertyType.TypeKind];
       if Assigned(L) then begin
         if P.PropertyType.TypeKind = tkClass then
           Continue;
       end;
 
+      if CheckIsStored
+      and (P.Visibility = mvPublished)
+      and not IsStoredProp(Obj, P.Name) then
+        Continue;
+
       S := SaveMethodMap[P.PropertyType.TypeKind];
       if Assigned(S) then
         S(Prefix + P.Name, P.GetValue(Obj));
 
 { TASConfigStore }
 
-constructor TASConfigStore.Create(AOwner: TComponent);
-begin
-  inherited;
-
-  FIncludePropertyNames := StringsClass.Create;
-  FExcludePropertyNames := StringsClass.Create;
-end;
-
-destructor TASConfigStore.Destroy;
-begin
-  FreeAndNil(FIncludePropertyNames);
-  FreeAndNil(FExcludePropertyNames);
-  inherited;
-end;
-
 procedure TASConfigStore.DoOnConfigChange;
 begin
   if Assigned(FOnConfigChange) then
 
 procedure TASConfigStore.Load(const Obj: TObject; const Path: string);
 begin
-  with Config do begin
-    IncludePropertyNames := Self.IncludePropertyNames;
-    ExcludePropertyNames := Self.ExcludePropertyNames;
-    Load(Obj, Path);
-  end;
+  Assert(Assigned(Config), 'No config file assigned to store');
+  Config.Load(Obj, Path);
 end;
 
 procedure TASConfigStore.Load(const Obj: TComponent);
 
 procedure TASConfigStore.Save(const Obj: TObject; const Path: string);
 begin
-  with Config do begin
-    IncludePropertyNames := Self.IncludePropertyNames;
-    ExcludePropertyNames := Self.ExcludePropertyNames;
-    Save(Obj, Path);
-  end;
+  Assert(Assigned(Config), 'No config file assigned to store');
+  Config.Save(Obj, Path);
 end;
 
 procedure TASConfigStore.SetConfig(const Value: TASCustomConfig);
   end;
 end;
 
-procedure TASConfigStore.SetExcludePropertyNames(const Value: TStrings);
-begin
-  FExcludePropertyNames.Assign(Value);
-end;
-
-procedure TASConfigStore.SetIncludePropertyNames(const Value: TStrings);
-begin
-  FIncludePropertyNames.Assign(Value);
-end;
-
 { TASCustomConfigRegistry }
 
 procedure TASCustomConfigRegistryIniFile.ClearIniFile;

File Test/TestASConfig.pas

     procedure TestLoadExclude;
     procedure TestSaveStringList;
     procedure TestLoadStringList;
+    procedure TestSaveStored;
+    procedure TestSaveNonStored;
+    procedure TestLoadStored;
+    procedure TestLoadNonStored;
   end;
 
   TestTASConfigIniFile = class(TestTASCustomConfig)
   private
     FAnotherProp: Boolean;
     FMyEnum: TMyEnum;
+    FStoredProp: string;
+    FIsStoredPropStored: Boolean;
+    function GetIsStoredPropStored: Boolean;
   public
     property MyEnum: TMyEnum read FMyEnum write FMyEnum;
   published
     property AnotherProp: Boolean read FAnotherProp write FAnotherProp;
+    property IsStoredPropStored: Boolean read GetIsStoredPropStored write FIsStoredPropStored;
+    property StoredProp: string read FStoredProp write FStoredProp stored GetIsStoredPropStored;
   end;
 
   TTestCollectionItem = class(TCollectionItem)
 
 procedure TestTASCustomConfig.TearDown;
 begin
+  Config.Clear;
   FreeAndNil(FASCustomConfig);
 end;
 
   end;
 end;
 
+procedure TestTASCustomConfig.TestLoadNonStored;
+var
+  Obj: TTestDescendent;
+  Ini: IASConfigFile;
+begin
+  Ini := CreateIniFile;
+  Ini.WriteString('Default', 'IsStoredPropStored', '0');
+  Ini.WriteString('Default', 'StoredProp', 'my stored prop');
+  Ini := nil;
+
+  Obj := TTestDescendent.Create;
+  try
+    Obj.StoredProp := '<default>';
+    Config.CheckIsStored := True;
+    Config.DeclaredOnly := True;
+    Config.StorePublished := True;
+
+    Config.Load(Obj);
+
+    CheckEquals('<default>', Obj.StoredProp);
+  finally
+    Obj.Free;
+  end;
+end;
+
+procedure TestTASCustomConfig.TestLoadStored;
+var
+  Obj: TTestDescendent;
+  Ini: IASConfigFile;
+begin
+  Ini := CreateIniFile;
+  Ini.WriteString('Default', 'IsStoredPropStored', '1');
+  Ini.WriteString('Default', 'StoredProp', 'my stored prop');
+  Ini := nil;
+
+  Obj := TTestDescendent.Create;
+  try
+    Obj.StoredProp := '<default>';
+    Config.CheckIsStored := True;
+    Config.DeclaredOnly := True;
+    Config.StorePublished := True;
+
+    Config.Load(Obj);
+
+    CheckEquals('my stored prop', Obj.StoredProp);
+  finally
+    Obj.Free;
+  end;
+end;
+
 procedure TestTASCustomConfig.TestLoadStringList;
 var
   S: TStringList;
 begin
-  Config.Clear;
-
   S := TStringList.Create;
   try
     with CreateIniFile do begin
   CheckEqualsString('meBar', Ini.ReadString('Default', 'MyEnum', ''));
 end;
 
+procedure TestTASCustomConfig.TestSaveNonStored;
+var
+  Obj: TTestDescendent;
+  Ini: IASConfigFile;
+begin
+  Obj := TTestDescendent.Create;
+  try
+    Obj.StoredProp := 'my stored prop';
+    Obj.IsStoredPropStored := False;
+
+    Config.CheckIsStored := True;
+    Config.DeclaredOnly := True;
+    Config.StorePublished := True;
+    Config.Save(Obj);
+  finally
+    Obj.Free;
+  end;
+
+  Ini := CreateIniFile;
+  CheckEqualsString('<default>', Ini.ReadString('Default', 'StoredProp', '<default>'));
+end;
+
+procedure TestTASCustomConfig.TestSaveStored;
+var
+  Obj: TTestDescendent;
+  Ini: IASConfigFile;
+begin
+  Obj := TTestDescendent.Create;
+  try
+    Obj.StoredProp := 'my stored prop';
+    Obj.IsStoredPropStored := True;
+
+    Config.CheckIsStored := True;
+    Config.DeclaredOnly := True;
+    Config.StorePublished := True;
+    Config.Save(Obj);
+  finally
+    Obj.Free;
+  end;
+
+  Ini := CreateIniFile;
+  CheckEqualsString('my stored prop', Ini.ReadString('Default', 'StoredProp', ''));
+end;
+
 procedure TestTASCustomConfig.TestSaveStringList;
 var
   S: TStringList;
 begin
-  Config.Clear;
-
   S := TStringList.Create;
   try
     S.Add('First line');
 begin
   Config := TASConfigIniFile.Create(Application);
   (Config as TASConfigIniFile).FileName := './as-test.ini';
+
+  inherited;
 end;
 
 procedure TestTASConfigIniFile.TestClearIniFile;
 begin
   Config := TASConfigRegistryIniFile.Create(Application);
   (Config as TASConfigRegistryIniFile).Path := '\Software\ASTEKK\Test';
+
+  inherited;
 end;
 
 
   inherited;
 end;
 
+{ TTestDescendent }
+
+function TTestDescendent.GetIsStoredPropStored: Boolean;
+begin
+  Result := FIsStoredPropStored;
+end;
+
 initialization
   // Register any test cases with the test runner
   RegisterTest(TestTASConfigIniFile.Suite);