[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 User
VERSION : 2.0.0 - updated as explained in the 2nd post below.
VERSION : 2.0.1 - fixedrefresh()
methodnext
statement, to avoid vicious loop
-
Perhaps we can modify the
User::ToolbarSet
hash's[]
method's errorProc
, so that it refreshes the hash automatically.Instead of line 28:
ToolbarSet = {}
which creates a hash that returnsnil
if 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'snext
statement to avoid a vicious loop. (We useHash#has_key?(key)
rather thanHash#[key]!=nil
, because our v2.0.0 update makes the[]
method call therefresh
method, so we must be very careful from within therefresh
method that any[]
method call does not cause a unwanted or unneeded, call torefresh
.)
Advertisement