Skip to content

Commit

Permalink
new class with calculated properties example
Browse files Browse the repository at this point in the history
  • Loading branch information
ninmonkey committed Dec 29, 2023
1 parent d7ccc7d commit 358dc3d
Showing 1 changed file with 131 additions and 0 deletions.
131 changes: 131 additions & 0 deletions Pwsh/Types/StringCrumble-with-ScriptProperties.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
function String.GetCrumbs {
<#
.SYNOPSIS
Split a string into chunks.'
.EXAMPLE
GetStringCrumbs -InputText 'bat man 3.14 cat, n!-choose-r' -SplitBy '[ ]'
.EXAMPLE
[WordCrumb]::new( 'foo bar 3.14 cat!bat; bat cat-dog', '\W+' )
.EXAMPLE
$w1 = [WordCrumb]::new( 'foo bar 3.14 cat!bat; bat cat-dog')
$w1.CrumbCount | Should -be 9
$w1.String = 'foo bar! cat'
$w1.CrumbCount | Should -be 3
$w1.String = 'foo bar'
$w1.CrumbCount | Should -be 2
#>
[Alias(
'GetStringCrumbs'
)]
param(
[string]$InputText,

[ArgumentCompletions(
'\W+',
'\b',
'\s+',
'\s',
"'[ ]'",
"'[ ]+'"

)]
[string]$SplitBy
)
[WordCrumb]::new( $InputText, $SplitBy )
}
class WordCrumb {
<#
.DESCRIPTION
Example of a pwsh class with getters/setters that mutate the state
modify properties 'c.String' or 'c.SplitBy'
#>
[string[]]
$Crumbs = @()

[int]
$CrumbCount = 0

hidden [string]
$RawString

[string]
$_SplitBy = '\W+'

WordCrumb ( [string]$Text ) {
$This.RawString = $Text
$This.Crumbs = $Text -split $this._SplitBy
$this.CrumbCount = $This.Crumbs.Count
}
WordCrumb ( [string]$Text, [string]$SplitBy ) {
$This.RawString = $Text
$this.Crumbs = $Text -split $SplitBy
$this._SplitBy = $SplitBy
$this.CrumbCount = $This.Crumbs.Count
}
# rebuild
[void] Update () { # aka Recalculate()
# $this = [WordCrumb]::Parse( $This.RawString, $this._SplitBy )

$new = [WordCrumb]::Parse( $This.RawString, $this._SplitBy )
$this.Crumbs = $New.Crumbs
$this.CrumbCount = $new.CrumbCount
$this.RawString = $new.RawString
$this._SplitBy = $new.SplitBy
}
static [WordCrumb] Parse( [string]$Text, [string]$SplitBy ) {
return [WordCrumb]::New( $Text, $SplitBy )
}
}
$get_RawString = {
return $this.RawString
}
$set_RawString = {
param( [string]$NewText )
$this.RawString = $NewText
$this.Update()
}

$get_SplitBy = {
return $this._SplitBy
}
$set_SplitBy = {
param( [string]$SplitBy )
$this._SplitBy = $SplitBy
$this.Update()
}

$add_ScriptProperty = @{
MemberType = 'ScriptProperty'
Force = $true
TypeName = 'WordCrumb'
# TypeConverter = '.'
# TypeAdapter = '.'
# TypeData = ''
}

$updateTypeDataSplat = @{
MemberName = 'String'
Value = $get_RawString
SecondValue = $set_RawString
}
Update-TypeData @updateTypeDataSplat @add_ScriptProperty

$updateTypeDataSplat = @{
MemberName = 'SplitBy'
Value = $get_SplitBy
SecondValue = $set_SplitBy
}
Update-TypeData @updateTypeDataSplat @add_ScriptProperty

[WordCrumb]::new( 'foo bar 3.14 cat!bat; bat cat-dog', '\W+' )
[WordCrumb]::new( 'foo bar 3.14 cat!bat; bat cat-dog')

$w1 = [WordCrumb]::new( 'foo bar 3.14 cat!bat; bat cat-dog')
$w1.CrumbCount | Should -be 9
$w1.String = 'foo bar! cat'
$w1.CrumbCount | Should -be 3
$w1.String = 'foo bar'
$w1.CrumbCount | Should -be 2

$w1.SplitBy = 'oo'
$w1.Crumbs | Should -BeExactly @('f', ' bar')

0 comments on commit 358dc3d

Please sign in to comment.