Beispiel #1
0
        public void TestTypeAndTracker(object type)
        {
            Context.DefineGlobalVariable("type", type);
            AssertOutput(delegate() {
                CompilerTest(@"
puts $type
puts $type.to_s
puts $type.to_class
puts $type.to_module
puts $type.to_class.static_method
puts $type.static_method rescue puts $!.class
");
            }, String.Format(@"
#<{0}:0x*>
#<{0}:0x*>
{1}
{1}
2
NoMethodError
", RubyUtils.GetQualifiedName(type.GetType()), RubyUtils.GetQualifiedName(typeof(ClassWithMethods1))), OutputFlags.Match);
        }
        public string BuildScriptHeader(Assembly languageFactoryAssembly)
        {
            // scan for extension types to monkey patch - ironruby doesn't have the
            // automagic wire-up implemented yet

            var extensionMethods = new Dictionary <MethodInfo, Type>();

            var assemblyExtensionAttrs = languageFactoryAssembly.GetCustomAttributes(typeof(ExtensionTypeAttribute), true);

            foreach (var assemblyExtension in assemblyExtensionAttrs.Cast <ExtensionTypeAttribute>())
            {
                foreach (var methodInfo in assemblyExtension.ExtensionType.GetMethods(BindingFlags.Public | BindingFlags.Static | BindingFlags.InvokeMethod))
                {
                    var methodExtensionAttrs = methodInfo.GetCustomAttributes(typeof(ExtensionAttribute), true);
                    if (methodExtensionAttrs == null || methodExtensionAttrs.Length == 0)
                    {
                        continue;
                    }

                    extensionMethods.Add(methodInfo, assemblyExtension.ExtensionType);
                }
            }

            var builder = new StringBuilder();

            var htmlEntries = extensionMethods
                              .Where(kv => kv.Key.GetParameters()[0].ParameterType == typeof(HtmlHelper))
                              .Select(kv => ":" + kv.Key.Name + "=>" + RubyUtils.GetQualifiedName(kv.Value))
                              .Distinct();

            var ajaxEntries = extensionMethods
                              .Where(kv => kv.Key.GetParameters()[0].ParameterType == typeof(AjaxHelper))
                              .Select(kv => ":" + kv.Key.Name + "=>" + RubyUtils.GetQualifiedName(kv.Value))
                              .Distinct();

            builder.AppendLine("$htmlExtensionMethods = {");
            builder.AppendLine(string.Join(",\r\n", htmlEntries.ToArray()));
            builder.AppendLine("}");
            builder.AppendLine("$ajaxExtensionMethods = {");
            builder.AppendLine(string.Join(",\r\n", ajaxEntries.ToArray()));
            builder.AppendLine("}");

            builder.AppendLine(@"
view_html = view.html
def view_html.method_missing(name, *parameters)
 extensionObject = $htmlExtensionMethods[name]
 if (extensionObject != nil)
   extensionObject.send(name, self, *parameters)
 else
   System::Web::Mvc::HtmlHelper.send(name, *parameters)
 end
end
view_ajax = view.ajax
def view_ajax.method_missing(name, *parameters)
 extensionObject = $ajaxExtensionMethods[name]
 if (extensionObject != nil)
   extensionObject.send(name, self, *parameters)
 else
   System::Web::Mvc::AjaxHelper.send(name, *parameters)
 end
end
");
            return(builder.ToString());
        }