Skip to content

Commit

Permalink
Add extension methods option (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
lfmundim authored Oct 30, 2021
1 parent aae223b commit edaf826
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 6 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ Then go ahead and consume it.
var zodiacSign = Zodiac.GetZodiacSignForDate(new DateTime(1950, 2, 12));
```

Another simpler option if all you need is the zodiac sign for a single date is:
```C#
var givenDateTime = new DateTime(1995, 8, 26);
var zodiacSign = givenDateTime.GetZodiacSign();
```

#### More examples
[Horoscope.Zodiac](https://github.com/ClydeDz/horoscope-nuget/blob/master/Src/Horoscope.TestConsole/ZodiacExamples.cs)
[Horoscope.ChineseZodiac](https://github.com/ClydeDz/horoscope-nuget/blob/master/Src/Horoscope.TestConsole/ChineseZodiacExamples.cs)
Expand Down
5 changes: 5 additions & 0 deletions Src/Horoscope.TestConsole/ChineseZodiacExamples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public static void ShowZodiacBasicExamples()
var zodiacSignForDate = ChineseZodiac.GetZodiacSignForDate(new DateTime(1966, 2, 12));
Console.WriteLine($"\nChinese zodiac sign for {new DateTime(1966, 2, 12).ToShortDateString()} is {zodiacSignForDate.ZodiacEnglishTranslation}");

// Another option would be:
var givenDate = new DateTime(1995, 8, 26);
var anotherZodiacSign = givenDate.GetChineseZodiacSign();
Console.WriteLine($"\nChinese zodiac sign for {givenDate.ToShortDateString()} is {anotherZodiacSign.ZodiacEnglishTranslation}");

var allChineseZodiacSigns = ChineseZodiac.GetAllZodiacSigns();
Console.WriteLine($"\nGet a list of all Chinese zodiac signs");
foreach (var currentZodiacSign in allChineseZodiacSigns)
Expand Down
6 changes: 6 additions & 0 deletions Src/Horoscope.TestConsole/ZodiacExamples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ public static void ShowZodiacBasicExamples()
Console.WriteLine($"\nZodiac details for {new DateTime(1966, 2, 12).ToShortDateString()}");
Console.WriteLine($"Name: {zodiacSign.ZodiacName} English name: {zodiacSign.ZodiacEnglishTranslation} Duration: {zodiacSign.ZodiacDuration}");

// Another option would be:
var givenDate = new DateTime(1995, 8, 26);
var anotherZodiacSign = givenDate.GetZodiacSign();
Console.WriteLine($"\nZodiac details for {givenDate.ToShortDateString()}");
Console.WriteLine($"Name: {anotherZodiacSign.ZodiacName} English name: {anotherZodiacSign.ZodiacEnglishTranslation} Duration: {anotherZodiacSign.ZodiacDuration}");

var capriconZodiacSign = Zodiac.GetZodiacSign(ZodiacSigns.Capricorn);
Console.WriteLine($"\nZodiac duration for {ZodiacSigns.Capricorn}");
Console.WriteLine(capriconZodiacSign.ZodiacDuration);
Expand Down
33 changes: 33 additions & 0 deletions Src/Horoscope.Tests/DateTimeExtensionsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using Horoscope.Extensions;
using Xunit;

namespace Horoscope.Tests
{
public class DateTimeExtensionsTest
{
[Theory]
[InlineData(2, 12, "Aquarius")]
[InlineData(3, 3, "Pisces")]
[InlineData(2, 28, "Pisces")]
[InlineData(10, 23, "Scorpio")]
public void GetZodiacSign_Test(int month, int day, string zodiacSign)
{
var dateTime = new DateTime(1950, month, day);
var symbol = dateTime.GetZodiacSign();
Assert.Equal(zodiacSign, symbol.ZodiacName);
}

[Theory]
[InlineData(2018, "Dog")]
[InlineData(1952, "Dragon")]
[InlineData(1969, "Rooster")]
[InlineData(1903, "Rabbit")]
public void GetChineseZodiacSign_Test(int year, string zodiacSign)
{
var dateTime = new DateTime(year, 4, 1);
var symbol = dateTime.GetChineseZodiacSign();
Assert.Equal(zodiacSign, symbol.ZodiacEnglishTranslation);
}
}
}
1 change: 1 addition & 0 deletions Src/Horoscope.Tests/Horoscope.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
<Compile Include="Helpers\ZodiacHelperTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ChineseZodiacTest.cs" />
<Compile Include="DateTimeExtensionsTest.cs" />
<Compile Include="ZodiacTest.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions Src/Horoscope.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
<metadata>
<id>Horoscope</id>
<version>1.0.0</version>
<version>1.1.0</version>
<title>Horoscope</title>
<authors>Clyde D'Souza</authors>
<owners>Clyde D'Souza</owners>
Expand All @@ -14,7 +14,7 @@
<iconUrl>https://raw.githubusercontent.com/ClydeDz/horoscope-nuget/master/Icon.png</iconUrl>
<description>A .NET library for zodiac signs. Get details on each zodiac sign, pass a date and know which zodiac sign it falls in or get a list of all zodiac signs. Now includes Chinese zodiac signs and elements!</description>
<summary>A .NET library for zodiac signs. Get details on each zodiac sign, pass a date and know which zodiac sign it falls in or get a list of all zodiac signs.</summary>
<releaseNotes>Enable publicy visible comments. Add contributing guidelines and a pull request template to the GitHub repository. Use new icon format. More release notes here: https://github.com/ClydeDz/horoscope-nuget/releases </releaseNotes>
<releaseNotes>Add extension methods option to DateTime objects to enable simpler, easier access to the Zodiac Signs.</releaseNotes>
<copyright>Copyright (c) 2018 Clyde D'Souza</copyright>
<tags>horoscope zodiac development library zodiac-signs chinese-zodiac chinese-horoscope yinyang chinese-elements yin-yang</tags>
<dependencies>
Expand Down
25 changes: 25 additions & 0 deletions Src/Horoscope/DateTimeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Horoscope.Model;
using System;

namespace Horoscope
{
/// <summary>
/// Enable usage of Zodiac and ChineseZodiac methods straight from DateTime instances
/// </summary>
public static class DateTimeExtensions
{
/// <summary>
/// Gets the zodiac sign.
/// </summary>
/// <param name="dateTime">The date for which you want the zodiac sign.</param>
/// <returns>A zodiac sign object.</returns>
public static ZodiacModel GetZodiacSign(this DateTime dateTime) => Zodiac.GetZodiacSignForDate(dateTime);

/// <summary>
/// Get the Chinese zodiac sign.
/// </summary>
/// <param name="dateTime">The date you want to query.</param>
/// <returns>Returns a Chinese zodiac sign object.</returns>
public static ChineseZodiacModel GetChineseZodiacSign(this DateTime dateTime) => ChineseZodiac.GetZodiacSignForDate(dateTime);
}
}
22 changes: 22 additions & 0 deletions Src/Horoscope/Extensions/DateTimeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Horoscope.Model;
using System;

namespace Horoscope.Extensions
{
public static class DateTimeExtensions
{
/// <summary>
/// Gets the zodiac sign for the date supplied.
/// </summary>
/// <param name="dateTime">The date for which you want the zodiac sign.</param>
/// <returns>A zodiac sign object.</returns>
public static ZodiacModel GetZodiacSign(this DateTime dateTime) => Zodiac.GetZodiacSignForDate(dateTime);

/// <summary>
/// Get the Chinese zodiac sign for the supplied date.
/// </summary>
/// <param name="dateTime">The date you want to query.</param>
/// <returns>Returns a Chinese zodiac sign object.</returns>
public static ChineseZodiacModel GetChineseZodiacSign(this DateTime dateTime) => ChineseZodiac.GetZodiacSignForDate(dateTime);
}
}
8 changes: 4 additions & 4 deletions Src/Horoscope/Horoscope.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
<Authors>Clyde D'Souza</Authors>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<PackageId>Horoscope</PackageId>
<Version>1.0.0</Version>
<Version>1.1.0</Version>
<Company />
<PackageLicenseUrl></PackageLicenseUrl>
<RepositoryUrl>https://github.com/ClydeDz/horoscope-nuget</RepositoryUrl>
<PackageProjectUrl>https://github.com/ClydeDz/horoscope-nuget</PackageProjectUrl>
<PackageTags>horoscope zodiac development library zodiac-signs chinese-zodiac chinese-horoscope yinyang chinese-elements yin-yang</PackageTags>
<Description>A .NET library for zodiac signs. Get details on each zodiac sign, pass a date and know which zodiac sign it falls in or get a list of all zodiac signs. Now includes Chinese zodiac signs and elements!</Description>
<Copyright>Copyright (c) 2018 Clyde D'Souza</Copyright>
<PackageReleaseNotes>Enable publicy visible comments. Add contributing guidelines and a pull request template to the GitHub repository. Use new icon format. More release notes here: https://github.com/ClydeDz/horoscope-nuget/releases</PackageReleaseNotes>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<FileVersion>1.0.0.0</FileVersion>
<releaseNotes>Add extension methods option to DateTime objects to enable simpler, easier access to the Zodiac Signs.</releaseNotes>
<AssemblyVersion>1.1.0.0</AssemblyVersion>
<FileVersion>1.1.0.0</FileVersion>
<PackageIconUrl></PackageIconUrl>
<Product>Horoscope</Product>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand Down

0 comments on commit edaf826

Please sign in to comment.