Skip to content

Instantly share code, notes, and snippets.

@exospheredata
Created January 14, 2017 20:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save exospheredata/1b1080463e517e8e13bc857ce86defed to your computer and use it in GitHub Desktop.
Save exospheredata/1b1080463e517e8e13bc857ce86defed to your computer and use it in GitHub Desktop.
Simple PowerShell v5 Class module to managing a ToyBox
#
# Example PowerShell v5 class
#
# Jeremy Goodrum - ExosphereData, LLC 2017
#
enum EnumState
{
Missing
Stored
Playing
}
class ToyBox
{
# Name of the Toy or Item to be stored
[string] $Name
# Current State or Location of the Toy
[EnumState] $State
# This is currently a Favorited item or toy
[boolean] $Favorite
hidden [System.Object[]] $stored_Items
[ToyBox[]] Get()
{
return $this.GetMyItems()
}
[ToyBox] Get([string] $Name)
{
$___Results = $this.GetMyItems()
$___thisResult = $___Results | ?{$_.Name -eq $Name}
if (-not [string]::IsNullOrEmpty($___thisResult))
{
$this.Name = $___thisResult.Name
$this.State = $___thisResult.State
$this.Favorite = $___thisResult.Favorite
} else {
Write-Warning ("The toy [{0}] was not found." -f $Name)
}
return $this
}
[void] Save()
{
$this.SaveMyItems()
}
[void] Remove()
{
$this.RemoveThisToy($this.Name)
}
[void] Remove([string]$___Name)
{
$this.RemoveThisToy($___Name)
}
[void] Save([string]$___Name)
{
$this.Name = $___Name
$this.State = "Missing"
$this.Favorite = $false
$this.SaveMyItems()
}
[void] Save([string]$___Name,[EnumState]$___State)
{
$this.Name = $___Name
$this.State = $___State
$this.Favorite = $false
$this.SaveMyItems()
}
[void] Save([string]$___Name,[EnumState]$___State,[boolean]$___Favorite)
{
$this.Name = $___Name
$this.State = $___State
$this.Favorite = $___Favorite
$this.SaveMyItems()
}
[ToyBox] Play()
{
$this.State = "Playing"
return $this
}
[ToyBox] Store()
{
$this.State = "Stored"
return $this
}
[ToyBox] Lost()
{
$this.State = "Missing"
return $this
}
[ToyBox] Like()
{
$this.Favorite = $true
return $this
}
[ToyBox] Hate()
{
$this.Favorite = $false
return $this
}
[void] Export ()
{
$this.stored_Items | ConvertTo-Json | Out-File -FilePath .\my_toys.txt
}
[void] Import ()
{
$___saveFile = (Get-Content -Path .\my_toys.txt)
$this.stored_Items = ( $___saveFile | ConvertFrom-Json )
}
hidden [string[]] SaveMyItems ()
{
$___storedItems = $this.GetMyItems()
$__existingItem = $___storedItems | ?{$_.Name -eq $this.Name}
if (-not [string]::IsNullOrEmpty($__existingItem) ){
$__existingItem.Name = $this.Name
$__existingItem.State = $this.State
$__existingItem.Favorite = $this.Favorite
$__currentList = ($___storedItems | ?{$_.Name -ne $this.Name})
$__currentList += $__existingItem
$this.stored_Items = ($__currentList | ConvertTo-Json)
}
else{
$__newItem = @{
Name = $this.Name
State = $this.State
Favorite = $this.Favorite
}
$___storedItems += $__newItem
$this.stored_Items = ($___storedItems | ConvertTo-Json)
}
return $this.stored_Items
}
hidden [void] RemoveThisToy ([string] $___Name)
{
$___storedItems = $this.GetMyItems()
$__existingItem = $___storedItems | ?{$_.Name -eq $___Name}
if (-not [string]::IsNullOrEmpty($__existingItem) ){
$__currentList = ($___storedItems | ?{$_.Name -ne $this.Name})
$this.stored_Items = ($__currentList | ConvertTo-Json)
}
}
hidden [ToyBox[]] GetMyItems()
{
if (-not [string]::IsNullOrEmpty($this.stored_Items) ){
return ($this.stored_Items | ConvertFrom-Json)
} else {
return $null
}
}
static [ToyBox[]] op_Addition([ToyBox] $Left, [ToyBox] $Right)
{
$__current = @()
$__current += ($Left | ConvertTo-Json)
$__current += ($Right | ConvertTo-Json)
return ($__current | ConvertFrom-Json)
}
}
function New-ToyBox{
param(
[string] $Name,
[EnumState] $State,
[boolean] $Favorite
)
$__toyBox = [ToyBox]::new()
if($Name) {$__toyBox.Name = $Name}
if($State) {$__toyBox.State = $State}
if($Favorite) {$__toyBox.Favorite = $Favorite}
return $__toyBox
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment