Permalink
Browse files

Refactored RubyEngine to be true facade and the controller factory to…

… actually be the factory
  • Loading branch information...
1 parent 6f43a2a commit cf9417c0dd58c940dc6901ad41fb4133aed918ec @casualjim casualjim committed Mar 26, 2009
Showing with 176 additions and 144 deletions.
  1. +73 −6 IronRubyMvc/Controllers/RubyControllerFactory.cs
  2. +55 −24 IronRubyMvc/Core/IRubyEngine.cs
  3. +48 −114 IronRubyMvc/Core/RubyEngine.cs
@@ -1,7 +1,10 @@
#region Usings
+using System.Text;
using System.Web.Mvc.IronRuby.Core;
+using System.Web.Mvc.IronRuby.Extensions;
using System.Web.Routing;
+using IronRuby.Builtins;
#endregion
@@ -10,10 +13,12 @@ namespace System.Web.Mvc.IronRuby.Controllers
public class RubyControllerFactory : IControllerFactory
{
private readonly IRubyEngine _engine;
+ private readonly IPathProvider _pathProvider;
private readonly IControllerFactory _innerFactory;
- internal RubyControllerFactory(IControllerFactory innerFactory, IRubyEngine engine)
+ internal RubyControllerFactory(IPathProvider pathProvider, IControllerFactory innerFactory, IRubyEngine engine)
{
+ _pathProvider = pathProvider;
_innerFactory = innerFactory;
_engine = engine;
}
@@ -22,20 +27,21 @@ internal RubyControllerFactory(IControllerFactory innerFactory, IRubyEngine engi
public IController CreateController(RequestContext requestContext, string controllerName)
{
-
try
{
return _innerFactory.CreateController(requestContext, controllerName);
}
- catch(InvalidOperationException)
+ catch (InvalidOperationException)
+ {
+ }
+ catch (HttpException)
{
}
- catch(HttpException){}
-
- return _engine.LoadController(requestContext, controllerName);
+ return LoadController(requestContext, controllerName);
}
+
public void ReleaseController(IController controller)
{
var disposable = controller as IDisposable;
@@ -45,5 +51,66 @@ public void ReleaseController(IController controller)
}
#endregion
+
+ /// <summary>
+ /// Loads the controller.
+ /// </summary>
+ /// <param name="requestContext">The request context.</param>
+ /// <param name="controllerName">Name of the controller.</param>
+ /// <returns></returns>
+ private RubyController LoadController(RequestContext requestContext, string controllerName)
+ {
+ var controllerFilePath = GetControllerFilePath(controllerName);
+ var controllerClassName = GetControllerClassName(controllerName);
+
+ _engine.RemoveClassFromGlobals(controllerClassName);
+
+ if (controllerFilePath.IsNullOrBlank())
+ return null;
+
+ _engine.RequireRubyFile(_pathProvider.MapPath(controllerFilePath));
+
+ var controllerClass = _engine.GetRubyClass(controllerClassName);
+ var controller = ConfigureController(controllerClass, requestContext);
+
+ return controller;
+ }
+
+
+ /// <summary>
+ /// Configures the controller.
+ /// </summary>
+ /// <param name="rubyClass">The ruby class.</param>
+ /// <param name="requestContext">The request context.</param>
+ /// <returns></returns>
+ private RubyController ConfigureController(RubyClass rubyClass, RequestContext requestContext)
+ {
+ var controller = _engine.CreateInstance<RubyController>(rubyClass);
+ controller.InternalInitialize(new ControllerConfiguration { Context = requestContext, Engine = _engine, RubyClass = rubyClass });
+ return controller;
+ }
+
+ /// <summary>
+ /// Gets the name of the controller class.
+ /// </summary>
+ /// <param name="controllerName">Name of the controller.</param>
+ /// <returns></returns>
+ private static string GetControllerClassName(string controllerName)
+ {
+ return (controllerName.EndsWith("Controller", StringComparison.OrdinalIgnoreCase)
+ ? controllerName
+ : Constants.ControllerclassFormat.FormattedWith(controllerName)).Pascalize();
+ }
+
+ private string GetControllerFilePath(string controllerName)
+ {
+ var fileName = Constants.ControllerPascalPathFormat.FormattedWith(controllerName.Pascalize());
+ if (_pathProvider.FileExists(fileName))
+ return fileName;
+
+ fileName = Constants.ControllerUnderscorePathFormat.FormattedWith(controllerName.Underscore());
+
+ return _pathProvider.FileExists(fileName) ? fileName : string.Empty;
+ }
}
}
@@ -12,23 +12,26 @@
namespace System.Web.Mvc.IronRuby.Core
{
+ /// <summary>
+ /// A facade over the classes for interacting with the IronRuby runtime
+ /// </summary>
public interface IRubyEngine
{
- /// <summary>
- /// Loads the controller.
- /// </summary>
- /// <param name="requestContext">The request context.</param>
- /// <param name="controllerName">Name of the controller.</param>
- /// <returns></returns>
- RubyController LoadController(RequestContext requestContext, string controllerName);
-
- /// <summary>
- /// Configures the controller.
- /// </summary>
- /// <param name="rubyClass">The ruby class.</param>
- /// <param name="requestContext">The request context.</param>
- /// <returns></returns>
- RubyController ConfigureController(RubyClass rubyClass, RequestContext requestContext);
+// /// <summary>
+// /// Loads the controller.
+// /// </summary>
+// /// <param name="requestContext">The request context.</param>
+// /// <param name="controllerName">Name of the controller.</param>
+// /// <returns></returns>
+// RubyController LoadController(RequestContext requestContext, string controllerName);
+//
+// /// <summary>
+// /// Configures the controller.
+// /// </summary>
+// /// <param name="rubyClass">The ruby class.</param>
+// /// <param name="requestContext">The request context.</param>
+// /// <returns></returns>
+// RubyController ConfigureController(RubyClass rubyClass, RequestContext requestContext);
/// <summary>
/// Calls the method.
@@ -39,15 +42,15 @@ public interface IRubyEngine
/// <returns></returns>
object CallMethod(object receiver, string message, params object[] args);
- /// <summary>
- /// Determines whether the specified controller as the action.
- /// </summary>
- /// <param name="controller">The controller.</param>
- /// <param name="actionName">Name of the action.</param>
- /// <returns>
- /// <c>true</c> if the specified controller has the action; otherwise, <c>false</c>.
- /// </returns>
- bool HasControllerAction(RubyController controller, string actionName);
+// /// <summary>
+// /// Determines whether the specified controller as the action.
+// /// </summary>
+// /// <param name="controller">The controller.</param>
+// /// <param name="actionName">Name of the action.</param>
+// /// <returns>
+// /// <c>true</c> if the specified controller has the action; otherwise, <c>false</c>.
+// /// </returns>
+// bool HasControllerAction(RubyController controller, string actionName);
/// <summary>
/// Gets the method names for the controller class.
@@ -91,6 +94,21 @@ public interface IRubyEngine
/// <param name="value">The value.</param>
void DefineReadOnlyGlobalVariable(string variableName, object value);
+
+ /// <summary>
+ /// Removes the class from globals.
+ /// </summary>
+ /// <param name="className">Name of the class.</param>
+ void RemoveClassFromGlobals(string className);
+
+ /// <summary>
+ /// Creates an instance of a ruby object.
+ /// </summary>
+ /// <typeparam name="T"></typeparam>
+ /// <param name="rubyClass">The ruby class.</param>
+ /// <returns></returns>
+ T CreateInstance<T>(RubyClass rubyClass);
+
/// <summary>
/// Gets the ruby class.
/// </summary>
@@ -118,5 +136,18 @@ public interface IRubyEngine
/// </summary>
/// <param name="block">The block.</param>
void ExecuteInScope(Action<ScriptScope> block);
+
+ /// <summary>
+ /// Requires the ruby file.
+ /// </summary>
+ /// <param name="path">The path.</param>
+ void RequireRubyFile(string path);
+
+ /// <summary>
+ /// Requires the ruby file.
+ /// </summary>
+ /// <param name="path">The path.</param>
+ /// <param name="readerType">Type of the reader.</param>
+ void RequireRubyFile(string path, ReaderType readerType);
}
}
Oops, something went wrong.

0 comments on commit cf9417c

Please sign in to comment.