Model attributes and hash

Is there possibility to store in Model Attribute hash?

array_h = Hash.new
Sketchup.active_model.set_attribute(‘dynamic_attributes’, ‘TYP’,array_h)

(1) Are you sure you want to put attributes into a 'dynamic_attributes' dictionary ?
You need a good reason to do so, as it is a shared dictionary.

(2) There currently is NO model level 'dynamic_attributes' dictionary.

(3) If you wish to store your extension specific attributes, then create your own dictionary.

  • Because the model object is a shared object between ALL extensions, you need to qualify any dictionary name that you create with something like the following format:

SpecificAuthor_SomeSpecificPluginName_SpecificDictionaryName

So say you had a “Widget” plugin, and you needed to save model specific settings:

dictname = "Mamjankowski_CrazyWidget_ModelSettings"

If your “CrazyWidget” extension has only 1 (one) total dictionary, then you would not need to “difference” the name, and you could shorten it to:

dictname = "Mamjankowski_CrazyWidget"

I strongly advise that the first two parts of any attribute dictionary name be the same as your toplevel module name + your extension sub-module name.

That way people can tell what extension a dictionary belongs to.

You need to avoid dictionary name collisions between your own extensions, as well as everyone else’s extensions.

Arrays are not a Hash.

A Sketchup::AttributeDictionary is not a hash, nor a subclass of Ruby’s Hash.

But they look very similar, and both have the Enumerable mixin module included.

my_hash.each {|key,value|
  dict[key.to_s]= value rescue dict[key.to_s]= value.to_s
}

Hashes can have any object reference as a key, but attribute dictionary might not.

Some value (object types) can be directly saved to and read from attribute dictionaries, but not hashes.

Test fully.