using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Mustache {
///
/// Defines a tag that conditionally prints its content, based on whether the passed in values are equal
///
internal sealed class LteTagDefinition : ConditionTagDefinition {
private const string ConditionParameter = "condition";
private const string TargetValueParameter = "targetValue";
///
/// Initializes a new instance of a IfTagDefinition.
///
public LteTagDefinition()
: base("lte") {}
///
/// Gets whether the tag only exists within the scope of its parent.
///
protected override bool GetIsContextSensitive() {
return false;
}
///
/// Gets the parameters that can be passed to the tag.
///
/// The parameters.
protected override IEnumerable GetParameters() {
return new[] {
new TagParameter(ConditionParameter) {IsRequired = true},
new TagParameter(TargetValueParameter) {IsRequired = true}
};
}
///
/// Gets whether the primary generator group should be used to render the tag.
///
/// The arguments passed to the tag.
///
/// True if the primary generator group should be used to render the tag;
/// otherwise, false to use the secondary group.
///
public override bool ShouldGeneratePrimaryGroup(Dictionary arguments) {
object condition = arguments[ConditionParameter];
object targetValue = arguments[TargetValueParameter];
return isConditionSatisfied(condition, targetValue);
}
private bool isConditionSatisfied(object condition, object targetValue) {
if (condition == null || targetValue == null) {
return false;
}
try {
return Convert.ToDouble(condition) <= Convert.ToDouble(targetValue);
} catch (Exception exception) {
return false;
}
return false;
}
///
/// Gets the parameters that are used to create a new child context.
///
/// The parameters that are used to create a new child context.
public override IEnumerable GetChildContextParameters() {
return new TagParameter[0];
}
}
}