Skip to content

Commit

Permalink
Adds the ruby engine facade to the view engine for dlr interactions
Browse files Browse the repository at this point in the history
  • Loading branch information
casualjim committed Mar 25, 2009
1 parent 8bdf47f commit 6f43a2a
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 74 deletions.
22 changes: 18 additions & 4 deletions IronRubyMvc/Controllers/RubyController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class RubyController : Controller

public string ControllerClassName
{
get { return Constants.CONTROLLERCLASS_FORMAT.FormattedWith(ControllerName); }
get { return Constants.ControllerclassFormat.FormattedWith(ControllerName); }
}

[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Params")]
Expand All @@ -46,6 +46,7 @@ public IDictionary<object, object> Params

private void PopulateParams()
{
//TODO: Possibly replace this with a binder implementation
var request = ControllerContext.HttpContext.Request;
_params =
new Dictionary<object, object>(ControllerContext.RouteData.Values.Count +
Expand All @@ -63,7 +64,7 @@ private void PopulateParamsWithFormData(HttpRequestBase request)
{
var symbolKey = SymbolTable.StringToId(key);
_params[symbolKey] = request.Form[key];
ModelState.Add(key, new ModelState{Value = new ValueProviderResult(request.Form[key], request.Form[key], CultureInfo.CurrentCulture)});
ModelState.Add(key, CreateModelState(request.Form[key]));
}
}

Expand All @@ -72,7 +73,9 @@ private void PopulateParamsWithQueryStringData(HttpRequestBase request)
foreach (string key in request.QueryString.Keys)
{
var symbolKey = SymbolTable.StringToId(key);
_params[symbolKey] = request.QueryString[key];
var value = request.QueryString[key];
_params[symbolKey] = value;
ModelState.Add(key, CreateModelState(value) );
}
}

Expand All @@ -82,9 +85,20 @@ private void PopulateParamsWithRouteData()
{
var key = SymbolTable.StringToId(item.Key);
_params[key] = item.Value;
// ModelState.Add(item.Key, CreateModelState(item.Value.ToString()));
}
}

private static ModelState CreateModelState(string value)
{
return new ModelState {Value = CreateValueProviderResult(value)};
}

private static ValueProviderResult CreateValueProviderResult(string value)
{
return new ValueProviderResult(value, value, CultureInfo.CurrentCulture);
}

internal void InternalInitialize(ControllerConfiguration config)
{
Initialize(config.Context);
Expand Down Expand Up @@ -145,7 +159,7 @@ public ActionResult RedirectToAction(string actionName, Hash values)
public new ViewResult View(string viewName, string masterName, object model)
{
var vdd = new ViewDataDictionary();
vdd["__scriptRuntime"] = ((RubyEngine) _engine).Runtime;
// vdd["__scriptRuntime"] = ((RubyEngine) _engine).Runtime;

_engine.CallMethod(this, "fill_view_data");
foreach (var entry in _viewData)
Expand Down
26 changes: 15 additions & 11 deletions IronRubyMvc/Core/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@ namespace System.Web.Mvc.IronRuby.Core
{
internal class Constants
{
public const string CONTROLLER_PASCAL_PATH_FORMAT = @"~\Controllers\{0}Controller.rb";
public const string CONTROLLER_UNDERSCORE_PATH_FORMAT = @"~\Controllers\{0}_controller.rb";
public const string CONTROLLERCLASS_FORMAT = "{0}Controller";
public const string CONTROLLERNAME_NAME_REGEX = @"^(\w)+$";
public const string ControllerPascalPathFormat = @"~\Controllers\{0}Controller.rb";
public const string ControllerUnderscorePathFormat = @"~\Controllers\{0}_controller.rb";
public const string ControllerclassFormat = "{0}Controller";
public const string ControllernameNameRegex = @"^(\w)+$";

public const string CONTROLLERS = "Controllers";
public const string Controllers = "Controllers";

public const string FILTERS = "Filters";
public const string FILTERS_PASCAL_PATH_FORMAT = @"~\Filters\{0}.rb";
public const string FILTERS_UNDERSCORE_PATH_FORMAT = @"~\Filters\{0}.rb";
public const string Filters = "Filters";
public const string FiltersPascalPathFormat = @"~\Filters\{0}.rb";
public const string FiltersUnderscorePathFormat = @"~\Filters\{0}.rb";

public const string MODELS = "Models";
public const string RUBYCONTROLLER_FILE = "System.Web.Mvc.IronRuby.Controllers.controller.rb";
public const string SCRIPT_RUNTIME_VARIABLE = "script_runtime";
public const string Models = "Models";
public const string RubycontrollerFile = "System.Web.Mvc.IronRuby.Controllers.controller.rb";
public const string ScriptRuntimeVariable = "script_runtime";

public const string Helpers = "Helpers";
public const string HelpersPascalPathFormat = @"~\Filters\{0}.rb";
public const string HelpersUnderscorePathFormat = @"~\Filters\{0}.rb";
}
}
1 change: 1 addition & 0 deletions IronRubyMvc/Core/IPathProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ public interface IPathProvider
string ApplicationPhysicalPath { get; }
bool FileExists(string filePath);
Stream Open(string filePath);
string MapPath(string filePath);
}
}
15 changes: 15 additions & 0 deletions IronRubyMvc/Core/IRubyEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Web.Mvc.IronRuby.Controllers;
using System.Web.Routing;
using IronRuby.Builtins;
using Microsoft.Scripting.Hosting;

