Skip to content

Commit

Permalink
Merge pull request #333 from mexx/display-attribute
Browse files Browse the repository at this point in the history
handle Display attribute specially
  • Loading branch information
MehdiK committed Sep 10, 2014
2 parents b6b03bc + ce49414 commit 66d9efc
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 1 deletion.
16 changes: 16 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,22 @@ You can even configure the name of the property of attibute to use as descriptio

`Configurator.EnumDescriptionPropertyLocator = p => p.Name == "Info"`

If you need to provide localised descriptions you can use `DisplayAttribute` data annotation instead.

```C#
public enum EnumUnderTest
{
[Display(Description = "EnumUnderTest_Member", ResourceType = typeof(Project.Resources))]
Member
}
```

You will get:

```C#
EnumUnderTest.Member.Humanize() => "content" // from Project.Resources found under "EnumUnderTest_Member" resource key
```

Hopefully this will help avoid littering enums with unnecessary attributes!

###<a id="dehumanize-enums">Dehumanize Enums</a>
Expand Down
1 change: 1 addition & 0 deletions release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
- [#320](https://github.com/MehdiK/Humanizer/pull/320): Fixed Dehumanize actually humanizing an already dehumanized string
- [#322](https://github.com/MehdiK/Humanizer/pull/322): DefaultFormatter.TimeSpanHumanize throws a more meaningful exception when called with TimeUnits larger than TimeUnit.Week
- [#314](https://github.com/MehdiK/Humanizer/pull/314): Added ByteRate class and supporting members to facilitate calculation of byte transfer rates
- [#333](https://github.com/MehdiK/Humanizer/pull/333): Added support to humanize enums from resource strings

[Commits](https://github.com/MehdiK/Humanizer/compare/v1.28.0...master)

Expand Down
14 changes: 14 additions & 0 deletions src/Humanizer.Tests/DehumanizeToEnumTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ public void AllCapitalMembersAreReturnedAsIs()
Assert.Equal(EnumUnderTest.ALLCAPITALS, EnumUnderTest.ALLCAPITALS.ToString().DehumanizeTo(typeof(EnumUnderTest)));
}

[Fact]
public void HonorsDisplayAttribute()
{
Assert.Equal(EnumUnderTest.MemberWithDisplayAttribute, EnumTestsResources.MemberWithDisplayAttribute.DehumanizeTo<EnumUnderTest>());
Assert.Equal(EnumUnderTest.MemberWithDisplayAttribute, EnumTestsResources.MemberWithDisplayAttribute.DehumanizeTo(typeof(EnumUnderTest)));
}

[Fact]
public void HonorsLocalizedDisplayAttribute()
{
Assert.Equal(EnumUnderTest.MemberWithLocalizedDisplayAttribute, EnumTestsResources.MemberWithLocalizedDisplayAttribute.DehumanizeTo<EnumUnderTest>());
Assert.Equal(EnumUnderTest.MemberWithLocalizedDisplayAttribute, EnumTestsResources.MemberWithLocalizedDisplayAttribute.DehumanizeTo(typeof(EnumUnderTest)));
}

struct DummyStructWithEnumInterfaces : IComparable, IFormattable, IConvertible
{
public int CompareTo(object obj)
Expand Down
12 changes: 12 additions & 0 deletions src/Humanizer.Tests/EnumHumanizeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,17 @@ public void AllCapitalMembersAreReturnedAsIs()
{
Assert.Equal(EnumUnderTest.ALLCAPITALS.ToString(), EnumUnderTest.ALLCAPITALS.Humanize());
}

[Fact]
public void HonorsDisplayAttribute()
{
Assert.Equal(EnumTestsResources.MemberWithDisplayAttribute, EnumUnderTest.MemberWithDisplayAttribute.Humanize());
}

[Fact]
public void HonorsLocalizedDisplayAttribute()
{
Assert.Equal(EnumTestsResources.MemberWithLocalizedDisplayAttribute, EnumUnderTest.MemberWithLocalizedDisplayAttribute.Humanize());
}
}
}
9 changes: 8 additions & 1 deletion src/Humanizer.Tests/EnumUnderTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace Humanizer.Tests
{
Expand All @@ -16,7 +17,11 @@ public enum EnumUnderTest
[CustomProperty(EnumTestsResources.MemberWithCustomPropertyAttribute)]
MemberWithCustomPropertyAttribute,
MemberWithoutDescriptionAttribute,
ALLCAPITALS
ALLCAPITALS,
[Display(Description = EnumTestsResources.MemberWithDisplayAttribute)]
MemberWithDisplayAttribute,
[Display(Description = "MemberWithLocalizedDisplayAttribute", ResourceType = typeof(EnumTestsResources))]
MemberWithLocalizedDisplayAttribute
}

public class EnumTestsResources
Expand All @@ -29,6 +34,8 @@ public class EnumTestsResources
public const string MemberWithoutDescriptionAttributeSentence = "Member without description attribute";
public const string MemberWithoutDescriptionAttributeTitle = "Member Without Description Attribute";
public const string MemberWithoutDescriptionAttributeLowerCase = "member without description attribute";
public const string MemberWithDisplayAttribute = "Description from Display attribute";
public static string MemberWithLocalizedDisplayAttribute { get { return "Localized description from Display attribute"; } }
}

public class ImposterDescriptionAttribute : Attribute
Expand Down
1 change: 1 addition & 0 deletions src/Humanizer.Tests/Humanizer.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<HintPath>..\packages\ApprovalUtilities.3.0.5\lib\net35\ApprovalUtilities.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
Expand Down
9 changes: 9 additions & 0 deletions src/Humanizer/EnumHumanizeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ namespace Humanizer
/// </summary>
public static class EnumHumanizeExtensions
{
private const string DisplayAttributeTypeName = "System.ComponentModel.DataAnnotations.DisplayAttribute";
private const string DisplayAttributeGetDescriptionMethodName = "GetDescription";

private static readonly Func<PropertyInfo, bool> StringTypedProperty = p => p.PropertyType == typeof(string);

/// <summary>
Expand Down Expand Up @@ -42,6 +45,12 @@ private static string GetCustomDescription(MemberInfo memberInfo)
foreach (var attr in attrs)
{
var attrType = attr.GetType();
if (attrType.FullName == DisplayAttributeTypeName)
{
var method = attrType.GetMethod(DisplayAttributeGetDescriptionMethodName);
if (method != null)
return method.Invoke(attr, new object[0]).ToString();
}
var descriptionProperty =
attrType.GetProperties()
.Where(StringTypedProperty)
Expand Down

0 comments on commit 66d9efc

Please sign in to comment.