Last active
August 29, 2015 13:56
-
-
Save shekibobo/9217202 to your computer and use it in GitHub Desktop.
Convert serialized yaml from syck to psych
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
require 'yaml' | |
# original idea comes from http://darwinweb.net/articles/convert-syck-to-psych-yaml-format | |
namespace :encoding do | |
desc 'convert serialized Content data from syck yaml serializations to psych' | |
task :psych => :environment do | |
content_id = ENV['content_id'] | |
if content_id | |
convert_data Content.find(content_id) | |
else | |
Content.all.each do |content| | |
convert_data(content) | |
end | |
end | |
puts "Done." | |
end | |
end | |
def convert_data(content) | |
use_syck | |
hash = content.data | |
use_psych | |
content.update_attributes(data: hash) ? print(".") : print("F(#{content.id})") | |
end | |
def use_syck | |
YAML::ENGINE.yamler = 'syck' | |
raise "Oops! Something went horribly wrong." unless YAML == Syck | |
end | |
def use_psych | |
YAML::ENGINE.yamler = 'psych' | |
raise "Oops! Something went horribly wrong." unless YAML == Psych | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment