Sha256: 7ab80452c6f6743fd4e10c4ba77f476bc23bbbf8339292f40a5bfabe21c5cbed

Contents?: true

Size: 1.29 KB

Versions: 3

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

require "active_model"

module StoreModel
  module Types
    # Implements ActiveModel::Type::Value type for handling an array of
    # StoreModel::Model
    class ManyPolymorphic < ManyBase
      include PolymorphicHelper

      # Initializes type for model class
      #
      # @param model_wrapper [Proc] class to handle
      #
      # @return [StoreModel::Types::PolymorphicArrayType ]
      def initialize(model_wrapper)
        @model_wrapper = model_wrapper
        super()
      end

      # Returns type
      #
      # @return [Symbol]
      def type
        :polymorphic_array
      end

      private

      def ensure_model_class(array)
        array.map do |object|
          next object if implements_model?(object.class)

          cast_model_type_value(object)
        end
      end

      def cast_model_type_value(value)
        model_klass = @model_wrapper.call(value)

        raise_extract_wrapper_error(model_klass) unless implements_model?(model_klass)

        model_klass.to_type.cast_value(value)
      end

      def raise_cast_error(value)
        raise StoreModel::Types::CastError,
              "failed casting #{value.inspect}, only String, " \
              "Hash or instances which implement StoreModel::Model are allowed"
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
store_model-4.2.0 lib/store_model/types/many_polymorphic.rb
store_model-4.1.0 lib/store_model/types/many_polymorphic.rb
store_model-4.0.0 lib/store_model/types/many_polymorphic.rb