-
Notifications
You must be signed in to change notification settings - Fork 6
/
HashToActionFilterConverter.cs
73 lines (61 loc) · 3.28 KB
/
HashToActionFilterConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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;
}
}
}