[Code] custom file_loaded() and file_loaded?() v2.0.0

[Code] custom file_loaded() and file_loaded?() v2.0.0

Postby Dan Rathbun » Thu Jun 21, 2012 12:11 am

Anton_S in another topic wrote:
Dan Rathbun wrote:
Code: Select all
unless file_loaded?(File.basename(__FILE__))
...
file_loaded(File.basename(__FILE__))

I recommend putting the whole __FILE__ instead of a File.basename(__FILE__). That way you would eliminate same filenames that are in different folders.


Actually.. the $loaded_files array, is nothing more than an array of strings. It does NOT matter whether the strings are pathnames to existing files, or non-existing files, or just some strings representing unique "already loaded flags".

thomthom in the same topic wrote:Just using the filename is not a unique ID - full path is.

It doesn't even have to be a filename you feed file_loaded - just any unique string.


What I mean is that the file_loaded() and file_loaded?() methods, do not do any testing to determine if the argument is a valid pathname.

SO

  • to save space (memory)
  • to be sure that the "loaded keys" are unique
  • to side-step the fact that __FILE__ does not work inside scrambled rubies

I have begun to set my "already loaded" arguments to a qualified namespace string, like:

unless file_loaded?("MyTopModule::ThisPluginModule:filename_without_extension")
I do not put the file extension as they may distro' as .rb or .rbs and it's not important anyway.

So, let's say my namespace is Boozer, and the plugin is Gadget, and the file sets up menu commands:

unless file_loaded?("Boozer::Gadget:menusetup")

Noone else is using my namespace, so the string should be unique. Even for me it should be unique, because I include the plugin name, and files only ever load once (in normal usage. ie, not debugging.)

So to use your example, the following strings would be unique:
"Anton::Lib:core"
"TT::Lib:core"
and
"TT::Lib2:core"



