Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

More refactoring. Replaced script runner with DLR stream content prov…

…ider

Signed-off-by: Jimmy Schementi <jschementi@gmail.com>
  • Loading branch information...
commit 1c96eb5efb9c3800371ef61e91394cc31fdb25bb 1 parent eca88ec
@casualjim casualjim authored committed
View
8 IronRubyMvc/Controllers/controller.rb
@@ -1,4 +1,4 @@
-module IronRubyMvc
+module IronRubyMvc
module Controllers
@@ -45,7 +45,7 @@ def initialize(on_exception)
@on_exception = on_exception
end
- def on_action_executing(context)
+ def on_exception(context)
on_exception.call(context)
end
@@ -60,11 +60,11 @@ def initialize(before_result = nil, after_result=nil)
@after_result = after_result
end
- def on_action_executing(context)
+ def on_result_executing(context)
before_result.call(context) unless before_result.nil?
end
- def on_action_executed(context)
+ def on_result_executed(context)
after_result.call(context) unless after_result.nil?
end
end
View
33 IronRubyMvc/Core/AssemblyResourceReader.cs
@@ -1,33 +0,0 @@
-#region Usings
-
-using System.IO;
-using System.Reflection;
-using System.Web.Mvc.IronRuby.Extensions;
-
-#endregion
-
-namespace System.Web.Mvc.IronRuby.Core
-{
- public class AssemblyResourceReader : Reader
- {
- private readonly Assembly _assembly;
-
- public AssemblyResourceReader(Assembly assembly)
- {
- _assembly = assembly;
- }
-
-// public AssemblyResourceReader() : this(typeof (AssemblyResourceReader).Assembly)
-// {
-// }
-
- public override string Read(string filePath)
- {
- using (var stream = _assembly.GetManifestResourceStream(filePath))
- if (stream.IsNotNull())
- using (var reader = new StreamReader(stream))
- return reader.ReadToEnd();
- return string.Empty;
- }
- }
-}
View
27 IronRubyMvc/Core/AssemblyStreamContentProvider.cs
@@ -0,0 +1,27 @@
+using System.IO;
+using System.Reflection;
+using Microsoft.Scripting;
+
+namespace System.Web.Mvc.IronRuby.Core
+{
+ public class AssemblyStreamContentProvider : StreamContentProvider
+ {
+ private readonly string _fileName;
+ private readonly Assembly _assembly;
+
+ public AssemblyStreamContentProvider(string fileName, Assembly assembly)
+ {
+ _fileName = fileName;
+ _assembly = assembly;
+ }
+
+ #region Overrides of StreamContentProvider
+
+ public override Stream GetStream()
+ {
+ return _assembly.GetManifestResourceStream(_fileName);
+ }
+
+ #endregion
+ }
+}
View
2  IronRubyMvc/Core/Constants.cs
@@ -2,7 +2,6 @@ namespace System.Web.Mvc.IronRuby.Core
{
internal class Constants
{
- public const string ACTION_NAME_REGEX = @"^(\w)+$";
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";
@@ -15,7 +14,6 @@ internal class Constants
public const string FILTERS_UNDERSCORE_PATH_FORMAT = @"~\Filters\{0}.rb";
public const string MODELS = "Models";
- public const string REQUEST_CONTEXT_VARIABLE = "request_context";
public const string RUBYCONTROLLER_FILE = "System.Web.Mvc.IronRuby.Controllers.controller.rb";
public const string SCRIPT_RUNTIME_VARIABLE = "script_runtime";
}
View
52 IronRubyMvc/Core/DefaultScriptRunner.cs
@@ -1,52 +0,0 @@
-#region Usings
-
-using System.Web.Mvc.IronRuby.Extensions;
-using Microsoft.Scripting.Hosting;
-
-#endregion
-
-namespace System.Web.Mvc.IronRuby.Core
-{
- public class DefaultScriptRunner : IScriptRunner
- {
- private readonly ScriptEngine _engine;
-
-
- public DefaultScriptRunner(ScriptEngine engine, string scriptPath, IReader reader)
- {
- _engine = engine;
- ScriptPath = scriptPath;
- Reader = reader;
- }
-
- #region IScriptRunner Members
-
- public string ScriptPath { get; private set; }
-
- public IReader Reader { get; private set; }
-
- public virtual object Execute()
- {
- if (ScriptPath.IsNullOrBlank())
- throw new NullReferenceException("You need to specify a ScriptPath in order to execute a script.");
-
- if (Reader.IsNull())
- throw new NullReferenceException("You need to provice a Reader in order to execute a script");
-
- return ExecuteScript(Reader.Read(ScriptPath));
- }
-
- public virtual object ExecuteFile(string scriptPath)
- {
- ScriptPath = scriptPath;
- return Execute();
- }
-
- public virtual object ExecuteScript(string script)
- {
- return _engine.Execute(script);
- }
-
- #endregion
- }
-}
View
34 IronRubyMvc/Core/FileReader.cs
@@ -1,34 +0,0 @@
-#region Usings
-
-using System.IO;
-
-#endregion
-
-namespace System.Web.Mvc.IronRuby.Core
-{
- public class FileReader : Reader
- {
- private readonly IPathProvider _pathProvider;
-
- public FileReader(IPathProvider pathProvider)
- {
- _pathProvider = pathProvider;
- }
-
- public override string Read(string filePath)
- {
- if (!_pathProvider.FileExists(filePath))
- {
- return string.Empty;
- }
-
- using (var stream = _pathProvider.Open(filePath))
- {
- using (TextReader reader = new StreamReader(stream))
- {
- return reader.ReadToEnd();
- }
- }
- }
- }
-}
View
7 IronRubyMvc/Core/IReader.cs
@@ -1,7 +0,0 @@
-namespace System.Web.Mvc.IronRuby.Core
-{
- public interface IReader
- {
- string Read(string filePath);
- }
-}
View
11 IronRubyMvc/Core/IScriptRunner.cs
@@ -1,11 +0,0 @@
-namespace System.Web.Mvc.IronRuby.Core
-{
- public interface IScriptRunner
- {
- string ScriptPath { get; }
- IReader Reader { get; }
- object Execute();
- object ExecuteFile(string scriptPath);
- object ExecuteScript(string script);
- }
-}
View
11 IronRubyMvc/Core/Reader.cs
@@ -1,11 +0,0 @@
-namespace System.Web.Mvc.IronRuby.Core
-{
- public abstract class Reader : IReader
- {
- #region IReader Members
-
- public abstract string Read(string filePath);
-
- #endregion
- }
-}
View
33 IronRubyMvc/Core/RubyEngine.cs
@@ -3,6 +3,7 @@
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Reflection;
+using System.Text;
using System.Web.Mvc.IronRuby.Controllers;
using System.Web.Mvc.IronRuby.Extensions;
using System.Web.Mvc.IronRuby.ViewEngine;
@@ -10,6 +11,7 @@
using IronRuby;
using IronRuby.Builtins;
using IronRuby.Runtime;
+using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
#endregion
@@ -53,12 +55,6 @@ public RubyEngine(ScriptRuntime runtime, IPathProvider pathProvider)
internal ScriptEngine Engine { get; private set; }
/// <summary>
- /// Gets the script runner.
- /// </summary>
- /// <value>The script runner.</value>
- internal IScriptRunner ScriptRunner { get; set; }
-
- /// <summary>
/// Gets the current scope.
/// </summary>
/// <value>The current scope.</value>
@@ -95,7 +91,7 @@ public RubyController LoadController(RequestContext requestContext, string contr
if (controllerFilePath.IsNullOrBlank())
return null;
- ScriptRunner.ExecuteFile(controllerFilePath);
+ Engine.CreateScriptSource(new VirtualPathStreamContentProvider(controllerFilePath), null, Encoding.UTF8).Execute(CurrentScope);
var controllerClass = GetRubyClass(controllerClassName);
var controller = ConfigureController(controllerClass, requestContext);
@@ -173,7 +169,7 @@ public void LoadAssembly(Assembly assembly)
public object ExecuteScript(string script)
{
- return ScriptRunner.ExecuteScript(script);
+ return Engine.Execute(script, CurrentScope);
}
/// <summary>
@@ -282,7 +278,6 @@ private void Initialize()
Context = Ruby.GetExecutionContext(Engine);
CurrentScope = Engine.CreateScope();
Operations = Engine.CreateOperations();
- ScriptRunner = new ScopedScriptRunner(Engine, CurrentScope, string.Empty, new FileReader(PathProvider));
LoadAssemblies(typeof (object), typeof (Uri), typeof (HttpResponseBase), typeof (RouteTable), typeof (Controller), typeof (RubyController));
AddLoadPaths();
DefineReadOnlyGlobalVariable(Constants.SCRIPT_RUNTIME_VARIABLE, Engine);
@@ -317,17 +312,6 @@ internal string GetControllerFilePath(string controllerName)
return PathProvider.FileExists(fileName) ? fileName : string.Empty;
}
- internal string GetFilterFilePath(string filterName)
- {
- var fileName = Constants.FILTERS_PASCAL_PATH_FORMAT.FormattedWith(filterName.Pascalize());
- if (PathProvider.FileExists(fileName))
- return fileName;
-
- fileName = Constants.FILTERS_UNDERSCORE_PATH_FORMAT.FormattedWith(filterName.Underscore());
-
- return PathProvider.FileExists(fileName) ? fileName : string.Empty;
- }
-
/// <summary>
/// Requires the ruby file.
/// </summary>
@@ -335,11 +319,10 @@ internal string GetFilterFilePath(string filterName)
/// <param name="readerType">Type of the reader.</param>
internal void RequireRubyFile(string path, ReaderType readerType)
{
- new DefaultScriptRunner(
- Engine,
- path,
- readerType == ReaderType.File ? new FileReader(PathProvider) : (IReader) new AssemblyResourceReader(typeof (IReader).Assembly)
- ).Execute();
+ Engine.CreateScriptSource(readerType == ReaderType.File
+ ? (StreamContentProvider) new VirtualPathStreamContentProvider(path)
+ : new AssemblyStreamContentProvider(path, typeof (IRubyEngine).Assembly), null, Encoding.ASCII).Execute();
+
}
/// <summary>
View
54 IronRubyMvc/Core/ScopedScriptRunner.cs
@@ -1,54 +0,0 @@
-#region Usings
-
-using System.Web.Mvc.IronRuby.Extensions;
-using Microsoft.Scripting.Hosting;
-
-#endregion
-
-namespace System.Web.Mvc.IronRuby.Core
-{
- public class ScopedScriptRunner : IScriptRunner
- {
- private readonly ScriptEngine _engine;
- private readonly ScriptScope _scope;
-
-
- public ScopedScriptRunner(ScriptEngine engine, ScriptScope scope, string scriptPath, IReader reader)
- {
- _engine = engine;
- _scope = scope;
- ScriptPath = scriptPath;
- Reader = reader;
- }
-
- #region IScriptRunner Members
-
- public string ScriptPath { get; private set; }
-
- public IReader Reader { get; private set; }
-
- public virtual object Execute()
- {
- if (ScriptPath.IsNullOrBlank())
- throw new NullReferenceException("You need to specify a ScriptPath in order to execute a script.");
-
- if (Reader.IsNull())
- throw new NullReferenceException("You need to provice a Reader in order to execute a script");
-
- return ExecuteScript(Reader.Read(ScriptPath));
- }
-
- public virtual object ExecuteFile(string scriptPath)
- {
- ScriptPath = scriptPath;
- return Execute();
- }
-
- public virtual object ExecuteScript(string script)
- {
- return _engine.Execute(script, _scope);
- }
-
- #endregion
- }
-}
View
25 IronRubyMvc/Core/VirtualPathStreamContentProvider.cs
@@ -0,0 +1,25 @@
+using System.IO;
+using System.Web.Hosting;
+using Microsoft.Scripting;
+
+namespace System.Web.Mvc.IronRuby.Core
+{
+ public class VirtualPathStreamContentProvider : StreamContentProvider
+ {
+ private readonly string _fileName;
+
+ public VirtualPathStreamContentProvider(string fileName)
+ {
+ _fileName = fileName;
+ }
+
+ #region Overrides of StreamContentProvider
+
+ public override Stream GetStream()
+ {
+ return HostingEnvironment.VirtualPathProvider.GetFile(_fileName).Open();
+ }
+
+ #endregion
+ }
+}
View
14 IronRubyMvc/System.Web.Mvc.IronRuby.csproj
@@ -82,23 +82,17 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Controllers\ControllerConfiguration.cs" />
+ <Compile Include="Core\AssemblyStreamContentProvider.cs" />
+ <Compile Include="Core\IPathProvider.cs" />
<Compile Include="Core\IRubyEngine.cs" />
<Compile Include="Controllers\RubyActionFilter.cs" />
<Compile Include="Controllers\RubyAuthorizationFilter.cs" />
<Compile Include="Controllers\RubyExceptionFilter.cs" />
<Compile Include="Controllers\RubyResultFilter.cs" />
- <Compile Include="Core\DefaultScriptRunner.cs" />
- <Compile Include="Core\AssemblyResourceReader.cs" />
<Compile Include="Core\Constants.cs" />
<Compile Include="Core\Enums.cs" />
- <Compile Include="Core\FileReader.cs" />
- <Compile Include="Core\IPathProvider.cs" />
- <Compile Include="Core\IReader.cs" />
- <Compile Include="Core\Reader.cs" />
<Compile Include="Core\RubyMvcApplication.cs" />
<Compile Include="Core\RubyRouteCollection.cs" />
- <Compile Include="Core\ScopedScriptRunner.cs" />
- <Compile Include="Core\IScriptRunner.cs" />
<Compile Include="Controllers\RubyActionDescriptor.cs" />
<Compile Include="Controllers\RubyController.cs" />
<Compile Include="Controllers\RubyControllerActionInvoker.cs" />
@@ -131,12 +125,10 @@
</Compile>
</ItemGroup>
<ItemGroup>
- <EmbeddedResource Include="Controllers\controller.rb" />
- </ItemGroup>
- <ItemGroup>
<None Include="..\IronRubyMvcKey.snk">
<Link>IronRubyMvcKey.snk</Link>
</None>
+ <EmbeddedResource Include="Controllers\controller.rb" />
</ItemGroup>
<ItemGroup>
<CodeAnalysisDictionary Include="CustomDictionary.xml" />
View
2  IronRubyMvcWeb/Controllers/HomeController.rb
@@ -1,4 +1,4 @@
-#require "HomeModel"
+#require "HomeModel"
require 'MyFilter'
View
2  IronRubyMvcWeb/Controllers/ProductsController.rb
@@ -1,4 +1,4 @@
-#require 'helpers/BaseController'
+#require 'helpers/BaseController'
#require 'ProductsRepository'
require 'IronRubyMvcWeb, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'
View
2  IronRubyMvcWeb/Routes.rb
@@ -1 +1 @@
-$routes.map_route("default", "{controller}/{action}/{id}", {:controller => 'Home', :action => 'index', :id => ''})
+$routes.map_route("default", "{controller}/{action}/{id}", {:controller => 'Home', :action => 'index', :id => ''})
Please sign in to comment.
Something went wrong with that request. Please try again.