Module depth: Good Practices
-
I've a extension naming question regarding module nesting. Which is it, 1, or 2 levels? I.E.:
Module HSMNameOfModule
or,
Module HSM #Author namespace Module NameOfModule #extension namespace
I have foggy memories of seeing 3 deep nesting but have forgotten the details.
-
@hsmyers said:
I've a extension naming question regarding module nesting. Which is it, 1, or 2 levels? I.E.:
> Module HSMNameOfModule >
or,
> Module HSM #Author namespace > Module NameOfModule #extension namespace >
I have foggy memories of seeing 3 deep nesting but have forgotten the details.
Option 2 is usually recommended.
-
The first level keeps your namespace clear of anybody elses.
The second level keeps this extension's namespace clear of your other extensions (it happens! I just had a collision between two of my extensions when I forgot about a name I had used already)
I think a third level would be needed only in a complex suite of extensions such as Fredo6 or TomTom develop - though of course any class you define inside the second level creates a third namespace. -
@slbaumgartner said:
The first level keeps your namespace clear of anybody elses.
The second level keeps this extension's namespace clear of your other extensions (it happens! I just had a collision between two of my extensions when I forgot about a name I had used already)
I think a third level would be needed only in a complex suite of extensions such as Fredo6 or TomTom develop - though of course any class you define inside the second level creates a third namespace.Useful explanation with war story The last bit reminded me where I'd seen the third level, so quite helpful!
-
I use a modified template that Dan Rathburn created. I also use his file naming convention for the loader etc.
You could strip this down. But I like to place my plugins in a shared folder so I can test a single plugin on 6 versions of Sketchup SU7 through SU2016
require('extensions.rb') module YourCompany # Proprietary TopLevel Namespace; No Trespassing! module ThisPlugin # Namespace for THIS plugin # Create an entry in the Extension list that loads script called; # "gkware_doormaker_loader.rb" APP_VERSION = '1.0.1' @plugin = SketchupExtension.new('Company Plugin Description', File.join('PluginName', 'Company_PluginName_loader')) @plugin.creator = 'Your Name' @plugin.copyright = '(c)2013-2016 by YourCompany' @plugin.version = APP_VERSION @plugin.description = 'Plugin does something - optional url to your website' unless @plugin.class.method_defined?(;path) # # Define a singleton method for @plugin to access the @path attribute. # (... because the API did not do it in extensions.rb !!) # def @plugin.path() instance_variable_get(;@path) end end # unless # Create local path and filename constants; PATH = @plugin.path() LOADER_SUFFIX = '_loader' LOADER = File.basename(PATH) tmp = LOADER.split('_') RBSFILE = tmp[0] + '_' + tmp[1] + '.rbs' # RELDIR is relative to Plugins dir, OR the dir from # the $LOAD_PATH array that require used to find it. RELDIR = File.dirname(PATH) ROOT = $LOAD_PATH.find(false) do |abspath| Kernel.test(?d, File.join(abspath, RELDIR)) end if ROOT ABSDIR = File.join(ROOT, RELDIR) else # assume the target dir is directly below, the dir that THIS file is in. ABSDIR = File.join(File.dirname(__FILE__), RELDIR) end # Register this extension with the Sketchup;;ExtensionManager Sketchup.register_extension(@plugin, true) end # module YourCompany;;ThisPlugin end # module YourCompany
Advertisement