Sha256: deef76eef291fa7f0635833e2b61fe7fab212700aa6777aeac4005fd81f45125

Contents?: true

Size: 1.88 KB

Versions: 17

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

require "active_model"

module StoreModel
  module Types
    # Implements ActiveModel::Type::Value type for handling an instance of StoreModel::Model
    class OneBase < ActiveModel::Type::Value
      attr_reader :model_klass

      # Returns type
      #
      # @return [Symbol]
      def type
        raise NotImplementedError
      end

      # Casts +value+ from DB or user to StoreModel::Model instance
      #
      # @param value [Object] a value to cast
      #
      # @return StoreModel::Model
      def cast_value(_value)
        raise NotImplementedError
      end

      # Determines whether the mutable value has been modified since it was read
      #
      # @param raw_old_value [Object] old value
      # @param new_value [Object] new value
      #
      # @return [Boolean]
      def changed_in_place?(raw_old_value, new_value)
        cast_value(raw_old_value) != new_value
      end

      protected

      def raise_cast_error(_value)
        raise NotImplementedError
      end

      def model_instance(_value)
        raise NotImplementedError
      end

      private

      # rubocop:disable Style/RescueModifier
      def decode_and_initialize(value)
        decoded = ActiveSupport::JSON.decode(value) rescue nil
        model_instance(decoded) unless decoded.nil?
      rescue ActiveModel::UnknownAttributeError => e
        handle_unknown_attribute(decoded, e)
      end
      # rubocop:enable Style/RescueModifier

      def handle_unknown_attribute(value, exception)
        attribute = exception.attribute.to_sym
        value_symbolized = value.symbolize_keys
        value_symbolized = value_symbolized[:attributes] if value_symbolized.key?(:attributes)

        cast_value(value_symbolized.except(attribute)).tap do |configuration|
          configuration.unknown_attributes[attribute.to_s] = value_symbolized[attribute]
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
store_model-4.1.0 lib/store_model/types/one_base.rb
store_model-4.0.0 lib/store_model/types/one_base.rb
store_model-3.0.2 lib/store_model/types/one_base.rb
store_model-3.0.1 lib/store_model/types/one_base.rb
store_model-3.0.0 lib/store_model/types/one_base.rb
store_model-2.4.0 lib/store_model/types/one_base.rb
store_model-2.3.0 lib/store_model/types/one_base.rb
store_model-2.2.0 lib/store_model/types/one_base.rb
store_model-2.1.2 lib/store_model/types/one_base.rb
store_model-2.1.1 lib/store_model/types/one_base.rb
store_model-2.1.0 lib/store_model/types/one_base.rb
store_model-2.0.1 lib/store_model/types/one_base.rb
store_model-2.0.0 lib/store_model/types/one_base.rb
store_model-1.6.2 lib/store_model/types/one_base.rb
store_model-1.6.1 lib/store_model/types/one_base.rb
store_model-1.6.0 lib/store_model/types/one_base.rb
store_model-1.5.1 lib/store_model/types/one_base.rb