#endregion

Expand Down Expand Up @@ -75,6 +76,14 @@ public interface IRubyEngine
/// <returns></returns>
object ExecuteScript(string script);

/// <summary>
/// Executes the script.
/// </summary>
/// <param name="script">The script.</param>
/// <param name="scope">The scope.</param>
/// <returns></returns>
object ExecuteScript(string script, ScriptScope scope);

/// <summary>
/// Defines the read only global variable.
/// </summary>
Expand Down Expand Up @@ -103,5 +112,11 @@ public interface IRubyEngine
/// </summary>
/// <param name="assemblies">The assemblies.</param>
void LoadAssemblies(params Type[] assemblies);

/// <summary>
/// Executes the block in scope.
/// </summary>
/// <param name="block">The block.</param>
void ExecuteInScope(Action<ScriptScope> block);
}
}
58 changes: 43 additions & 15 deletions IronRubyMvc/Core/RubyEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ public string GetMethodName(object receiver, string message)
return message;
}

public void ExecuteInScope(Action<ScriptScope> block)
{
var scope = Engine.CreateScope();
block(scope);
}

/// <summary>
/// Calls the method.
/// </summary>
Expand Down Expand Up @@ -206,9 +212,25 @@ public void LoadAssembly(Assembly assembly)
Runtime.LoadAssembly(assembly);
}

/// <summary>
/// Executes the script.
/// </summary>
/// <param name="script">The script.</param>
/// <returns></returns>
public object ExecuteScript(string script)
{
return Engine.Execute(script, CurrentScope);
return ExecuteScript(script, CurrentScope);
}

/// <summary>
/// Executes the script.
/// </summary>
/// <param name="script">The script.</param>
/// <param name="scope">The scope.</param>
/// <returns></returns>
public object ExecuteScript(string script, ScriptScope scope)
{
return Engine.Execute(script, scope ?? CurrentScope);
}

/// <summary>
Expand Down Expand Up @@ -264,16 +286,13 @@ private void Initialize()
Operations = Engine.CreateOperations();
LoadAssemblies(typeof (object), typeof (Uri), typeof (HttpResponseBase), typeof(MembershipCreateStatus), typeof (RouteTable), typeof (Controller), typeof (RubyController));
AddLoadPaths();
DefineReadOnlyGlobalVariable(Constants.SCRIPT_RUNTIME_VARIABLE, Engine);
DefineReadOnlyGlobalVariable(Constants.ScriptRuntimeVariable, Engine);
RequireControllerFile();
}

private void RequireControllerFile()
{
// Engine.RequireRubyFile(@"C:\tools\ironruby\ironrubymvc\IronRubyMvc\Controllers\controller.rb");
// RequireRubyFile("~/Controllers/controller.rb", ReaderType.File);
Engine.RequireRubyFile(HostingEnvironment.MapPath("~/Controllers/controller.rb"));
// RequireRubyFile(Constants.RUBYCONTROLLER_FILE, ReaderType.AssemblyResource);
Engine.RequireRubyFile(PathProvider.MapPath("~/Controllers/controller.rb"));
}

/// <summary>
Expand All @@ -285,16 +304,16 @@ internal static string GetControllerClassName(string controllerName)
{
return (controllerName.EndsWith("Controller", StringComparison.OrdinalIgnoreCase)
? controllerName
: Constants.CONTROLLERCLASS_FORMAT.FormattedWith(controllerName)).Pascalize();
: Constants.ControllerclassFormat.FormattedWith(controllerName)).Pascalize();
}

