[Code] custom file_loaded() and file_loaded?() v2.0.0
-
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 makesAuthor::LoadUtil
a private module !!YOU use it by including it in your sub-modules.
# 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
andfile_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 moduleAuthor
, 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.) -
-
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.
-
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 arequire
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. -
Here is jst a little mistake in the script:
Inappend_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
-
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.
-
Now.. that said.. yes there are alternatives.
Sometimes I use
defined?
If the module var is going to be
@@menu
, then I use:unless defined?(@@menu) @@menu=UI.menu('Plugins') # define items, etc end
-
@anton_s said:
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 theAnton
module, but it did not.I will have to modify the tests a bit.
-
@dan rathbun said:
Anton, I set you a personalized copy of v2.0.0 by Private Message.
Lol, I feel speacial now.
-
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.
Advertisement