diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2009-10-19 12:33:36 -0700 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2009-10-19 12:33:36 -0700 |
commit | a49c3b03650b3193cc9440a3b219ab7f19326297 (patch) | |
tree | dd68d637a074bc08d1328d780aeb092b2a4c8874 /activerecord/lib/active_record/types/serialize.rb | |
parent | 028911ad00e7f7733064a80393a1548401bba7af (diff) | |
parent | 1ac5cf478825391071d34ec3d7f294fe28c0fceb (diff) | |
download | rails-a49c3b03650b3193cc9440a3b219ab7f19326297.tar.gz rails-a49c3b03650b3193cc9440a3b219ab7f19326297.tar.bz2 rails-a49c3b03650b3193cc9440a3b219ab7f19326297.zip |
Merge branch 'master' of github.com:rails/rails
Diffstat (limited to 'activerecord/lib/active_record/types/serialize.rb')
-rw-r--r-- | activerecord/lib/active_record/types/serialize.rb | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/types/serialize.rb b/activerecord/lib/active_record/types/serialize.rb new file mode 100644 index 0000000000..7b6af1981f --- /dev/null +++ b/activerecord/lib/active_record/types/serialize.rb @@ -0,0 +1,33 @@ +module ActiveRecord + module Type + class Serialize < Object + + def cast(value) + unserialize(value) + end + + def appendable? + true + end + + protected + + def unserialize(value) + unserialized_object = object_from_yaml(value) + + if unserialized_object.is_a?(@options[:serialize]) || unserialized_object.nil? + unserialized_object + else + raise SerializationTypeMismatch, + "#{name} was supposed to be a #{@options[:serialize]}, but was a #{unserialized_object.class.to_s}" + end + end + + def object_from_yaml(string) + return string unless string.is_a?(String) && string =~ /^---/ + YAML::load(string) rescue string + end + + end + end +end
\ No newline at end of file |