Sha256: 01466d7845265ea87f4588acb9ef508a9418b24f78b5d31e344098477ab0e907

Contents?: true

Size: 1.92 KB

Versions: 23

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true

require "yaml"

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

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

      def dump(obj)
        return if obj.nil?

        assert_valid_value(obj, action: "dump")
        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.start_with?("---")
        obj = yaml_load(yaml)

        assert_valid_value(obj, action: "load")
        obj ||= object_class.new if object_class != Object

        obj
      end

      def assert_valid_value(obj, action:)
        unless obj.nil? || obj.is_a?(object_class)
          raise SerializationTypeMismatch,
            "can't #{action} `#{@attr_name}`: was supposed to be a #{object_class}, but was a #{obj.class}. -- #{obj.inspect}"
        end
      end

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

        if YAML.respond_to?(:unsafe_load)
          def yaml_load(payload)
            if ActiveRecord.use_yaml_unsafe_load
              YAML.unsafe_load(payload)
            else
              YAML.safe_load(payload, permitted_classes: ActiveRecord.yaml_column_permitted_classes, aliases: true)
            end
          end
        else
          def yaml_load(payload)
            if ActiveRecord.use_yaml_unsafe_load
              YAML.load(payload)
            else
              YAML.safe_load(payload, permitted_classes: ActiveRecord.yaml_column_permitted_classes, aliases: true)
            end
          end
        end
    end
  end
end

Version data entries

23 entries across 23 versions & 4 rubygems

Version Path
activerecord-7.0.8.6 lib/active_record/coders/yaml_column.rb
activerecord-7.0.8.5 lib/active_record/coders/yaml_column.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activerecord-7.0.8.4/lib/active_record/coders/yaml_column.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/activerecord-7.0.5.1/lib/active_record/coders/yaml_column.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/activerecord-7.0.5.1/lib/active_record/coders/yaml_column.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/activerecord-7.0.5.1/lib/active_record/coders/yaml_column.rb
activerecord-7.0.8.4 lib/active_record/coders/yaml_column.rb
activerecord-7.0.8.1 lib/active_record/coders/yaml_column.rb
activerecord-7.0.8 lib/active_record/coders/yaml_column.rb
activerecord-7.0.7.2 lib/active_record/coders/yaml_column.rb
activerecord-7.0.7.1 lib/active_record/coders/yaml_column.rb
activerecord-7.0.7 lib/active_record/coders/yaml_column.rb
activerecord-7.0.6 lib/active_record/coders/yaml_column.rb
activerecord-7.0.5.1 lib/active_record/coders/yaml_column.rb
rubypitaya-3.12.5 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/coders/yaml_column.rb
activerecord-7.0.5 lib/active_record/coders/yaml_column.rb
activerecord-7.0.4.3 lib/active_record/coders/yaml_column.rb
rubypitaya-3.12.4 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/coders/yaml_column.rb
activerecord-7.0.4.2 lib/active_record/coders/yaml_column.rb
rubypitaya-3.12.3 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/coders/yaml_column.rb