Running one lym from another?

edited February 2015 in Ruby Scripting

Hi,

I understand .lym is intended to provide meta-information along with the script itself, is XML format and therefore cannot be run using ruby's 'load'.

However there are cases where running (i.e. ruby's "load" or "require") one .lym from another .lym would be useful. One example is if I create a toolbar button in script:

a = RBA::Action.new
a.title = "Title"

Then of course when it is pressed you normally do this:

a.on_triggered { # CODE GOES HERE }

But what if "#CODE GOES HERE" is very long code, and you have many buttons, so it is unweildy (and bad organization) to paste all these on_triggered actions in one macro. Well, if it's an .rb then you can do:

a.on_triggered { load "file.rb" }

but if it's a .lym then you are stuck.

I thought about writing a script to parse the .lym XML and return the heart of it as a long string. But first wanted to ask if there is perhaps an easier way to run one .lym file from another .lym file? Or if there is a good reason for not allowing this?

Thanks,
David

Comments

  • edited March 2015

    Hm, turned out to not be too hard.. Paste this into a .lym file and "Run on startup". I also included ability to run .lydrc.

    # Make it possible to load .lym and .lydrc files
    module Kernel
      def load_lym(file)
        require 'rexml/document'  
        doc = REXML::Document.new(File.new(file))
        str = REXML::XPath.first(doc,"//text").text
        eval(str)
      end
      def load_lydrc(file)
        require 'rexml/document'  
        doc = REXML::Document.new(File.new(file))
        str = REXML::XPath.first(doc,"//text").text
        engine = DRC::DRCEngine::new
        engine.instance_eval(str, '')
      end
    end
    

    Then you just use load_lym or load_lydrc in other files where you would otherwise use load.

    Not sure if it's good practice to add methods to Kernel but it seems to work..

  • edited March 2015

    Hi David,

    I'd suggest to put the core code into a normal .rb file and load or require this file from your .lym's.

    The lym files are intended to be top level files. Here the meta information is required, but not for the files you include. The meta information is used to provide the menu binding, the language used and to configure a macro for automatic running. BTW: you can configure plain ruby files too, but then the text will be enhanced with ugly comments which you must not delete.

    You can create plain .rb files from IDE too and the debugger will work properly with them as well. In order to include them you can either use "load" or "required". "load" is better for debugging, since the file will be loaded every time the macro is executed. "require" is better for deployed applications since the loading time is reduced.

    Here is a sample:

    # --------------------------------------------------
    # plain_file.rb:
    
    def core_function(m)
      RBA::MessageBox::info("Info", "This is the message: " + m, RBA::MessageBox::Ok)
    end
    
    # --------------------------------------------------
    # macro1.lym:
    
    dir = File.dirname($0)
    $:.find(dir) || $:.push(dir)
    
    load "plain_file.rb"
    # or: require "plain_file"
    
    core_function("Message 1")
    
    # --------------------------------------------------
    # macro2.lym:
    
    dir = File.dirname($0)
    $:.find(dir) || $:.push(dir)
    
    load "plain_file.rb"
    # or: require "plain_file"
    
    core_function("Message 2")
    

    Please note the trick about the search path ($:): this will make "load" and "require" look up the plain file relative to the lym files. The "find" will make sure the path is not expanded every time the macro is run.

    Best regards,

    Matthias

  • edited November -1

    Thanks Matthias,

    Yes that is the way I used to do it. I just have certain use cases when it's advantageous for .lym to be on equal footing as .rb, when it comes to the ability to 'load' or 'require'. Anyway I suppose either method works.

    Thanks for your comments, and nice trick with the $:! :-)

    Thanks,
    David

Sign In or Register to comment.