-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathPersistedOptions.cs
56 lines (47 loc) · 1.56 KB
/
PersistedOptions.cs
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
namespace PowerControl
{
public class PersistedOptions : CommonHelpers.BaseSettings
{
public const string OptionsKey = "Options";
public PersistedOptions(string name) : base(name + "Options")
{
}
public struct Option
{
public PersistedOptions Options { get; set; }
public string Key { get; set; }
public string FullKey(string setting)
{
return String.Format("{0}_{1}", Key, setting);
}
public bool Exist
{
get => Options.GetOptions().Contains(Key);
}
public Option Set<T>(string setting, T value)
{
// Get and persist value on first access
Options.Get(FullKey(setting), value, true);
return this;
}
public T Get<T>(string setting, T defaultValue)
{
// Get and do not persist value
return Options.Get(FullKey(setting), defaultValue, false);
}
}
public Option ForOption(string option)
{
return new Option() { Options = this, Key = option };
}
public void SetOptions(IEnumerable<Option> options)
{
Set<string>(OptionsKey, String.Join(",", options.Select((option) => option.Key)));
}
public string[] GetOptions()
{
var options = Get<string>(OptionsKey, "");
return options.Split(',', StringSplitOptions.RemoveEmptyEntries);
}
}
}