aboutsummaryrefslogblamecommitdiffstats
path: root/activerecord/lib/active_record/coders/yaml_column.rb
blob: 9b0df119ef154493effb16de07c4322c9f86377f (plain) (tree)

































                                                                                      
module ActiveRecord
  # :stopdoc:
  module Coders
    class YAMLColumn
      RESCUE_ERRORS = [ ArgumentError ]

      if defined?(Psych) && defined?(Psych::SyntaxError)
        RESCUE_ERRORS << Psych::SyntaxError
      end

      attr_accessor :object_class

      def initialize(object_class = Object)
        @object_class = object_class
      end

      def load(yaml)
        return yaml unless yaml.is_a?(String) && yaml =~ /^---/
        begin
          obj = YAML::load(yaml)

          unless obj.is_a?(object_class) || obj.nil?
            raise SerializationTypeMismatch,
              "Attribute was supposed to be a #{object_class}, but was a #{obj.class}"
          end

        rescue *RESCUE_ERRORS
          yaml
        end
      end
    end
  end
  # :startdoc
end