Skip to content

Commit

Permalink
feat (Builders): moves attribute/class modification to interfaces
Browse files Browse the repository at this point in the history
This should make future extension methods easier to share.
  • Loading branch information
jamiepollock committed Jan 15, 2024
1 parent 70d7314 commit c542670
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 73 deletions.
25 changes: 25 additions & 0 deletions src/Rhythm.Drop.Builders/Links/IHtmlAttributesLinkBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace Rhythm.Drop.Builders.Links;

using Rhythm.Drop.Models.Common.Attributes;

/// <summary>
/// A contract for creating a link builder which modifies attributes.
/// </summary>
/// <typeparam name="TBuilder">The type of the builder to return after modifying HTML attributes.</typeparam>
public interface IHtmlAttributesLinkBuilder<TBuilder>
{
/// <summary>
/// Adds an attribute to the current builder.
/// </summary>
/// <param name="name">The name of the attribute.</param>
/// <param name="value">The value of the attribute.</param>
/// <returns>A <typeparamref name="TBuilder"/>.</returns>
TBuilder IncludeAttribute(string name, object? value);

/// <summary>
/// Removes an attribute from the current builder.
/// </summary>
/// <param name="name">The name of the attribute.</param>
/// <returns>A <typeparamref name="TBuilder"/>.</returns>
TBuilder ExcludeAttribute(string name);
}
22 changes: 22 additions & 0 deletions src/Rhythm.Drop.Builders/Links/IHtmlClassesLinkBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace Rhythm.Drop.Builders.Links;

