[Code] User::ToolbarSet collection
-
[ Code ] User::ToolbarSet collection
An example of a universal toolbar set collection.
(I keep these scripts in a "User" sub-dir of the "Plugins" dir.)# User_ToolbarSet.rb # # VERSION ; 2.0.1 # # An example by; Dan Rathbun # # Maintain a Hash of custom Ruby Toolbar object references, # and access them by using their name as the hash key; # Example; # User;;ToolbarSet["Shadow Strings Fix"].show() # # You can add new references into the Hash when you create toolbar; # Ex; # tb = UI;;Toolbar.new("Nifty Tools") # User;;ToolbarSet[tb.name]= tb # # OR; # caption = "Nifty Tools" # User;;ToolbarSet[caption]= UI;;Toolbar.new(caption) # # You can call refresh() anytime to be sure the hash is up to date; # Ex; # User;;ToolbarSet.refresh() # module User ### Hash to hold references to Ruby Toolbar objects; # ToolbarSet = Hash.new {|this,key| # block of code that gets called if the [key] method # fails to find a member with the given key argument; this.refresh() if this.has_key?(key) this[key] # return the new toolbar reference else msg = "Unknown Key '#{key.to_s}' in User;;ToolbarSet hash!" puts('ERROR; '<<msg) if $VERBOSE raise(IndexError,msg,caller) end } ### Extend the ToolbarSet hash with a refresh method; # def ToolbarSet.refresh() added = 0 count = ObjectSpace.each_object(UI;;Toolbar){|tb| next if ToolbarSet.has_key?(tb.name) && ToolbarSet[tb.name].is_a?(UI;;Toolbar) # Otherwise, add it's reference to the hash; ToolbarSet[tb.name]= tb added += 1 } if $VERBOSE # normally false (can be nil for silent mode) if added > 0 plural = added>1 ? "s" ; "" puts("#{Module.nesting[0].name};;ToolbarSet#refresh(); #{added} new toolbar#{plural} added.") else puts("#{Module.nesting[0].name};;ToolbarSet#refresh(); No new toolbars.") end puts(" total UI;;Toolbar objects; #{count}") end # if $VERBOSE end ### initially load the ToolbarSet hash by calling refresh now; # ToolbarSet.refresh() end # module UserVERSION : 2.0.0 - updated as explained in the 2nd post below.
VERSION : 2.0.1 - fixedrefresh()methodnextstatement, to avoid vicious loop

-
Perhaps we can modify the
User::ToolbarSethash's[]method's errorProc, so that it refreshes the hash automatically.Instead of line 28:
ToolbarSet = {}
which creates a hash that returnsnilif the[]method does not find a member with the given key ...We replace line 28 with a call using the block form of the
Hash::new()class constructor.
NOW.. whenever the[]method does not find a member with the given key, the block is run:ToolbarSet = Hash.new {|this,key| this.refresh() if this.has_key?(key) this[key] # return the new ref else msg = "Unknown Key '#{key.to_s}' in User;;ToolbarSet hash!" puts('ERROR; '<<msg) if $VERBOSE raise(IndexError,msg,caller) end }
-
UPDATED code in 1st post to v2.0.0, per 2nd post above.
-
UPDATED code in 1st post to v2.0.1, in
refresh()method'snextstatement to avoid a vicious loop. (We useHash#has_key?(key)rather thanHash#[key]!=nil, because our v2.0.0 update makes the[]method call therefreshmethod, so we must be very careful from within therefreshmethod that any[]method call does not cause a unwanted or unneeded, call torefresh.)
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better π
Register LoginAdvertisement