Skip to content
Browse files

Fixed an issue with the binder

  • Loading branch information...
1 parent d845815 commit 7344b8c392a4510c9501c73632e8246bf712edaa @casualjim casualjim committed Mar 28, 2009
View
4 IronRubyMvc/Controllers/ParamsBinder.cs
@@ -20,11 +20,11 @@ public object BindModel(ControllerContext controllerContext, ModelBindingContext
controllerContext.EnsureArgumentNotNull("controllerContext");
bindingContext.EnsureArgumentNotNull("bindingContext");
- _params = (bindingContext.Model as Dictionary<SymbolId, object>) ?? new Dictionary<SymbolId, object>();
+ _params = (bindingContext.Model as IDictionary<SymbolId, object>) ?? new Dictionary<SymbolId, object>();
bindingContext.ValueProvider.ForEach(pair =>
{
bindingContext.ModelState.SetModelValue(pair.Key, pair.Value);
- _params.Add(pair.Key.ToSymbolId(), pair.Value.RawValue);
+ _params.Add(pair.Key.ToSymbolId(), pair.Value.AttemptedValue);
});
// var request = controllerContext.HttpContext.Request;
// var modelState = controllerContext.Controller.ViewData.ModelState;
View
19 IronRubyMvc/Controllers/RubyActionMethodSelector.cs
@@ -57,7 +57,9 @@ private void PopulateLookupTables(IEnumerable<string> methodNames)
methodNames.Where(
method =>
AliasedMethods.DoesNotContain(
- pair => String.Equals(pair.Key, method.Underscore(), StringComparison.OrdinalIgnoreCase) || String.Equals(pair.Key, method.Pascalize(), StringComparison.OrdinalIgnoreCase)));
+ pair => String.Equals(pair.Key, method.Underscore(), StringComparison.OrdinalIgnoreCase) || String.Equals(pair.Key, method.Pascalize(), StringComparison.OrdinalIgnoreCase)
+ )
+ );
}
private static KeyValuePair<string, PredicateList> KeyValuePairFor(KeyValuePair<object, object> pair)
@@ -155,20 +157,11 @@ public bool IsValidForAction(ControllerContext context, string name)
var result = false;
foreach (var list in _predicates)
{
- if (list(context, name))
- {
- result = true;
- break;
- }
+ if (!list(context, name)) continue;
+ result = true;
+ break;
}
return result;
-// var result = _predicates.Any(predicate =>
-// {
-// var result1 = predicate(context, name.Underscore());
-// var result2 = predicate(context, name.Pascalize());
-// return result1 || result2;
-// });
-// return result;
}
public Func<ControllerContext, string, TResult> ConvertProcToFunc<TResult>(Proc proc)
View
3 IronRubyMvc/Controllers/RubyController.cs
@@ -36,7 +36,7 @@ public string ControllerClassName
{
if (_params == null)
{
-
+ PopulateParams();
}
return _params;
@@ -67,6 +67,7 @@ internal void InternalInitialize(ControllerConfiguration config)
ControllerName = config.RubyClass.Name.Replace("Controller", string.Empty);
RubyType = config.RubyClass;
Binders = RubyModelBinders.Binders;
+// ValueProvider = new RubyValueProviderDictionary(ControllerContext);
}
protected override void Execute(RequestContext requestContext)
View
167 IronRubyMvc/Controllers/RubyValueProviderDictionary.cs
@@ -0,0 +1,167 @@
+#region Usings
+
+using System.Collections;
+using System.Collections.Generic;
+using System.Collections.Specialized;
+using System.Globalization;
+using System.Web.Mvc.IronRuby.Extensions;
+
+#endregion
+
+namespace System.Web.Mvc.IronRuby.Controllers
+{
+ public class RubyValueProviderDictionary : IDictionary<string, ValueProviderResult>
+ {
+ private readonly IDictionary<string, ValueProviderResult> _dictionary = new Dictionary<string, ValueProviderResult>();
+
+ public RubyValueProviderDictionary(ControllerContext controllerContext)
+ {
+ ControllerContext = controllerContext;
+ if (controllerContext.IsNotNull()) PopulateDictionary();
+ }
+
+ public ControllerContext ControllerContext { get; set; }
+
+ private void PopulateDictionary()
+ {
+ var request = ControllerContext.HttpContext.Request;
+ PopulateParamsWithRequestData(request.Form);
+ PopulateParamsWithRequestData(request.QueryString);
+ PopulateParamsWithRouteData(ControllerContext.RouteData.Values);
+ }
+
+ private void PopulateParamsWithRouteData(IEnumerable<KeyValuePair<string, object>> dictionary)
+ {
+ if (dictionary.IsNull()) return;
+ dictionary.ForEach(pair =>
+ {
+ var rawValue = pair.Value;
+ var attempted = Convert.ToString(pair.Value, CultureInfo.InvariantCulture);
+ AddToDictionaryIfNotPresent(
+ pair.Key,
+ new ValueProviderResult(rawValue, attempted, CultureInfo.InvariantCulture)
+ );
+ });
+ }
+
+ private void PopulateParamsWithRequestData(NameValueCollection collection)
+ {
+ if(collection.IsNull()) return;
+ collection.AllKeys.ForEach(key =>
+ {
+ var rawValue = collection.GetValues(key);
+ var attempted = collection.Get(key);
+ AddToDictionaryIfNotPresent(
+ key,
+ new ValueProviderResult(rawValue, attempted, CultureInfo.InvariantCulture)
+ );
+ });
+ }
+
+ private void AddToDictionaryIfNotPresent(string key, ValueProviderResult result)
+ {
+ if (key.IsNullOrBlank() || _dictionary.ContainsKey(key)) return;
+ _dictionary.Add(key, result);
+ }
+
+ #region Implementation of IEnumerable
+
+ public IEnumerator<KeyValuePair<string, ValueProviderResult>> GetEnumerator()
+ {
+ return _dictionary.GetEnumerator();
+ }
+
+ IEnumerator IEnumerable.GetEnumerator()
+ {
+ return GetEnumerator();
+ }
+
+ #endregion
+
+ #region Implementation of ICollection<KeyValuePair<string,ValueProviderResult>>
+
+ public void Add(KeyValuePair<string, ValueProviderResult> item)
+ {
+ item.EnsureArgumentNotNull("item");
+ _dictionary.Add(item);
+ }
+
+ public void Clear()
+ {
+ _dictionary.Clear();
+ }
+
+ public bool Contains(KeyValuePair<string, ValueProviderResult> item)
+ {
+ item.EnsureArgumentNotNull("item");
+
+ return _dictionary.Contains(item);
+ }
+
+ public void CopyTo(KeyValuePair<string, ValueProviderResult>[] array, int arrayIndex)
+ {
+ array.EnsureArgumentNotNull("array");
+
+ _dictionary.CopyTo(array, arrayIndex);
+ }
+
+ public bool Remove(KeyValuePair<string, ValueProviderResult> item)
+ {
+ item.EnsureArgumentNotNull("item");
+
+ return _dictionary.Remove(item);
+ }
+
+ public int Count
+ {
+ get { return _dictionary.Count; }
+ }
+
+ public bool IsReadOnly
+ {
+ get { return _dictionary.IsReadOnly; }
+ }
+
+ #endregion
+
+ #region Implementation of IDictionary<string,ValueProviderResult>
+
+ public bool ContainsKey(string key)
+ {
+ return _dictionary.ContainsKey(key);
+ }
+
+ public void Add(string key, ValueProviderResult value)
+ {
+ _dictionary.Add(key, value);
+ }
+
+ public bool Remove(string key)
+ {
+ return _dictionary.Remove(key);
+ }
+
+ public bool TryGetValue(string key, out ValueProviderResult value)
+ {
+ return _dictionary.TryGetValue(key, out value);
+ }
+
+ public ValueProviderResult this[string key]
+ {
+ get { return _dictionary[key]; }
+ set { _dictionary[key] = value; }
+ }
+
+ public ICollection<string> Keys
+ {
+ get { return _dictionary.Keys; }
+ }
+
+ public ICollection<ValueProviderResult> Values
+ {
+ get { return _dictionary.Values; }
+ }
+
+ #endregion
+ }
+}

0 comments on commit 7344b8c

Please sign in to comment.
Something went wrong with that request. Please try again.