public void Saving_Example() { using (var f = new TempfileLife()) { var rpath = JsonSettings.ResolvePath(f); StrongBox <int> saved = new StrongBox <int>(0); var o = JsonSettings.Load <ExampleNotifyingSettings>(f).EnableAutosave(); saved.Value.Should().Be(0); o.AfterSave += (s, destinition) => { saved.Value++; }; o.Residents.Add("Cookie Monster"); //Boom! saves. saved.Value.Should().Be(1); o.Residents = new ObservableCollection <string>(); //Boom! saves. saved.Value.Should().Be(2); o.Residents.Add("Cookie Monster"); //Boom! saves. saved.Value.Should().Be(3); o.NonAutosavingProperty = new ObservableCollection <object>(); //doesn't save o.NonAutosavingProperty.Add("Jim"); //doesn't save saved.Value.Should().Be(3); o.Street += "-1"; //Boom! saves. saved.Value.Should().Be(4); o.AutoProperty = "Hello"; //Boom! saves. saved.Value.Should().Be(5); o.IgnoredFromAutosaving = "Hello"; //doesn't save saved.Value.Should().Be(5); } }
public void SavingInterface() { using (var f = new TempfileLife()) { var rpath = JsonSettings.ResolvePath(f); ISettings o = JsonSettings.Load <InterfacedSettings>(f.FileName).EnableIAutosave <InterfacedSettings, ISettings>(); Console.WriteLine(File.ReadAllText(rpath)); o.property.ShouldBeEquivalentTo(null); o.property = "test"; var o2 = JsonSettings.Load <InterfacedSettings>(f.FileName); o2.property.ShouldBeEquivalentTo("test"); var jsn = File.ReadAllText(rpath); jsn.Contains("\"test\"").Should().BeTrue(); Console.WriteLine(jsn); } }
public void CreateNonExistingSettings() { var path = JsonSettings.ResolvePath(new Settings(), "swag.json", true); using (var f = new TempfileLife(path)) { if (File.Exists(f)) { File.Delete(f); } File.Exists(f).Should().BeFalse(); var o = JsonSettings.Load <Settings>(f); o.Str = "lol"; File.Exists(f).Should().BeTrue(); o.Save(); File.Exists(f).Should().BeTrue(); } }
public void Saving() { using (var f = new TempfileLife()) { var rpath = JsonSettings.ResolvePath(f); bool saved = false; var o = JsonSettings.Load <Settings>(f.FileName).EnableAutosave(); o.AfterSave += (s, destinition) => { saved = true; }; o.property.ShouldBeEquivalentTo(null); Console.WriteLine(File.ReadAllText(rpath)); o.property = "test"; saved.ShouldBeEquivalentTo(true); var o2 = JsonSettings.Load <Settings>(f.FileName).EnableAutosave(); o2.property.ShouldBeEquivalentTo("test"); var jsn = File.ReadAllText(rpath); jsn.Contains("\"test\"").Should().BeTrue(); Console.WriteLine(jsn); } }
public void OverrideExistingBySmallerSettingsFile() { var path = JsonSettings.ResolvePath(new SettingsLarger(), "swaggy.json", true); using (var f = new TempfileLife(path)) { if (File.Exists(f)) { File.Delete(f); } File.Exists(f).Should().BeFalse(); var o = JsonSettings.Load <SettingsLarger>(f); o.Str = o.Str2 = o.Str3 = "lol"; o.Save(); File.Exists(f).Should().BeTrue(); var o2 = JsonSettings.Load <Settings>(f); o2.Str.ShouldBeEquivalentTo("lol"); } }
internal static T _withFileName <T>(this T _instance, string filename, bool throwless = false) where T : JsonSettings { _instance.FileName = JsonSettings.ResolvePath(_instance, filename, throwless); return(_instance); }