Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Ove Andersen 435 posts 1541 karma points c-trib
    Oct 19, 2010 @ 10:58
    Ove Andersen
    0

    IronRuby and Umbraco

    Has anyone successfully managed to create a Node object in a IronRuby script?

    I know it works in Python by doing "x = Node(-1)", but I cannot get it to work in Ruby. It just gives the following error:

    Error loading python script (file: RubyTest.rb, Type: ''
    undefined method `Node' for main:Object

     

  • jaygreasley 416 posts 403 karma points
    Oct 19, 2010 @ 13:27
    jaygreasley
    0

    Hi there,

    the first thing that jumps out at me is

    Error loading python script

    Is it definitely being executed by IronRuby ?

    J

  • Ove Andersen 435 posts 1541 karma points c-trib
    Oct 19, 2010 @ 13:51
    Ove Andersen
    0

    Yes. Here is the full stacktrace:

    Error loading python script (file: RubyTest.rb, Type: ''
    undefined method `Node' for main:Object
      at Microsoft.Scripting.Interpreter.ThrowInstruction.Run(InterpretedFrame frame)
      at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
      at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1)
      at IronRuby.Runtime.RubyScriptCode.Run(Scope scope, Boolean bindGlobals)
      at umbraco.scripting.MacroScriptEngine.Execute()
      at umbraco.scripting.MacroScriptEngine.ExecuteFile(String path)
      at umbraco.scripting.MacroScript.ExecuteFile(String path, Hashtable variables)
      at umbraco.macro.loadMacroDLR(macro macro, Hashtable attributes, Hashtable pageElements)
      at umbraco.macro.renderMacro(Hashtable attributes, Hashtable pageElements, Int32 pageId)
  • Aaron Powell 1708 posts 3046 karma points c-trib
    Oct 19, 2010 @ 23:59
    Aaron Powell
    1

    @Jay - I think that's just cuz the macro engines error messages aren't very smart :P

    @Ove - Have you got an include for the NodeFactory API in your file? Can you post your whole file?

  • Ove Andersen 435 posts 1541 karma points c-trib
    Oct 20, 2010 @ 09:32
    Ove Andersen
    0

    Here you go:

    require "umbraco, Version=1.0.3891.20719, Culture=neutral, PublicKeyToken=null"

    parent = Node(-1)

    result = "<ul>";

    parent.Children.each do |node|
      result += "<li><a href='" + node.NiceUrl + "'>" + node.Name + "</a></li>"
    end

    result += "</ul>"
  • Aaron Powell 1708 posts 3046 karma points c-trib
    Oct 20, 2010 @ 11:22
    Aaron Powell
    1

    You might need to do a specific namespace import, cuz Node is at umbraco.presentation.nodeFactory.

    I know you have to do that with IronPython (which I use so I'm not the best person for IronRuby help :P)

  • Ove Andersen 435 posts 1541 karma points c-trib
    Oct 20, 2010 @ 11:58
    Ove Andersen
    0

    Tried adding the following line below "require ..." , but it didn't work.

    include umbraco::presentation::nodeFactory

    Here is the new error message:

    Error loading python script (file: RubyTest.rb, Type: ''
    undefined method `umbraco' for main:Object
      at Microsoft.Scripting.Interpreter.ThrowInstruction.Run(InterpretedFrame frame)
      at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
      at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1)
      at IronRuby.Runtime.RubyScriptCode.Run(Scope scope, Boolean bindGlobals)
      at umbraco.scripting.MacroScriptEngine.Execute()
      at umbraco.scripting.MacroScriptEngine.ExecuteFile(String path)
      at umbraco.scripting.MacroScript.ExecuteFile(String path, Hashtable variables)
      at umbraco.macro.loadMacroDLR(macro macro, Hashtable attributes, Hashtable pageElements)
      at umbraco.macro.renderMacro(Hashtable attributes, Hashtable pageElements, Int32 pageId)
  • Ove Andersen 435 posts 1541 karma points c-trib
    Oct 20, 2010 @ 12:04
    Ove Andersen
    0

    Ok. Found the following quote from Scott Hanselman:

    "The naming integration with .NET is persnickety at best. The C# Wesabe client had a lowercase class called "wesaberest" that IronRuby just couldn't see, so I changed it to uppercase and aliased it with a constant."

    The post is from summer 2007, but could it be that the bug still exists?

  • Ove Andersen 435 posts 1541 karma points c-trib
    Oct 20, 2010 @ 12:59
    Ove Andersen
    1

    With the above knowledge in mind, I found the following article: http://ironshay.com/post/Working-with-NET-Lowercase-Namespaces-and-Classes-in-IronRuby.axsp

    And I got it working!

    Umbraco = Object.const_get("umbraco")
    Library = Umbraco.const_get("library")
    NodeFactory = Umbraco.const_get("presentation").const_get("nodeFactory")

    # Get source node from parameter
    parent = NodeFactory::Node.new(System::Int32.Parse(source))

    # Start writing out the list
    result = "<ul>"

    parent.Children.each do |child|
      result += "<li><a href='" + Library.NiceUrl(child.Id) + "'>" + child.Name + "</a></li>"
    end

    result += "</ul>"

    puts result

    EDIT: It seems the "require ... " line was not needed,  so I removed it.

  • Jonas Eriksson 930 posts 1825 karma points
    Oct 20, 2010 @ 14:58
    Jonas Eriksson
    0

    Fun!

    I intended to do some ruby-labs, here's a couple of things I thought of experimenting with. Maybe you have already?

    How does importing ruby libraries work? Is that well handled by the macro engine? (For Python it isnt because the path to /python arent setup as it should).

    How do you sort and filter an array like that in Ruby? find_all sort_by ?

    I read some about built in template system, which seems very useful: http://freshmeat.net/articles/templates-in-ruby

    # The template.  Notice the single quotes around this string to delay
    # evaluation.
    
    template = 'Dear #{customer},\nPlease pay us #{amount} now.\n'
    
    # The "replacement" values.
    
    customer = "Joe Schmoe"
    amount = sprintf( "$%0.2f", 250 )
    
    # Evaluate the string using eval, which will use the "replacement" values
    # from the local context.
    
    print eval( '"' + template + '"' )
    
  • Jonas Eriksson 930 posts 1825 karma points
    Oct 21, 2010 @ 15:51
    Jonas Eriksson
    1

    Cool, importing a class-file worked almost oob. Just needed to add a path to the python-folder in \config\scripting.config (should come with the installation imho, will add that to codeplex):

    <options>
    <set language="Ruby" option="LibraryPaths" value="python\;...
    </options>   

    test.rb:

    class Test
      def initialize(firstname)
        @firstname=firstname
      end
      def putsfirstname
        puts @firstname
      end
    end

    test2.rb:

    require 'test'
    m = Test.new("test")

    I never played with Ruby before. Interesting that it requires some conventions, like capitalizing class-names, otherwise it wont save.

  • Jonas Eriksson 930 posts 1825 karma points
    Oct 21, 2010 @ 16:15
    Jonas Eriksson
    1

    Playtime continues. Filter with filter_by and sorting with sort_by works fine:

    parent.Children.find_all{|f| f.NodeTypeAlias=="mydoctype" }.sort_by{|s|s.name}.each do |child|
      result += "<li><a href='" + Library.NiceUrl(child.Id) + "'>" + child.Name + "</a></li>"
    end
  • Ove Andersen 435 posts 1541 karma points c-trib
    Oct 21, 2010 @ 16:31
    Ove Andersen
    0

    Sweet! Here is one that filters out hidden nodes:

    parent.Children.find_all{|f| f.GetProperty("umbracoNaviHide")!="1"}.each do |child|
      result += "<li><a href='" + Library.NiceUrl(child.Id) + "'>" + child.Name + "</a></li>"
    end

     

  • Jonas Eriksson 930 posts 1825 karma points
    Oct 21, 2010 @ 16:39
    Jonas Eriksson
    0

    Starting to like Ruby :) But needs .Value + nil-check, right? does it work like python?:

    parent.Children.find_all{|f| f.GetProperty("umbracoNaviHide") && f.GetProperty("umbracoNaviHide").Value != "1" }.sort_by{|s|s.name}.each do |child|

     

  • Jonas Eriksson 930 posts 1825 karma points
    Oct 22, 2010 @ 12:48
    Jonas Eriksson
    0

    Using Ruby standard templating engine ERB it's possible to create stuff like:

    require "erb"
      # Create ERB-template
      template = %{
          <h1><%= currentPage.name%></h1>
      <ul>
      <% currentPage.Children.each do |c| %>
               <li><%= c.name %></li>
            <% end %>
    </ul>
      }.gsub(/^  /, '')
      rhtml = ERB.new(template)
      rhtml.run(binding)

    The Umbraco macro-engine gracefully gives us the currentPage object. ERB included in Ruby standard libraries and is necessary to download and include in the Ruby configuration file. But thats done i 5 minutes.

  • Jonas Eriksson 930 posts 1825 karma points
    Oct 22, 2010 @ 13:02
    Jonas Eriksson
    0

    And here's a recursive (multi-level) example:

    require "erb"
    Umbraco = Object.const_get("umbraco")
    Library = Umbraco.const_get("library")
      # Create ERB-template
      template = %{
           <% def PrintChildrenRecursively(n); _erbout = ''; %>
               <% if n.Children.Count>0 %>
               <ul>
               <% n.Children.each do |c| %>
                 <li><%= c.name %>
                 <%= PrintChildrenRecursively(c) %></li>            
               <% end %>
               </ul>
               <% end %>
             <% end %>
     
    <h1><%= currentPage.name%></h1>
    <%= PrintChildrenRecursively(currentPage) %>
      }.gsub(/^  /, '')
      rhtml = ERB.new(template)
      rhtml.run(binding)
     

    changing <%= PrintChildrenRecursively(currentPage) %> to call with a root node we have a full sitemap.

  • Jonas Eriksson 930 posts 1825 karma points
    Oct 22, 2010 @ 13:12
    Jonas Eriksson
    0

    Small note about getting ERB to run:

    The Ruby-libraries are here : http://www.ruby-lang.org/en/downloads/

    Get version 1.8.7 (IronRuby targets 1.8x). Extract it to somewhere, like c:\Ruby187 and change /config/scripting.config so that the librarypaths setting has the right paths:

    <microsoft.scripting>
    ...
        <options>
          <set language="Ruby" option="LibraryPaths" value="python\;C:\Ruby187\lib\ruby\site_ruby\1.8\;C:\Ruby187\lib\ruby\1.8\;" />
        </options>
    </microsoft.scripting>
  • Jonas Eriksson 930 posts 1825 karma points
    Jan 22, 2011 @ 08:39
    Jonas Eriksson
    0

    I jump into this again as my "ERB"-import advide could be misleading for some - there's no real need for it as I see %Q is a built in nice way to mix html with code:

     www.aaron-powell.com/umbraco-menu-with-ironruby

    see also this thread for more %Q samples

    http://our.umbraco.org/forum/developers/extending-umbraco/16412-IronRuby,-Umbraco-461,-and-Library-methods

    Kudos to Slace, Chriztian and Ted

Please Sign in or register to post replies

Write your reply to:

Draft