using System;
using System.Collections.Generic;
using System.Linq;
namespace mustache
{
///
/// Associates parameters to their argument values.
///
internal sealed class ArgumentCollection
{
private readonly Dictionary _argumentLookup;
///
/// Initializes a new instance of an ArgumentCollection.
///
public ArgumentCollection()
{
_argumentLookup = new Dictionary();
}
///
/// Associates the given parameter to the key placeholder.
///
/// The parameter to associate the key with.
/// The key placeholder used as the argument.
/// If the key is null, the default value of the parameter will be used.
public void AddArgument(TagParameter parameter, string key)
{
_argumentLookup.Add(parameter, key);
}
///
/// Gets the key that will be used to find the substitute value.
///
/// The name of the parameter.
public string GetKey(TagParameter parameter)
{
string key;
if (_argumentLookup.TryGetValue(parameter, out key))
{
return key;
}
else
{
return null;
}
}
///
/// Substitutes the key placeholders with their respective values.
///
/// The current lexical scope.
/// A dictionary associating the parameter name to the associated value.
public Dictionary GetArguments(KeyScope scope)
{
Dictionary arguments = new Dictionary();
foreach (KeyValuePair pair in _argumentLookup)
{
object value;
if (pair.Value == null)
{
value = pair.Key.DefaultValue;
}
else
{
value = scope.Find(pair.Value);
}
arguments.Add(pair.Key.Name, value);
}
return arguments;
}
}
}