internal string GetControllerFilePath(string controllerName)
{
var fileName = Constants.CONTROLLER_PASCAL_PATH_FORMAT.FormattedWith(controllerName.Pascalize());
var fileName = Constants.ControllerPascalPathFormat.FormattedWith(controllerName.Pascalize());
if (PathProvider.FileExists(fileName))
return fileName;

fileName = Constants.CONTROLLER_UNDERSCORE_PATH_FORMAT.FormattedWith(controllerName.Underscore());
fileName = Constants.ControllerUnderscorePathFormat.FormattedWith(controllerName.Underscore());

return PathProvider.FileExists(fileName) ? fileName : string.Empty;
}
Expand All @@ -316,15 +335,17 @@ internal void RequireRubyFile(string path, ReaderType readerType)
/// </summary>
public void AddLoadPaths()
{
var controllersDir = Path.Combine(PathProvider.ApplicationPhysicalPath, Constants.CONTROLLERS);
var modelsDir = Path.Combine(PathProvider.ApplicationPhysicalPath, Constants.MODELS);
var filtersDir = Path.Combine(PathProvider.ApplicationPhysicalPath, Constants.FILTERS);
Context.Loader.SetLoadPaths(new[] {controllersDir, modelsDir, filtersDir});
var controllersDir = Path.Combine(PathProvider.ApplicationPhysicalPath, Constants.Controllers);
var modelsDir = Path.Combine(PathProvider.ApplicationPhysicalPath, Constants.Models);
var filtersDir = Path.Combine(PathProvider.ApplicationPhysicalPath, Constants.Filters);
var helpersDir = Path.Combine(PathProvider.ApplicationPhysicalPath, Constants.Helpers);

Context.Loader.SetLoadPaths(new[] {controllersDir, modelsDir, filtersDir, helpersDir});
}


/// <summary>
/// Initializes the iron ruby MVC.
/// Initializes ironruby MVC.
/// </summary>
/// <param name="pathProvider">The Path provider.</param>
/// <param name="routesPath">The routes path.</param>
Expand All @@ -333,6 +354,13 @@ public static RubyEngine InitializeIronRubyMvc(IPathProvider pathProvider, strin
return InitializeIronRubyMvc(pathProvider, routesPath, path => new VirtualPathStreamContentProvider(path));
}

/// <summary>
/// Initializes the ironruby MVC.
/// </summary>
/// <param name="pathProvider">The path provider.</param>
/// <param name="routesPath">The routes path.</param>
/// <param name="contentProviderFactory">The content provider factory.</param>
/// <returns></returns>
public static RubyEngine InitializeIronRubyMvc(IPathProvider pathProvider, string routesPath, Func<string, StreamContentProvider> contentProviderFactory)
{
var engine = InitializeIronRuby(pathProvider, contentProviderFactory);
Expand All @@ -345,7 +373,7 @@ private static void IntializeMvc(IRubyEngine engine)
{
var factory = new RubyControllerFactory(ControllerBuilder.Current.GetControllerFactory(), engine);
ControllerBuilder.Current.SetControllerFactory(factory);
ViewEngines.Engines.Add(new RubyViewEngine());
ViewEngines.Engines.Add(new RubyViewEngine(engine));
}

private static RubyEngine InitializeIronRuby(IPathProvider vpp, Func<string, StreamContentProvider> contentProviderFactory)
Expand Down
5 changes: 5 additions & 0 deletions IronRubyMvc/Core/VirtualPathProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public Stream Open(string filePath)
return file.Open();
}

public string MapPath(string filePath)
{
return HostingEnvironment.MapPath(filePath);
}

public string ApplicationPhysicalPath
{
get { return HostingEnvironment.ApplicationPhysicalPath; }
Expand Down
8 changes: 4 additions & 4 deletions IronRubyMvc/ViewEngine/RubyScriptBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ namespace System.Web.Mvc.IronRuby.ViewEngine
{
internal class RubyScriptBlock
{
[ThreadStatic] private static bool ignoreNextNewLine;
[ThreadStatic] private static bool IgnoreNextNewLine;

private RubyScriptBlock(string block)
{
var ignoreNewLine = ignoreNextNewLine;
var ignoreNewLine = IgnoreNextNewLine;

if (String.IsNullOrEmpty(block))
{
Expand All @@ -26,11 +26,11 @@ private RubyScriptBlock(string block)
if (block.EndsWith("-%>", StringComparison.OrdinalIgnoreCase))
{
endOffset = 5;
ignoreNextNewLine = true;
IgnoreNextNewLine = true;
}
else
{
ignoreNextNewLine = false;
IgnoreNextNewLine = false;
}

if (block.StartsWith("<%=", StringComparison.OrdinalIgnoreCase))
Expand Down
Loading

0 comments on commit 6f43a2a

Please sign in to comment.