Sha256: 66dc1d189f504be98de1d88f1eaa24b5470cfe0796f172748876db7b6bc20f64

Contents?: true

Size: 1.19 KB

Versions: 35

Compression:

Stored size: 1.19 KB

Contents

require 'yaml'

module ActiveRecord
  module Coders # :nodoc:
    class YAMLColumn # :nodoc:

      attr_accessor :object_class

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

      def dump(obj)
        return if obj.nil?

        assert_valid_value(obj)
        YAML.dump obj
      end

      def load(yaml)
        return object_class.new if object_class != Object && yaml.nil?
        return yaml unless yaml.is_a?(String) && yaml =~ /^---/
        obj = YAML.load(yaml)

        assert_valid_value(obj)
        obj ||= object_class.new if object_class != Object

        obj
      end

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

      private

      def check_arity_of_constructor
        begin
          load(nil)
        rescue ArgumentError
          raise ArgumentError, "Cannot serialize #{object_class}. Classes passed to `serialize` must have a 0 argument constructor."
        end
      end
    end
  end
end

Version data entries

35 entries across 35 versions & 4 rubygems

Version Path
activerecord-5.0.7.2 lib/active_record/coders/yaml_column.rb
activerecord-5.0.7.1 lib/active_record/coders/yaml_column.rb
activerecord-5.0.7 lib/active_record/coders/yaml_column.rb
activerecord-5.0.6 lib/active_record/coders/yaml_column.rb
activerecord-5.0.6.rc1 lib/active_record/coders/yaml_column.rb
activerecord-5.0.5 lib/active_record/coders/yaml_column.rb
activerecord-5.0.5.rc2 lib/active_record/coders/yaml_column.rb
activerecord-5.0.5.rc1 lib/active_record/coders/yaml_column.rb
activerecord-5.0.4 lib/active_record/coders/yaml_column.rb
activerecord-5.0.4.rc1 lib/active_record/coders/yaml_column.rb
activerecord-5.0.3 lib/active_record/coders/yaml_column.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/activerecord-5.0.2/lib/active_record/coders/yaml_column.rb
activerecord-5.0.2 lib/active_record/coders/yaml_column.rb
activerecord-5.0.2.rc1 lib/active_record/coders/yaml_column.rb
autocompl-0.2.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/coders/yaml_column.rb
autocompl-0.2.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/coders/yaml_column.rb
autocompl-0.2.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/coders/yaml_column.rb
autocompl-0.1.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/coders/yaml_column.rb
autocompl-0.1.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/coders/yaml_column.rb
autocompl-0.1.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/coders/yaml_column.rb