/// <summary>
/// A contract for creating a link builder which modifies HTML classes.
/// </summary>
/// <typeparam name="TBuilder">The type of the builder to return after modifying HTML classes.</typeparam>
public interface IHtmlClassesLinkBuilder<TBuilder>
{
/// <summary>
/// Adds a class to the current builder.
/// </summary>
/// <param name="className">The class name to add.</param>
/// <returns>A <typeparamref name="TBuilder"/>.</returns>
TBuilder AddClass(string className);

/// <summary>
/// Adds a class to the current builder.
/// </summary>
/// <param name="className">The class name to remove.</param>
/// <returns>A <typeparamref name="TBuilder"/>.</returns>
TBuilder RemoveClass(string className);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/// <summary>
/// A contract for creating a link builder with modal and a label.
/// </summary>
public interface IModalAndLabelLinkBuilder
public interface IModalAndLabelLinkBuilder : IHtmlAttributesLinkBuilder<IModalAndLabelLinkBuilder>, IHtmlClassesLinkBuilder<IModalAndLabelLinkBuilder>
{
/// <summary>
/// Gets the label of the link builder.
Expand All @@ -18,35 +18,6 @@ public interface IModalAndLabelLinkBuilder
/// </summary>
IModal Modal { get; }

/// <summary>
/// Adds an attribute to the current builder.
/// </summary>
/// <param name="name">The name of the attribute.</param>
/// <param name="value">The value of the attribute.</param>
/// <returns>A <see cref="IModalAndLabelLinkBuilder"/>.</returns>
IModalAndLabelLinkBuilder IncludeAttribute(string name, object? value);

/// <summary>
/// Removes an attribute from the current builder.
/// </summary>
/// <param name="name">The name of the attribute.</param>
/// <returns>A <see cref="IModalAndLabelLinkBuilder"/>.</returns>
IModalAndLabelLinkBuilder ExcludeAttribute(string name);

/// <summary>
/// Adds a class to the current builder.
/// </summary>
/// <param name="className">The class name to add.</param>
/// <returns>A <see cref="IModalAndLabelLinkBuilder"/>.</returns>
IModalAndLabelLinkBuilder AddClass(string className);

/// <summary>
/// Adds a class to the current builder.
/// </summary>
/// <param name="className">The class name to remove.</param>
/// <returns>A <see cref="IModalAndLabelLinkBuilder"/>.</returns>
IModalAndLabelLinkBuilder RemoveClass(string className);

/// <summary>
/// Attempts to build a <see cref="IModalLink"/> based on the current input.
/// </summary>
Expand Down
3 changes: 0 additions & 3 deletions src/Rhythm.Drop.Builders/Links/Modals/ModalLinkBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ internal sealed class ModalLinkBuilder(IModal modal) : IModalLinkBuilder, IModal
/// <inheritdoc/>
public IModal Modal => modal;

/// <inheritdoc/>
public IReadOnlyHtmlAttributeCollection Attributes => _attributes.ToReadOnly();

/// <inheritdoc/>
public string? Label { get; private set; }

Expand Down
37 changes: 1 addition & 36 deletions src/Rhythm.Drop.Builders/Links/Url/IUrlAndLabelLinkBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
namespace Rhythm.Drop.Builders.Links.Url;

using Rhythm.Drop.Builders.Links;
using Rhythm.Drop.Models.Common.Attributes;
using Rhythm.Drop.Models.Links;

/// <summary>
/// A contract for creating a <see cref="ILinkBuilder"/> with a URL and label.
/// </summary>
public interface IUrlAndLabelLinkBuilder
public interface IUrlAndLabelLinkBuilder : IHtmlAttributesLinkBuilder<IUrlAndLabelLinkBuilder>, IHtmlClassesLinkBuilder<IUrlAndLabelLinkBuilder>
{
/// <summary>
/// Gets the URL of the link builder.
Expand All @@ -19,43 +18,9 @@ public interface IUrlAndLabelLinkBuilder
/// </summary>
string? Label { get; }

/// <summary>
/// Gets the attributes of the link builder.
/// </summary>
IReadOnlyHtmlAttributeCollection Attributes { get; }

/// <summary>
/// Attempts to build a <see cref="ILink"/> with a URL and label.
/// </summary>
/// <returns>A <see cref="ILink"/> if the input is valid.</returns>
ILink? Build();

/// <summary>
/// Adds an attribute to the current builder.
/// </summary>
/// <param name="name">The name of the attribute.</param>
/// <param name="value">The value of the attribute.</param>
/// <returns>A <see cref="IUrlAndLabelLinkBuilder"/>.</returns>
IUrlAndLabelLinkBuilder IncludeAttribute(string name, object? value);

/// <summary>
/// Removes an attribute from the current builder.
/// </summary>
/// <param name="name">The name of the attribute.</param>
/// <returns>A <see cref="IUrlAndLabelLinkBuilder"/>.</returns>
IUrlAndLabelLinkBuilder ExcludeAttribute(string name);

/// <summary>
/// Adds a class to the current builder.
/// </summary>
/// <param name="className">The class name to add.</param>
/// <returns>A <see cref="IUrlAndLabelLinkBuilder"/>.</returns>
IUrlAndLabelLinkBuilder AddClass(string className);

/// <summary>
/// Adds a class to the current builder.
/// </summary>
/// <param name="className">The class name to remove.</param>
/// <returns>A <see cref="IUrlAndLabelLinkBuilder"/>.</returns>
IUrlAndLabelLinkBuilder RemoveClass(string className);
}
6 changes: 2 additions & 4 deletions src/Rhythm.Drop.Builders/Links/Url/UrlLinkBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using Rhythm.Drop.Models.Common.Attributes;
using Rhythm.Drop.Models.Links;

/// <summary>
/// An implementation of <see cref="IUrlLinkBuilder"/>.
/// </summary>
Expand All @@ -19,9 +20,6 @@ internal sealed class UrlLinkBuilder(string? url) : IUrlLinkBuilder, IUrlAndLabe
/// <inheritdoc/>
public string? Label { get; private set; }

/// <inheritdoc/>
public IReadOnlyHtmlAttributeCollection Attributes => _attributes.ToReadOnly();

/// <inheritdoc/>
public IUrlAndLabelLinkBuilder AndLabel(string? label)
{
Expand All @@ -39,7 +37,7 @@ public IUrlAndLabelLinkBuilder AndLabel(string? label)

_attributes.SetAttribute("href", Url);

return new AnchorLink(Label, Attributes);
return new AnchorLink(Label, _attributes.ToReadOnly());
}

/// <inheritdoc/>
Expand Down

0 comments on commit c542670

Please sign in to comment.