Skip to content

Instantly share code, notes, and snippets.

@PlasticLizard
Created November 18, 2011 23:54
Show Gist options
  • Save PlasticLizard/1378141 to your computer and use it in GitHub Desktop.
Save PlasticLizard/1378141 to your computer and use it in GitHub Desktop.
getting around BSON serialization issues
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