Sha256: 48b1c8298d06a09d47e58033b25bab053544623b23787e5b59425eb6a7feb3f7

Contents?: true

Size: 1.8 KB

Versions: 26

Compression:

Stored size: 1.8 KB

Contents

# frozen_string_literal: true

module ActiveRecord
  module Type
    class Serialized < DelegateClass(ActiveModel::Type::Value) # :nodoc:
      undef to_yaml if method_defined?(:to_yaml)

      include ActiveModel::Type::Helpers::Mutable

      attr_reader :subtype, :coder

      def initialize(subtype, coder)
        @subtype = subtype
        @coder = coder
        super(subtype)
      end

      def deserialize(value)
        if default_value?(value)
          value
        else
          coder.load(super)
        end
      end

      def serialize(value)
        return if value.nil?
        unless default_value?(value)
          super coder.dump(value)
        end
      end

      def inspect
        Kernel.instance_method(:inspect).bind_call(self)
      end

      def changed_in_place?(raw_old_value, value)
        return false if value.nil?
        raw_new_value = encoded(value)
        raw_old_value.nil? != raw_new_value.nil? ||
          subtype.changed_in_place?(raw_old_value, raw_new_value)
      end

      def accessor
        ActiveRecord::Store::IndifferentHashAccessor
      end

      def assert_valid_value(value)
        if coder.respond_to?(:assert_valid_value)
          coder.assert_valid_value(value, action: "serialize")
        end
      end

      def force_equality?(value)
        coder.respond_to?(:object_class) && value.is_a?(coder.object_class)
      end

      private
        def default_value?(value)
          value == coder.load(nil)
        end

        def encoded(value)
          return if default_value?(value)
          payload = coder.dump(value)
          if payload && binary? && payload.encoding != Encoding::BINARY
            payload = payload.dup if payload.frozen?
            payload.force_encoding(Encoding::BINARY)
          end
          payload
        end
    end
  end
end

Version data entries

26 entries across 24 versions & 3 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activerecord-7.0.3.1/lib/active_record/type/serialized.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activerecord-7.0.2.3/lib/active_record/type/serialized.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/activerecord-7.0.2.3/lib/active_record/type/serialized.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/activerecord-7.0.3.1/lib/active_record/type/serialized.rb
rubypitaya-3.12.5 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/type/serialized.rb
activerecord-7.0.4.3 lib/active_record/type/serialized.rb
rubypitaya-3.12.4 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/type/serialized.rb
activerecord-7.0.4.2 lib/active_record/type/serialized.rb
rubypitaya-3.12.3 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/type/serialized.rb
activerecord-7.0.4.1 lib/active_record/type/serialized.rb
rubypitaya-3.12.2 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/type/serialized.rb
activerecord-7.0.4 lib/active_record/type/serialized.rb
activerecord-7.0.3.1 lib/active_record/type/serialized.rb
activerecord-7.0.3 lib/active_record/type/serialized.rb
activerecord-7.0.2.4 lib/active_record/type/serialized.rb
activerecord-7.0.2.3 lib/active_record/type/serialized.rb
activerecord-7.0.2.2 lib/active_record/type/serialized.rb
activerecord-7.0.2.1 lib/active_record/type/serialized.rb
activerecord-7.0.2 lib/active_record/type/serialized.rb
activerecord-7.0.1 lib/active_record/type/serialized.rb