Deserializing classes in Ruby using YAML.load

Quick note: when using YAML.load to deserialize an instance of a class that was serialized using to_yaml may end up not working! I have an object called ApplicationProvision, call to_yaml and it looks like this ;

"--- !ruby/object:ApplicationProvision \nconfiguration: \n- !ruby/object:InstantStack::StackConfigurationOption \n default_value: NginX\n possible_values: \n - Apache\n - NginX\n question_key: web_server\n question_text: Do you want to host from Apache or NginX?\n question_type: multiple_choice\n response_value: NginX\n- !ruby/object:InstantStack::StackConfigurationOption \n default_value: MySQL\n possible_values: \n - MySQL\n - SQLite\n question_key: db_server\n question_text: Do you want to host the database from MySQL or SQLite?\n question_type: multiple_choice\n response_value: MySQL\nguid: 993b9956-df61-4612-a8b4-c71937603467\nprevious_stage: 5\nregion: 1\nsize: 4\nstack: 21\nstate: :configured\n"
.. perfect

However deserialized, it looks like this;

#<ApplicationProvision:0x105716000
@class="ApplicationProvision",
@ivars=
{"region"=>1,
"stack"=>21,
"size"=>4,
"previous_stage"=>5,
"guid"=>"993b9956-df61-4612-a8b4-c71937603467",
"configuration"=>
[#<InstantStack::StackConfigurationOption:0x105717540
@default_value="NginX",
@possible_values=["Apache", "NginX"],
@question_key="web_server",
@question_text="Do you want to host from Apache or NginX?",
@question_type="multiple_choice",
@response_value="NginX">,
#<InstantStack::StackConfigurationOption:0x1057169d8
@default_value="MySQL",
@possible_values=["MySQL", "SQLite"],
@question_key="db_server",
@question_text="Do you want to host the database from MySQL or SQLite?",
@question_type="multiple_choice",
@response_value="MySQL">],
"state"=>:configured}>

Not good! Little bit of reading around this problem and I found this post on StackOverflow.

Add a require_dependency at the top of the class in context and everything works fine

#<ApplicationProvision:0x10585aec0
@configuration=
[#<InstantStack::StackConfigurationOption:0x10585c400
@default_value="NginX",
@possible_values=["Apache", "NginX"],
@question_key="web_server",
@question_text="Do you want to host from Apache or NginX?",
@question_type="multiple_choice",
@response_value="NginX">,
#<InstantStack::StackConfigurationOption:0x10585b898
@default_value="MySQL",
@possible_values=["MySQL", "SQLite"],
@question_key="db_server",
@question_text="Do you want to host the database from MySQL or SQLite?",
@question_type="multiple_choice",
@response_value="MySQL">],
@guid="993b9956-df61-4612-a8b4-c71937603467",
@previous_stage=5,
@region=1,
@size=4,
@stack=21,
@state=:configured>

et voila!

This entry was posted in Uncategorized. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>