Skip to content

Commit

Permalink
1.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jdhitsolutions committed Dec 29, 2023
1 parent bfa0af1 commit e270a6b
Show file tree
Hide file tree
Showing 13 changed files with 346 additions and 217 deletions.
Binary file modified MyNumber.psd1
Binary file not shown.
121 changes: 18 additions & 103 deletions MyNumber.psm1
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
#requires -version 5.0

#region Main

#define class

Class MyNumber {

#properties
[double]$Number
[double]$Square
[double]$Cube
Expand All @@ -21,14 +18,12 @@ Class MyNumber {
[double]$Exp
[double]$Factorial
[double[]]$Factors
[psobject]$Custom
[PSObject]$Custom
#store the custom scriptblock in a hidden property which can be managed through a module function
hidden [scriptblock]$CustomScriptBlock

#methods

[mynumber] Refresh() {

[MyNumber] Refresh() {
$this.Square = ($this.number * $this.number)
$this.Cube = [math]::Pow($this.number, 3)
$this.Sqrt = [math]::Sqrt($this.number)
Expand All @@ -39,18 +34,18 @@ Class MyNumber {
$this.CircleArea = [math]::PI * ($this.number * $this.number)
$this.Inverse = 1 / $this.number
$this.Exp = [math]::Exp($this.number)
$this.Factorial = (1..$this.number | foreach-object -begin {$r = 1} -process {$r *= $_} -end {$r})
$this.Factors = (1..$($this.number) | where-object {-Not ($this.number % $_)})
$this.Factorial = (1..$this.number | ForEach-Object -Begin { $r = 1 } -Process { $r *= $_ } -End { $r })
$this.Factors = (1..$($this.number) | Where-Object { -Not ($this.number % $_) })
$this.IsEven = $this.TestIsEven()
$this.IsPrime = $this.TestIsPrime()
if ($this.CustomScriptBlock) {
$customresult = Invoke-Command -ScriptBlock $this.CustomScriptBlock -ArgumentList $this.Number
$CustomResult = Invoke-Command -ScriptBlock $this.CustomScriptBlock -ArgumentList $this.Number
}
else {
$customresult = 0
$CustomResult = 0
}
$this.Custom = $customresult

$this.Custom = $CustomResult
#class methods require the Return keyword
Return $this
}

Expand All @@ -71,17 +66,17 @@ Class MyNumber {
Return $False
}
}
[string]ToBinary() {
[String]ToBinary() {
$r = [convert]::ToString($this.Number, 2)
Return $r
}

[string]ToOctal() {
[String]ToOctal() {
$r = [convert]::ToString($this.Number, 8)
Return $r
}

[string]ToHex() {
[String]ToHex() {
$r = [convert]::ToString($this.Number, 16)
Return $r
}
Expand All @@ -93,94 +88,14 @@ Class MyNumber {
}

}
#endregion

Function New-MyNumber {
[CmdletBinding()]
[OutputType([MyNumber])]

Param(
[Parameter(Position = 0, Mandatory, HelpMessage = "Enter a numeric value.", ValueFromPipeline)]
[double[]]$Number,
[scriptblock]$CustomScriptBlock
)
Process {
Foreach ($n in $Number) {
Write-Verbose "Creating a myNumber object for $n"
$obj = New-Object -TypeName MyNumber -ArgumentList $n

if ($CustomScriptBlock) {
$obj.CustomScriptBlock = $CustomScriptBlock
$obj.Refresh() | out-Null
}
$obj
}
}
}

Function Convert-MyNumber {
[CmdletBinding(defaultparametersetname = 'binary')]
[OutputType([System.String])]

Param(
[Parameter(Position = 0, ValueFromPipelineByPropertyName, ValueFromPipeline, Mandatory)]
[Double]$Number,
[Parameter(ParameterSetName = "binary")]
[Switch]$ToBinary,
[Parameter(ParameterSetName = "hex")]
[Switch]$ToHex,
[Parameter(ParameterSetName = "octal")]
[Switch]$ToOctal
)

Begin { }
Process {
Write-Verbose "Processing $number"
Switch ($PSCmdlet.ParameterSetName) {
"binary" {
[convert]::ToString($Number, 2)
}
"hex" {
[convert]::ToString($Number, 16)
}
"octal" {
[convert]::ToString($Number, 8)
}
}
}
End {}
}
#extend the class with custom type extensions

Function Set-MyNumber {
[cmdletbinding(SupportsShouldProcess, DefaultParameterSetName = "value")]
[OutputType([MyNumber])]
Update-TypeData -TypeName MyNumber -MemberType ScriptProperty -MemberName Computername -Value {[System.Environment]::MachineName} -Force

Param(
[Parameter(Position = 0, ValueFromPipeline, Mandatory)]
[MyNumber]$Number,
[Parameter(Position = 1, Mandatory, ParameterSetName = "Value")]
[double]$Value,
[Parameter(ParameterSetName = "script")]
[scriptblock]$CustomScriptBlock
)
Begin {}
Process {
Update-TypeData -TypeName MyNumber -MemberType AliasProperty -MemberName Value -Value Number -Force

switch ($pscmdlet.ParameterSetName) {
"value" {
if ($pscmdlet.ShouldProcess("MyNumber", "Set number to $value")) {
$number.number = $Value
$number.refresh()
}
} #value
"script" {
if ($pscmdlet.ShouldProcess("MyNumber", "Set custom script to $CustomScriptblock")) {
$number.CustomScriptBlock = $CustomScriptBlock
$number.refresh()
}
}
}
}
End {}
}
#endregion

#dot source module functions
Get-ChildItem -Path $PSScriptRoot\functions\*.ps1 | ForEach-Object -Process { . $_.FullName }
35 changes: 20 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
# MyNumber

A demonstration class-based module that creates a *number* object. The functionality of the module isn't that practical. But it can serve as a model on how you might build a class-based PowerShell tool.
:mortar_board: A demonstration class-based module that creates a *number* object. The functionality of the module isn't that practical. But it can serve as a model of how you might build a class-based PowerShell tool.

The module should work on Windows PowerShell and PowerShell Core cross-platform. It is intended for educational purposes.
The module should work on Windows PowerShell and PowerShell 7, including cross-platform. It is intended for educational purposes.

Install the latest version from the PowerShell Gallery.

```powershell
Install-Module MyNumber
```

In PowerShell Core you'll most likely need to include `-scope currentuser`.
You can also use the `Microsoft.PowerShell.PSResourceGet` module.

```powershell
Install-PSResource MyNumber -scope AllUsers
```

The class is defined in [MyNumber.psm1](mynumber.psm1). Functionality is exposed through a set of functions.

+ [New-MyNumber](./docs/New-MyNumber.md)
+ [Set-MyNumber](./docs/Set-MyNumber.md)
+ [Convert-MyNumber](./docs/Convert-MyNumber.md)
+ [New-MyNumber](/docs/New-MyNumber.md)
+ [Set-MyNumber](/docs/Set-MyNumber.md)
+ [Convert-MyNumber](/docs/Convert-MyNumber.md)

The module also includes custom format and type extensions. The examples shown below may not reflect these changes.


```powershell
PS C:\> $x = new-mynumber 11
PS C:\> $x = New-MyNumber 11
PS C:\> $x
Expand All @@ -40,7 +47,7 @@ Factorial : 39916800
Factors : {1, 11}
Custom : 0
PS C:\> $x | Set-mynumber -value 123
PS C:\> $x | Set-MyNumber -value 123
Number : 123
Expand All @@ -60,17 +67,17 @@ Factorial : 1.21463043670253E+205
Factors : {1, 3, 41, 123}
Custom : 0
PS C:\> $x | convert-mynumber -tohex
PS C:\> $x | Convert-MyNumber -ToHex
7b
PS C:\> convert-mynumber 1024 -ToBinary
PS C:\> Convert-MyNumber 1024 -ToBinary
10000000000
```

You can also create a number with a custom scriptblock.
You can also create a number with a custom script block.

```powershell
PS C:\> new-mynumber 77 -CustomScriptBlock {Param($x) [char][int]$x }
PS C:\> New-MyNumber 77 -CustomScriptBlock {Param($x) [char][int]$x }
Number : 77
Expand All @@ -91,8 +98,6 @@ Factors : {1, 7, 11, 77}
Custom : M
```

You should include a parameter in your scriptblock for the number value. It will be passed as an argument when calculating the value.
You should include a parameter in your script block for the number value. It will be passed as an argument when calculating the value.

You can only access the class definition through the functions.

_Last updated 23 October 2018_
53 changes: 30 additions & 23 deletions Tests/MyNumber.tests.ps1
Original file line number Diff line number Diff line change
@@ -1,37 +1,44 @@

#this is an incomplete Pester test

Import-Module $PSScriptRoot\..\MyNumber.psd1 -Force

Describe New-MyNumber {
$Test = @(
@{Property = 'Cube'; Type = 'double' }
@{Property = 'Sine'; Type = 'double' }
@{Property = 'Sqrt'; Type = 'double' }
@{Property = 'Inverse'; Type = 'double' }
@{Property = 'IsPrime'; Type = 'bool' }
@{Property = 'CircleArea'; Type = 'double' }
@{Property = 'Cosine'; Type = 'double' }
@{Property = 'Square'; Type = 'double' }
@{Property = 'Log'; Type = 'double' }
@{Property = 'Tangent'; Type = 'double' }
@{Property = 'IsEven'; Type = 'bool' }
@{Property = 'Number'; Type = 'double' }
@{Property = 'Factors'; Type = 'double[]' }
@{Property = 'Factorial'; Type = 'double' }
@{Property = 'Exp'; Type = 'double' }
)

BeforeAll {
$n = New-MyNumber 1
}

It 'Should run without error' {
{ New-MyNumber 1 } | Should -Not Throw
{ New-MyNumber -Number 1 } | Should -Not -Throw
}
It 'Should create a MyNumber object type' {
$n = New-MyNumber 1
$n.GetType().Name | Should -Be 'MyNumber'
}

$PropHash = @{
Log = [double]
Number = [double]
Sqrt = [double]
IsEven = [bool]
IsPrime = [bool]
Cosine = [double]
Factorial = [double]
Exp = [double]
Inverse = [double]
Sine = [double]
Square = [double]
CircleArea = [double]
Tangent = [double]
Factors = [double[]]
Cube = [double]
}
$PropHash.GetEnumerator() | ForEach-Object -Begin { $n = New-MyNumber 2 } -Process {
It "Should have a property called $($_.key) of type $($_.value)" {
$n.$($_.key) -is $_.value | Should -Be $True
}

It 'Should have a property called <Property> with a type of <Type>' -ForEach $Test {

$n.PSObject.Properties[$Property].TypeNameOfValue | Should -Be ($Type -As [Type]).FullName
}

It 'Should create a new object with pipeline input' {
(1..5 | New-MyNumber).count | Should -Be 5
}
Expand Down
6 changes: 3 additions & 3 deletions Docs/Convert-MyNumber.md → docs/Convert-MyNumber.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Convert a numeric value to the specified format. The result will be a string.
### Example 1

```powershell
PS C:\> convert-mynumber 123
PS C:\> Convert-MyNumber 123
1111011
```

Expand All @@ -49,14 +49,14 @@ The default behavior is to convert to binary.
### Example 2

```powershell
PS C:\> 1024678 | convert-mynumber -ToHex
PS C:\> 1024678 | Convert-MyNumber -ToHex
fa2a6
```

### Example 3

```powershell
PS C:\> 1024678 | convert-mynumber -ToOctal
PS C:\> 1024678 | Convert-MyNumber -ToOctal
3721246
```

Expand Down
6 changes: 3 additions & 3 deletions Docs/New-MyNumber.md → docs/New-MyNumber.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Number Square Cube
### Example 3

```powershell
PS C:\> new-mynumber 77 -CustomScriptBlock {Param($x) [char][int]$x }
PS C:\> New-MyNumber 77 -CustomScriptBlock {Param($x) [char][int]$x }
Number : 77
Expand All @@ -86,7 +86,7 @@ Factors : {1, 7, 11, 77}
Custom : M
```

Here is an example using a custom scriptblock. You should include a parameter in your scriptblock for the number value.
Here is an example using a custom script block. You should include a parameter in your script block for the number value.

## PARAMETERS

Expand All @@ -108,7 +108,7 @@ Accept wildcard characters: False

### -CustomScriptBlock

You can specify a custom scriptblock that can be used to calculate a custom value. You should use a scriptblock with a parameter indicating the current number. When the scriptblock is executed the number value is passed as an argument. See examples.
You can specify a custom script block that can be used to calculate a custom value. You should use a script block with a parameter indicating the current number. When the scrip tblock is executed the number value is passed as an argument. See examples.

```yaml
Type: ScriptBlock
Expand Down
Loading

0 comments on commit e270a6b

Please sign in to comment.