Skip to content

Commit

Permalink
Serialize everything with kebab case by default
Browse files Browse the repository at this point in the history
fixes #131
  • Loading branch information
joukevandermaas committed Oct 23, 2016
1 parent 52bb6b0 commit a2990dc
Show file tree
Hide file tree
Showing 6 changed files with 762 additions and 738 deletions.
178 changes: 89 additions & 89 deletions Saule/ApiResource.cs
Original file line number Diff line number Diff line change
@@ -1,71 +1,71 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using Humanizer;

namespace Saule
{
/// <summary>
/// Represents a resource that can be consumed by clients.
/// </summary>
public abstract class ApiResource
{
private static readonly ConcurrentDictionary<Type, ApiResource> Resources =
new ConcurrentDictionary<Type, ApiResource>();

private readonly List<ResourceAttribute> _attributes = new List<ResourceAttribute>();
private readonly List<ResourceRelationship> _relationships = new List<ResourceRelationship>();

/// <summary>
/// Initializes a new instance of the <see cref="ApiResource"/> class.
/// </summary>
protected ApiResource()
{
var type = GetType();

var name = type.Name;
OfType(name.ToUpperInvariant().EndsWith("RESOURCE")
? name.Remove(name.Length - "RESOURCE".Length)
: name);

WithId("Id");

Resources.TryAdd(type, this);
}

/// <summary>
/// The url path of this resource.
/// </summary>
public string UrlPath { get; private set; }

/// <summary>
/// The type name of this resource.
/// </summary>
public string ResourceType { get; private set; }

internal IEnumerable<ResourceAttribute> Attributes => _attributes;

internal IEnumerable<ResourceRelationship> Relationships => _relationships;

internal string IdProperty { get; private set; }

/// <summary>
/// Customize the type name of this resource. The default value
/// is the name of the class (without 'Resource', if it exists).
/// </summary>
/// <param name="value">The type of the resource.</param>
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using Humanizer;

namespace Saule
{
/// <summary>
/// Represents a resource that can be consumed by clients.
/// </summary>
public abstract class ApiResource
{
private static readonly ConcurrentDictionary<Type, ApiResource> Resources =
new ConcurrentDictionary<Type, ApiResource>();

private readonly List<ResourceAttribute> _attributes = new List<ResourceAttribute>();
private readonly List<ResourceRelationship> _relationships = new List<ResourceRelationship>();

/// <summary>
/// Initializes a new instance of the <see cref="ApiResource"/> class.
/// </summary>
protected ApiResource()
{
var type = GetType();

var name = type.Name;
OfType(name.ToUpperInvariant().EndsWith("RESOURCE")
? name.Remove(name.Length - "RESOURCE".Length)
: name);

WithId("Id");

Resources.TryAdd(type, this);
}

/// <summary>
/// The url path of this resource.
/// </summary>
public string UrlPath { get; private set; }

/// <summary>
/// The type name of this resource.
/// </summary>
public string ResourceType { get; private set; }

internal IEnumerable<ResourceAttribute> Attributes => _attributes;

internal IEnumerable<ResourceRelationship> Relationships => _relationships;

internal string IdProperty { get; private set; }

/// <summary>
/// Customize the type name of this resource. The default value
/// is the name of the class (without 'Resource', if it exists).
/// </summary>
/// <param name="value">The type of the resource.</param>
protected void OfType(string value)
{
OfType(value, value.Pluralize(inputIsKnownToBeSingular: false));
}

/// <summary>
/// Customize the type name of this resource. The default value
/// is the name of the class (without 'Resource', if it exists).
/// </summary>
/// <param name="value">The type of the resource.</param>
/// <param name="path">The url pathspec of this relationship (default is the
/// pluralized version of the type name)</param>
/// <summary>
/// Customize the type name of this resource. The default value
/// is the name of the class (without 'Resource', if it exists).
/// </summary>
/// <param name="value">The type of the resource.</param>
/// <param name="path">The url pathspec of this relationship (default is the
/// pluralized version of the type name)</param>
protected void OfType(string value, string path)
{
ResourceType = value.ToDashed();
Expand All @@ -87,11 +87,11 @@ protected string WithId(string name)
return IdProperty;
}

/// <summary>
/// Specify an attribute of this resource.
/// </summary>
/// <param name="name">The name of the attribute.</param>
/// <returns>The <see cref="ResourceAttribute"/>.</returns>
/// <summary>
/// Specify an attribute of this resource.
/// </summary>
/// <param name="name">The name of the attribute.</param>
/// <returns>The <see cref="ResourceAttribute"/>.</returns>
protected ResourceAttribute Attribute(string name)
{
VerifyPropertyName(name);
Expand All @@ -103,10 +103,10 @@ protected ResourceAttribute Attribute(string name)
return result;
}

/// <summary>
/// Specify a to-one relationship of this resource.
/// </summary>
/// <param name="name">The name of the relationship.</param>
/// <summary>
/// Specify a to-one relationship of this resource.
/// </summary>
/// <param name="name">The name of the relationship.</param>
/// <typeparam name="T">The api resource type of the relationship.</typeparam>
/// <returns>The <see cref="ResourceRelationship"/>.</returns>
protected ResourceRelationship BelongsTo<T>(string name)
Expand All @@ -115,12 +115,12 @@ protected ResourceRelationship BelongsTo<T>(string name)
return BelongsTo<T>(name, name);
}

/// <summary>
/// Specify a to-one relationship of this resource.
/// </summary>
/// <param name="name">The name of the relationship.</param>
/// <param name="path">The url pathspec of this relationship (default
/// is the name)</param>
/// <summary>
/// Specify a to-one relationship of this resource.
/// </summary>
/// <param name="name">The name of the relationship.</param>
/// <param name="path">The url pathspec of this relationship (default
/// is the name)</param>
/// <typeparam name="T">The api resource type of the relationship.</typeparam>
/// <returns>The <see cref="ResourceRelationship"/>.</returns>
protected ResourceRelationship BelongsTo<T>(string name, string path)
Expand All @@ -136,10 +136,10 @@ protected ResourceRelationship BelongsTo<T>(string name, string path)
return result;
}

/// <summary>
/// Specify a to-many relationship of this resource.
/// </summary>
/// <param name="name">The name of the relationship.</param>
/// <summary>
/// Specify a to-many relationship of this resource.
/// </summary>
/// <param name="name">The name of the relationship.</param>
/// <typeparam name="T">The api resource type of the relationship.</typeparam>
/// <returns>The <see cref="ResourceRelationship"/>.</returns>
protected ResourceRelationship HasMany<T>(string name)
Expand All @@ -148,12 +148,12 @@ protected ResourceRelationship HasMany<T>(string name)
return HasMany<T>(name, name);
}

/// <summary>
/// Specify a to-many relationship of this resource.
/// </summary>
/// <param name="name">The name of the relationship.</param>
/// <param name="path">The url pathspec of this relationship (default is the name).</param>
/// <typeparam name="T">The api resource type of the relationship.</typeparam>
/// <summary>
/// Specify a to-many relationship of this resource.
/// </summary>
/// <param name="name">The name of the relationship.</param>
/// <param name="path">The url pathspec of this relationship (default is the name).</param>
/// <typeparam name="T">The api resource type of the relationship.</typeparam>
/// <returns>The <see cref="ResourceRelationship"/>.</returns>
protected ResourceRelationship HasMany<T>(string name, string path)
where T : ApiResource, new()
Expand Down Expand Up @@ -197,5 +197,5 @@ private static T GetUniqueResource<T>()
: new T();
return resource;
}
}
}
}
Loading

0 comments on commit a2990dc

Please sign in to comment.