Skip to content

Commit

Permalink
Refactored RubyEngine to be true facade and the controller factory to…
Browse files Browse the repository at this point in the history
… actually be the factory
  • Loading branch information
casualjim committed Mar 26, 2009
1 parent 6f43a2a commit cf9417c
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 144 deletions.
79 changes: 73 additions & 6 deletions IronRubyMvc/Controllers/RubyControllerFactory.cs
@@ -1,7 +1,10 @@
#region Usings #region Usings


using System.Text;
using System.Web.Mvc.IronRuby.Core; using System.Web.Mvc.IronRuby.Core;
using System.Web.Mvc.IronRuby.Extensions;
using System.Web.Routing; using System.Web.Routing;
using IronRuby.Builtins;


#endregion #endregion


Expand All @@ -10,10 +13,12 @@ namespace System.Web.Mvc.IronRuby.Controllers
public class RubyControllerFactory : IControllerFactory public class RubyControllerFactory : IControllerFactory
{ {
private readonly IRubyEngine _engine; private readonly IRubyEngine _engine;
private readonly IPathProvider _pathProvider;
private readonly IControllerFactory _innerFactory; private readonly IControllerFactory _innerFactory;


internal RubyControllerFactory(IControllerFactory innerFactory, IRubyEngine engine) internal RubyControllerFactory(IPathProvider pathProvider, IControllerFactory innerFactory, IRubyEngine engine)
{ {
_pathProvider = pathProvider;
_innerFactory = innerFactory; _innerFactory = innerFactory;
_engine = engine; _engine = engine;
} }
Expand All @@ -22,20 +27,21 @@ internal RubyControllerFactory(IControllerFactory innerFactory, IRubyEngine engi


public IController CreateController(RequestContext requestContext, string controllerName) public IController CreateController(RequestContext requestContext, string controllerName)
{ {

try try
{ {
return _innerFactory.CreateController(requestContext, controllerName); 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) public void ReleaseController(IController controller)
{ {
var disposable = controller as IDisposable; var disposable = controller as IDisposable;
Expand All @@ -45,5 +51,66 @@ public void ReleaseController(IController controller)
} }


#endregion #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;
}
} }
} }
79 changes: 55 additions & 24 deletions IronRubyMvc/Core/IRubyEngine.cs
Expand Up @@ -12,23 +12,26 @@


namespace System.Web.Mvc.IronRuby.Core namespace System.Web.Mvc.IronRuby.Core
{ {
/// <summary>
/// A facade over the classes for interacting with the IronRuby runtime
/// </summary>
public interface IRubyEngine public interface IRubyEngine
{ {
/// <summary> // /// <summary>
/// Loads the controller. // /// Loads the controller.
/// </summary> // /// </summary>
/// <param name="requestContext">The request context.</param> // /// <param name="requestContext">The request context.</param>
/// <param name="controllerName">Name of the controller.</param> // /// <param name="controllerName">Name of the controller.</param>
/// <returns></returns> // /// <returns></returns>
RubyController LoadController(RequestContext requestContext, string controllerName); // RubyController LoadController(RequestContext requestContext, string controllerName);

//
/// <summary> // /// <summary>
/// Configures the controller. // /// Configures the controller.
/// </summary> // /// </summary>
/// <param name="rubyClass">The ruby class.</param> // /// <param name="rubyClass">The ruby class.</param>
/// <param name="requestContext">The request context.</param> // /// <param name="requestContext">The request context.</param>
/// <returns></returns> // /// <returns></returns>
RubyController ConfigureController(RubyClass rubyClass, RequestContext requestContext); // RubyController ConfigureController(RubyClass rubyClass, RequestContext requestContext);


/// <summary> /// <summary>
/// Calls the method. /// Calls the method.
Expand All @@ -39,15 +42,15 @@ public interface IRubyEngine
/// <returns></returns> /// <returns></returns>
object CallMethod(object receiver, string message, params object[] args); object CallMethod(object receiver, string message, params object[] args);


/// <summary> // /// <summary>
/// Determines whether the specified controller as the action. // /// Determines whether the specified controller as the action.
/// </summary> // /// </summary>
/// <param name="controller">The controller.</param> // /// <param name="controller">The controller.</param>
/// <param name="actionName">Name of the action.</param> // /// <param name="actionName">Name of the action.</param>
/// <returns> // /// <returns>
/// <c>true</c> if the specified controller has the action; otherwise, <c>false</c>. // /// <c>true</c> if the specified controller has the action; otherwise, <c>false</c>.
/// </returns> // /// </returns>
bool HasControllerAction(RubyController controller, string actionName); // bool HasControllerAction(RubyController controller, string actionName);


/// <summary> /// <summary>
/// Gets the method names for the controller class. /// Gets the method names for the controller class.
Expand Down Expand Up @@ -91,6 +94,21 @@ public interface IRubyEngine
/// <param name="value">The value.</param> /// <param name="value">The value.</param>
void DefineReadOnlyGlobalVariable(string variableName, object value); 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> /// <summary>
/// Gets the ruby class. /// Gets the ruby class.
/// </summary> /// </summary>
Expand Down Expand Up @@ -118,5 +136,18 @@ public interface IRubyEngine
/// </summary> /// </summary>
/// <param name="block">The block.</param> /// <param name="block">The block.</param>
void ExecuteInScope(Action<ScriptScope> block); 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);
} }
} }

0 comments on commit cf9417c

Please sign in to comment.