< Summary

Information
Class: MyNet.Text.Templating.TextTemplateOptionsBuilder
Assembly: MyNet.Text
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Text/Templating/TextTemplateOptionsBuilder.cs
Tag: 323_28699572109
Line coverage
87%
Covered lines: 27
Uncovered lines: 4
Coverable lines: 31
Total lines: 119
Line coverage: 87%
Branch coverage
0%
Covered branches: 0
Total branches: 2
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
WithQuantity(...)100%11100%
PrefixWithQuantity(...)100%11100%
SuffixWithQuantity(...)100%11100%
WithArgument(...)100%11100%
WithArguments(...)0%620%
Build()100%11100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Text/Templating/TextTemplateOptionsBuilder.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="TextTemplateOptionsBuilder.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Collections.Generic;
 9using System.Collections.ObjectModel;
 10
 11namespace MyNet.Text.Templating;
 12
 13/// <summary>
 14/// Builder for creating <see cref="TextTemplateOptions"/> instances for use with text templates.
 15/// </summary>
 16public sealed class TextTemplateOptionsBuilder
 17{
 5718    private readonly Dictionary<string, object?> _arguments = new(StringComparer.OrdinalIgnoreCase);
 19    private decimal? _quantity;
 5720    private QuantityRenderingMode _quantityRenderingMode = QuantityRenderingMode.PlaceholderOnly;
 5721    private string _quantitySeparator = " ";
 22    private string? _quantityFormat;
 23
 24    /// <summary>
 25    /// Sets the count used for pluralization and rendering.
 26    /// Automatically exposes the "count" rendering argument.
 27    /// </summary>
 28    /// <param name="count">The count value.</param>
 29    /// <param name="quantityRenderingMode">The mode for rendering quantity information.</param>
 30    /// <returns>The current builder.</returns>
 31    public TextTemplateOptionsBuilder WithQuantity(decimal count, QuantityRenderingMode quantityRenderingMode = Quantity
 32    {
 633        _quantity = count;
 634        _quantityRenderingMode = quantityRenderingMode;
 35
 636        return this;
 37    }
 38
 39    /// <summary>
 40    /// Sets the count used for pluralization and rendering, with the quantity value rendered as a prefix to the transla
 41    /// </summary>
 42    /// <param name="count">The count value.</param>
 43    /// <param name="quantityFormat">The format string to use when rendering the quantity value.</param>
 44    /// <param name="quantitySeparator">The separator to use between the quantity value and its unit.</param>
 45    /// <returns>The current builder.</returns>
 46    public TextTemplateOptionsBuilder PrefixWithQuantity(decimal count, string? quantityFormat = null, string quantitySe
 47    {
 3948        _quantity = count;
 3949        _quantityRenderingMode = QuantityRenderingMode.Prefix;
 3950        _quantitySeparator = quantitySeparator;
 3951        _quantityFormat = quantityFormat;
 52
 3953        return this;
 54    }
 55
 56    /// <summary>
 57    /// Sets the count used for pluralization and rendering, with the quantity value rendered as a suffix to the transla
 58    /// </summary>
 59    /// <param name="count">The count value.</param>
 60    /// <param name="quantityFormat">The format string to use when rendering the quantity value.</param>
 61    /// <param name="quantitySeparator">The separator to use between the quantity value and its unit.</param>
 62    /// <returns>The current builder.</returns>
 63    public TextTemplateOptionsBuilder SuffixWithQuantity(decimal count, string? quantityFormat = null, string quantitySe
 64    {
 365        _quantity = count;
 366        _quantityRenderingMode = QuantityRenderingMode.Suffix;
 367        _quantitySeparator = quantitySeparator;
 368        _quantityFormat = quantityFormat;
 69
 370        return this;
 71    }
 72
 73    /// <summary>
 74    /// Adds or replaces a rendering argument.
 75    /// </summary>
 76    /// <param name="name">The argument name.</param>
 77    /// <param name="value">The argument value.</param>
 78    /// <returns>The current builder.</returns>
 79    public TextTemplateOptionsBuilder WithArgument(string name, object? value)
 80    {
 381        ArgumentException.ThrowIfNullOrWhiteSpace(name);
 82
 383        _arguments[name] = value;
 84
 385        return this;
 86    }
 87
 88    /// <summary>
 89    /// Adds multiple rendering arguments.
 90    /// </summary>
 91    /// <param name="arguments">The arguments to add.</param>
 92    /// <returns>The current builder.</returns>
 93    public TextTemplateOptionsBuilder WithArguments(IEnumerable<KeyValuePair<string, object?>> arguments)
 94    {
 095        ArgumentNullException.ThrowIfNull(arguments);
 96
 097        foreach (var argument in arguments)
 98        {
 099            _arguments[argument.Key] = argument.Value;
 100        }
 101
 0102        return this;
 103    }
 104
 105    /// <summary>
 106    /// Builds the <see cref="TextTemplateOptions"/> instance with the configured options.
 107    /// </summary>
 108    /// <returns>The created text template options.</returns>
 109    public TextTemplateOptions Build()
 57110        => new()
 57111        {
 57112            Quantity = _quantity,
 57113            QuantityFormat = _quantityFormat,
 57114            QuantityRenderingMode = _quantityRenderingMode,
 57115            QuantitySeparator = _quantitySeparator,
 57116            Arguments = new ReadOnlyDictionary<string, object?>(new Dictionary<string, object?>(_arguments, StringCompar
 57117        };
 118}
 119