Support for data serialization formats
-
I have a bunch of configuration data that I want to serialize, write to a file, and have my plugin load.
My initial plan was to use YAML for this, but I don't have a clear idea of how to get the YAML libraries to load (on a Mac Pro). I tried the following, but it causes SU to crash:
lrs = '/Library/Ruby/Site' ulr = '/System/Library/Frameworks/' + 'Ruby.framework/Versions/1.8/' + 'usr/lib/ruby' [ "#{lrs}/1.8", "#{lrs}/1.8/powerpc-darwin9.0", "#{lrs}/1.8/universal-darwin9.0", "#{lrs}", "#{ulr}/1.8", "#{ulr}/1.8/powerpc-darwin9.0", "#{ulr}/1.8/universal-darwin9.0", "." ].each {|lib| $; << lib } require 'yaml'
I'd be willing to recast my data as JSON, but I don't see a clear path there, either. One possible brute force solution would be to serialize the data as a Ruby statement and have the plugin eval it, but I'd really like to avoid that.
Suggestions, anyone?
-
@richmorin said:
My initial plan was to use YAML for this, but I don't have a clear idea of how to get the YAML libraries to load (on a Mac Pro). I tried the following, but it causes SU to crash:
I'd suggest having require search the standard libs first, then vendor libs, and lastly the site libs. (But if you want extensions to be able to override the standards, well the the opposite order may be the thing for you. For instance you may have a custom 'yaml.rb' in your site lib, that you wish to be loaded instead of the 'yaml.rb' in the standard lib.)The standard 'yaml.rb' file should be located in the standard ruby library directory. Check manually that it is actually there.
-
Another thing, find the rbconfig.rb file, and read it, or run it and then look at the RbConfig::CONFIG hash. It has all the paths to the proper libs in it for your platform.
The 'rbconfig.rb' file should be in the platform sub-dir of the standard library dir.
You might even check if the file has already been loaded by:
defined? RbConfig::CONFIG
at the console. -
@richmorin said:
I have a bunch of configuration data that I want to serialize, write to a file, and have my plugin load.
Suggestions, anyone?
You could do with a Hash as it's done in 'rbconfig.rb', (or better yet an OpenStruct,) and have your plugin require it's 'plugcfg.rb' file. But this is only good if the configuration options always will be the same when your plugin starts up.
If they will change depending on user choices, etc. then a PStore might be the answer.
Both 'ostruct.rb' and 'pstore.rb' are located in the standard lib directory.
Con with PStore, it has other file dependencies. Your Users would need a full Ruby installation.
OpenStruct objects only need 'ostruct.rb' (it's basically a Hash that has automatic attribute accessor generation.) So you could put a copy of 'ostruct.rb' in your plugin's code subdir, and use it IF the user did not have a full ruby install.The 3rd, option is to use the Sketchup.write_defaultand Sketchup.read_defaultmethods to store your plugin options in the Windows Registry (win32) or Plist file (Mac). Con here is that your options must be 'flatfile' data. No multi-level data is allowed.
All you have is the KEY for your plugin, and a list of Attributes (and their) values under the Key. Pro is you can read from and write to the attributes any time you wish. (Strongly suggest any keyname begin with "Plugin_" and NO spaces in keyname!) -
I got the list of libraries I tried by typing $: in an irb session. I could try fiddling with the list to find a configuration that works, but that seems like it might be a snipe hunt...
It would be really nice to hear from someone who has a working setup that uses the system's Ruby libraries on OSX...
-
page on class PStore
http://www.ruby-doc.org/docs/ProgrammingRuby/html/lib_standard.html#PStore.new
then scroll up to top of PStore section -
@dan rathbun said:
@richmorin said:
I have a bunch of configuration data that I want to serialize, write to a file, and have my plugin load.
Suggestions, anyone?
The 3rd, option is to use the Sketchup.write_defaultand Sketchup.read_defaultmethods to store your plugin options in the Windows Registry (win32) or Plist file (Mac).
- Con here is that your options must be 'flatfile' data. No multi-level data is allowed.
Actually not entirely true.
You can set up the text fields of your attributes anyway you wish, since it's your plugin that will use them.
If it's easier, and faster for loading.. you can have 'array' fields.
ie, a single attribute value can actually be a CSV list of multiple values. (You can use whatever delimiter you wish, not just comma.)del = '%' # delimiter valListString = arrayOfValues.join(del) # then assign valListString to attribute value # reading is opposite # assign attribute value to valListString arrayOfValues = valListString.split(del) # you'd need to iterate the array and convert # any number strings to numerics # see the Sketchup extenstion String.to_l
Advertisement