[Nitro] Setings to YAML (was Re: Change settings programmatically)

gabriele renzi surrender_it at yahoo.it
Wed Aug 9 06:27:42 EDT 2006


TRANS ha scritto:
>>Anyway, a take on the other question: If I recall correct you recently
>>changed the implementation of annotations so that each one keeps track
>>of its owner, and this may be the reason thay're no more serializable
>>with a dummy to_yaml.
>>Are there any plans to provide this kind of functionality, or to accept
>>a patch that could do it?
>>Or possibly I'm just horribly wrong?
> 
> 
> Ah, now there's an unexpected consequence. Personally I'm not so sure
> they should be tracking their owner, but George had requested that
> they at least know hteir own name, which menas they might as well know
> their owner too. But I'll have alook at it again and see. Could you
> give me an example of how you had serialized them?

there was an old patch from Jonas Pfenninger[1] which basically has this:

[Glue::Configuration#to_yaml and #save added
Jonas Pfenniger <zimba.tm at gmail.com>**20060523120027] {
hunk ./glue/lib/glue/configuration.rb 137
+
+    # Converts the configuration into yaml data.
+
+    def to_yaml()
+      options = Hash.new
+      @@owners.each_pair do |owner,ss|
+        hash = Hash.new
+        options[owner.to_s.gsub(/::/,'.')] = {}
+        ss.each_pair do |key, setting|
+          options[owner.to_s.gsub(/::/,'.')][key.to_s] = setting.value
+        end
+      end
+      options.to_yaml
+    end
+
+    # Saves the yaml data in the designed file.
+
+    def save(filename)
+      File.open(filename,'w').write(to_yaml.to_s)
+    end


you can see from the attached test that the output should be reparsable 
from Configuration.parse, which is already there but whose format is not 
cleanly documented:

hunk ./glue/test/glue/tc_configuration.rb 133
+
+    # Settings save and reload
+    Configuration.parse(Configuration.to_yaml)
+    assert_equal 'wow', Dummy.root_dir
+

possibly we could redefine to_yaml in Setting to do the same thing to 
owner that is done there, i.e. to_s+gsub.

But be careful, I'm only _supposing_ this is the reason fot it to fail..
the error I'm seeing happens in cases like:
 >> Configuration.settings.at(0).to_yaml
TypeError: can't dump anonymous class Class
         from lib/ruby/1.8/yaml/rubytypes.rb:6:in `to_yaml'

         from lib/ruby/1.8/yaml/rubytypes.rb:41:in `to_yaml'
         from lib/ruby/1.8/yaml/rubytypes.rb:40:in `to_yaml'
         from lib/ruby/1.8/yaml/rubytypes.rb:39:in `to_yaml'
         from lib/ruby/1.8/yaml.rb:387:in `quick_emit'
         from lib/ruby/1.8/yaml/rubytypes.rb:38:in `to_yaml'
         from lib/ruby/1.8/yaml/rubytypes.rb:18:in `to_yaml'
         from lib/ruby/1.8/yaml/rubytypes.rb:17:in `to_yaml'
         from lib/ruby/1.8/yaml/rubytypes.rb:16:in `to_yaml'
         from lib/ruby/1.8/yaml.rb:387:in `quick_emit'
         from lib/ruby/1.8/yaml/rubytypes.rb:15:in `to_yaml'
         from (irb):2

it seem that redefining to_yaml_properties to avoid @type, @owner and 
@options make to_yaml work again, so the problem may not be strictly 
related to just owner..


[1]http://rubyforge.org/pipermail/nitro-general/2006-May/004569.html



-- 

blog en: http://www.riffraff.info
blog it: http://riffraff.blogsome.com
jabber : rff.rff at gmail dot com



More information about the Nitro-general mailing list