Furthermore... nothing really says that we MUST use the global $loaded_files array. (It's an invention of the SU DevTeam.)

If you are coding within your own unique toplevel "Author"/"Company" namespace... you can define your own "already loaded" string tag array, as a module variable, named whatever you like.
Then define getter and setter methods (or copy & modify the two from "sketchup.rb",) into your toplevel module.

Here's a template (now tested, and updated to v2.0.0) to do this, as a mixin module:
LoadUtil.rb


:idea:
0
Last edited by Dan Rathbun on Thu Jun 28, 2012 7:18 am, edited 1 time in total.
    I'm not here much anymore. But a PM will fire email notifications.
    User avatar
    Dan Rathbun 
    PluginStore Author
    PluginStore Author
     

    Re: [Code] custom file_loaded() and file_loaded?()

    Postby Dan Rathbun » Thu Jun 21, 2012 4:47 am

    Ok I guess I need to explain how to use a mixin module, using the global include() method.

    In the example below (and in the template,) you change the word "Author", to YOUR toplevel module name.

    This LoadUtil module is written FOR PRIVATE USE. append_features will not let it be included into any one else's namespace.
    It makes Author::LoadUtil a private module !!

    YOU use it by including it in your sub-modules.

    Code: Select all
    # file: "Author/NiftyPlugin/Setup.rb"
    module Author::NiftyPlugin

      require("Author/LoadUtil")
      include(Author::LoadUtil)

      unless file_loaded?("NiftyPlugin:Setup")
        # do code to setup menus
        # do code to setup toolbars
        file_loaded("NiftyPlugin:Setup")
      end

    end

    In the above code sample, the file_loaded and file_loaded? that get called, are the ones you mixed in (which LOCALLY overrode the global ones, that are defined in "Tools/sketchup.rb")

    You have your OWN @@loaded_files array, up in module Author, that you keep all to yourself, and there will never be any clashes caused by OTHER people's scripts. (Now you can make a mistake and cause a clash, but you can fix it, without having to get someone else to change their code.)
    0
      I'm not here much anymore. But a PM will fire email notifications.
      User avatar
      Dan Rathbun 
      PluginStore Author
      PluginStore Author
       

      Re: [Code] custom file_loaded() and file_loaded?()

      Postby thomthom » Thu Jun 21, 2012 8:12 am

      :thumb: :thumb:
      0
      Thomas Thomassen — SketchUp Monkey & Coding addict
      List of my plugins and link to the CookieWare fund
      User avatar
      thomthom 
      PluginStore Author
      PluginStore Author
       

      Re: [Code] custom file_loaded() and file_loaded?()

      Postby Jim » Sun Jun 24, 2012 11:59 am

      The only purpose of file_loaded appears to be as "menu guards" that prevent duplicate menu items from being created if the file is loaded more than once. This is helpful when developing a plugin, but what is the point in a released plugin? Better to not add elements to either a global $loaded_files or a namespaced equivalent.
      0
      Hi

      Jim 
      Global Moderator
       

      Re: [Code] custom file_loaded() and file_loaded?()

      Postby Dan Rathbun » Sun Jun 24, 2012 10:35 pm

      Sometimes it's easier to just "go with the flow.."

      Keeping two editions (a debug, and a release,) for every script and plugin just confuses things.

      Also there has been historical "bugs" and "quirks" in the way the Ruby's require worked. Remember that until SU 8.x, everyone was still running on the PC with 1.8.0, which had a require that works a bit differently than it does in later versions.

      So.. a historical part of this issues, was also dealing with require's wanting to load the same file more than once.
      0
        I'm not here much anymore. But a PM will fire email notifications.
        User avatar
        Dan Rathbun 
        PluginStore Author
        PluginStore Author
         

        Re: [Code] custom file_loaded() and file_loaded?()

        Postby Dan Rathbun » Sun Jun 24, 2012 10:38 pm

        Now.. that said.. yes there are alternatives.

        Sometimes I use defined?

        If the module var is going to be @@menu, then I use:

        Code: Select all
        unless defined?(@@menu)
          @@menu=UI.menu('Plugins')
          # define items, etc
        end
        0
        Last edited by Dan Rathbun on Thu Jun 28, 2012 7:23 am, edited 1 time in total.
          I'm not here much anymore. But a PM will fire email notifications.
          User avatar
          Dan Rathbun 
          PluginStore Author
          PluginStore Author
           

          Re: [Code] custom file_loaded() and file_loaded?()

          Postby Anton_S » Thu Jun 28, 2012 12:20 am

          Here is jst a little mistake in the script:
          error.png

          script.png

          In append_features, mod.name returns "". Where first interacted mod itself is #<Class:Anton>.

          Since, I quite don't know what should there be, I can't fix it. So Dan fix it please :)
          0

          Anton_S 
          PluginStore Author
          PluginStore Author
           

          Re: [Code] custom file_loaded() and file_loaded?()

          Postby Dan Rathbun » Thu Jun 28, 2012 2:57 am

          Anton_S wrote:In append_features, mod.name returns "". Where first interacted mod itself is #<Class:Anton>.

          I did say it was not tested.. so it's at concept level.

          It cannot get past the mod.name test, because I tried to include it into module Anton's proxy class, which is anonymous, so it has no name. (That is why mod.name == "") I thought it would call the name method of the Anton module, but it did not.

          I will have to modify the tests a bit.
          0
          Last edited by Dan Rathbun on Thu Jun 28, 2012 7:24 am, edited 1 time in total.
            I'm not here much anymore. But a PM will fire email notifications.
            User avatar
            Dan Rathbun 
            PluginStore Author
            PluginStore Author
             

            Re: [Code] custom file_loaded() and file_loaded?() v2.0.0

            Postby Dan Rathbun » Thu Jun 28, 2012 7:22 am

            Anton, I set you a personalized copy of v2.0.0 by Private Message.

            Everyone else... you can get the generic template v2.0.0 update, in the first post.
            0
              I'm not here much anymore. But a PM will fire email notifications.
              User avatar
              Dan Rathbun 
              PluginStore Author
              PluginStore Author
               

              Re: [Code] custom file_loaded() and file_loaded?() v2.0.0

              Postby Anton_S » Thu Jun 28, 2012 8:07 am

              Dan Rathbun wrote:Anton, I set you a personalized copy of v2.0.0 by Private Message.


              Lol, I feel speacial now.

              :thumb: :thumb: :thumb:
              0

              Anton_S 
              PluginStore Author
              PluginStore Author
               

              Re: [Code] custom file_loaded() and file_loaded?() v2.0.0

              Postby Dan Rathbun » Sat Jun 30, 2012 7:25 am

              OK.. so, for those you who have read the thread.. looked at the template, and scratch your head and wonder why all the complexity to control a private array ??

              Answer.. it's just an example, of a private library mixin module.

              In practice, you can rename the mixin module to whatever you like... and insert whatever proprietary functionality you wish to include in only YOUR plugins (ie, methods in the mixin sections, other class variables and constants, etc.) whilst not allowing some other "hack" to use those features in their plugins.
              0
                I'm not here much anymore. But a PM will fire email notifications.
                User avatar
                Dan Rathbun 
                PluginStore Author
                PluginStore Author
                 

                SketchUcation One-Liner Adverts

                by Ad Machine » 5 minutes ago



                Ad Machine 
                Robot
                 



                 

                Return to Developers' Forum

                Who is online

                Users browsing this forum: No registered users and 7 guests

                Visit our sponsors: