Created
November 18, 2011 23:54
-
-
Save PlasticLizard/1378141 to your computer and use it in GitHub Desktop.
getting around BSON serialization issues
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def to_mongo(doc, with = {}) | |
prep_for_save(doc) if ["msgs","configurations"].include? doc["type"] | |
#ruote functional tests suggest that messages will occasionally have | |
#an unserialized FlowExpressionId if they are representing an error. | |
#Since we are no longer converting documents to JSON, this will cause | |
#mongo db to reject the document, so we convert it. configurations need classes | |
#converted to strings | |
doc.merge(with).merge!("put_at" => Ruote.now_to_utc_s) | |
end | |
def prep_for_save(doc) | |
if doc.is_a?(Hash) | |
doc.keys.each do |key| | |
val = doc[key] | |
if key.nil? | |
#this exists purely to survive ruotes functional tests | |
#where errors are deliberately introduced. | |
doc.delete(key) | |
doc[""] = val | |
elsif val.respond_to?(:to_storage_id) | |
doc[key] = val.to_storage_id | |
elsif val.kind_of?(Class) | |
doc[key] = val.name | |
else | |
prep_for_save(val) | |
end | |
end | |
elsif doc.kind_of?(Array) | |
doc.each_with_index do |child, idx| | |
if child.kind_of?(Class) | |
doc[idx] = child.name | |
else | |
prep_for_save(child) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment