Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic support for action filters is close :)
Signed-off-by: Jimmy Schementi <jschementi@gmail.com>
- Loading branch information
1 parent
a6a4e14
commit 4f606c0
Showing
21 changed files
with
510 additions
and
126 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using IronRuby.Builtins; | ||
using IronRubyMvcLibrary.Core; | ||
using IronRubyMvcLibrary.Extensions; | ||
using Microsoft.Scripting; | ||
|
||
namespace IronRubyMvcLibrary.Controllers | ||
{ | ||
public interface IConverter<TTarget> | ||
{ | ||
TTarget Convert(); | ||
} | ||
public class HashToActionFilterConverter : IConverter<RubyActionFilter> | ||
{ | ||
private readonly Hash _filterDescription; | ||
|
||
private static readonly IEnumerable<SymbolId> _actionFilterDenominators = new[] | ||
{ | ||
SymbolTable.StringToId("before"), | ||
SymbolTable.StringToId("after"), | ||
SymbolTable.StringToId("around"), | ||
SymbolTable.StringToId("before_result"), | ||
SymbolTable.StringToId("after_result") | ||
}; | ||
|
||
private static readonly IDictionary<When, SymbolId> _actionWhen = new Dictionary<When, SymbolId> | ||
{ | ||
{When.BeforeAction, SymbolTable.StringToId("before")}, | ||
{When.AfterAction, SymbolTable.StringToId("after")}, | ||
{When.BeforeResult, SymbolTable.StringToId("before_result")}, | ||
{When.AfterResult, SymbolTable.StringToId("after_result")}, | ||
|
||
}; | ||
|
||
private static readonly SymbolId whenKey = SymbolTable.StringToId("when"); | ||
|
||
public HashToActionFilterConverter(Hash filterDescription) | ||
{ | ||
_filterDescription = filterDescription; | ||
} | ||
|
||
#region Implementation of IConverter<RubyActionFilter> | ||
|
||
public RubyActionFilter Convert() | ||
{ | ||
if (!IsActionFilter(_filterDescription)) | ||
throw new InvalidDataException("The filter description is invalid."); | ||
|
||
return new RubyActionFilter | ||
{ | ||
BeforeAction = FindProc(_filterDescription, _actionWhen[When.BeforeAction]), | ||
AfterAction = FindProc(_filterDescription, _actionWhen[When.AfterAction]), | ||
BeforeResult = FindProc(_filterDescription, _actionWhen[When.BeforeResult]), | ||
AfterResult = FindProc(_filterDescription, _actionWhen[When.AfterResult]) | ||
}; | ||
} | ||
|
||
#endregion | ||
|
||
private static bool IsActionFilter(IDictionary<object, object> hash) | ||
{ | ||
var key = (SymbolId)hash[whenKey]; | ||
return _actionFilterDenominators.Contains(key); | ||
} | ||
|
||
private static Proc FindProc(IDictionary<object, object> hash, SymbolId key) | ||
{ | ||
return hash.ContainsKey(key) ? (Proc)hash[key] : null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
#region Usings | ||
|
||
using System.Collections.Generic; | ||
using System.Web.Mvc; | ||
using IronRuby.Builtins; | ||
using IronRubyMvcLibrary.Core; | ||
using IronRubyMvcLibrary.Extensions; | ||
|
||
#endregion | ||
|
||
namespace IronRubyMvcLibrary.Controllers | ||
{ | ||
/// <summary> | ||
/// Implements <see cref="IActionFilter"/> and <see cref="IResultFilter"/> | ||
/// This class hooks the ruby defined filters with | ||
/// </summary> | ||
public class RubyActionFilter : IActionFilter, IResultFilter | ||
{ | ||
public IEnumerable<string> OnlyForActions { get; set; } | ||
public IEnumerable<string> ExceptForActions { get; set; } | ||
public Proc BeforeAction { get; set; } | ||
public Proc AfterAction { get; set; } | ||
public Proc BeforeResult { get; set; } | ||
public Proc AfterResult { get; set; } | ||
public When FilterType { get; set; } | ||
|
||
#region Implementation of IActionFilter | ||
|
||
/// <summary> | ||
/// Called before the action executes | ||
/// </summary> | ||
/// <param name="filterContext">The filter context.</param> | ||
public void OnActionExecuting(ActionExecutingContext filterContext) | ||
{ | ||
if (BeforeAction.IsNotNull() && CanExecute(filterContext)) | ||
BeforeAction.Call(filterContext); | ||
} | ||
|
||
/// <summary> | ||
/// Called after an action executed | ||
/// </summary> | ||
/// <param name="filterContext">The filter context.</param> | ||
public void OnActionExecuted(ActionExecutedContext filterContext) | ||
{ | ||
if ( AfterAction.IsNotNull() && CanExecute(filterContext)) | ||
AfterAction.Call(filterContext); | ||
} | ||
|
||
#endregion | ||
|
||
#region Implementation of IResultFilter | ||
|
||
public void OnResultExecuting(ResultExecutingContext filterContext) | ||
{ | ||
if (BeforeResult.IsNotNull()) | ||
BeforeResult.Call(filterContext); | ||
} | ||
|
||
public void OnResultExecuted(ResultExecutedContext filterContext) | ||
{ | ||
if (AfterResult.IsNotNull()) | ||
AfterResult.Call(filterContext); | ||
} | ||
|
||
#endregion | ||
|
||
#region Implementation of IRubyControllerFilter | ||
|
||
private bool CanExecute(ActionExecutingContext context) | ||
{ | ||
var actionName = context.ActionDescriptor.ActionName; | ||
return CanExecute(actionName); | ||
} | ||
|
||
private bool CanExecute(ActionExecutedContext context) | ||
{ | ||
var actionName = context.ActionDescriptor.ActionName; | ||
return CanExecute(actionName); | ||
} | ||
|
||
private bool CanExecute(string actionName) | ||
{ | ||
return OnlyForActions.IsEmpty() || OnlyForActions.Contains(actionName) | ||
|| ExceptForActions.IsEmpty() || ExceptForActions.DoesNotContain(actionName); | ||
} | ||
|
||
#endregion | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.