Last active
October 10, 2019 15:20
-
-
Save exlted/e30e53b0a3022af8eddacead71812d8d to your computer and use it in GitHub Desktop.
Simple-Settings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SettingsObj | |
{ | |
[String] $location | |
$settings | |
SettingsObj ([String]$location) | |
{ | |
$this.location = $location | |
$this.updateSettings(); | |
} | |
updateSettings() { | |
#If there are already settings in the specified location, load them | |
if([System.IO.File]::Exists($this.location)){ | |
$this.settings = Import-Clixml $this.location | |
} else { | |
#Otherwise, create a new, empty, settings object | |
$this.settings = New-Object 'System.Collections.Generic.Dictionary[String,String]' | |
} | |
} | |
[String] getSetting([String]$setting) { | |
$this.updateSettings(); | |
return $this.settings[$setting] | |
} | |
[bool] hasSetting([String]$setting) { | |
$this.updateSettings(); | |
return $this.settings.ContainsKey($setting) | |
} | |
setSetting([String]$setting, [String]$newVal) { | |
$this.updateSettings(); | |
if($this.settings.ContainsKey($setting)){ | |
this.settings[$setting] = $newVal; | |
} else { | |
$this.settings.Add($setting, $newVal) | |
} | |
#Whenever settings are added/changed, export them so they are on-disk | |
Export-Clixml -InputObject $this.settings -Path $this.location | |
} | |
removeSetting([String]$setting) { | |
$this.updateSettings(); | |
if($this.settings.ContainsKey($setting)){ | |
$this.settings.Remove($setting) | |
#Whenever settings are removed, export them so they are on-disk | |
Export-Clixml -InputObject $this.settings -Path $this.location | |
} | |
} | |
printSettings() { | |
Write-Host $($this.settings | Out-String) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment