Sha256: ff6742b96fd435e948b624a801fc13aa3a2eff2fb141b49c6ab8e7f0bf8cb59e

Contents?: true

Size: 1.27 KB

Versions: 30

Compression:

Stored size: 1.27 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
      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

30 entries across 30 versions & 1 rubygems

Version Path
store_model-3.0.2 lib/store_model/types/many_polymorphic.rb
store_model-3.0.1 lib/store_model/types/many_polymorphic.rb
store_model-3.0.0 lib/store_model/types/many_polymorphic.rb
store_model-2.4.0 lib/store_model/types/many_polymorphic.rb
store_model-2.3.0 lib/store_model/types/many_polymorphic.rb
store_model-2.2.0 lib/store_model/types/many_polymorphic.rb
store_model-2.1.2 lib/store_model/types/many_polymorphic.rb
store_model-2.1.1 lib/store_model/types/many_polymorphic.rb
store_model-2.1.0 lib/store_model/types/many_polymorphic.rb
store_model-2.0.1 lib/store_model/types/many_polymorphic.rb
store_model-2.0.0 lib/store_model/types/many_polymorphic.rb
store_model-1.6.2 lib/store_model/types/many_polymorphic.rb
store_model-1.6.1 lib/store_model/types/many_polymorphic.rb
store_model-1.6.0 lib/store_model/types/many_polymorphic.rb
store_model-1.5.1 lib/store_model/types/many_polymorphic.rb
store_model-1.5.0 lib/store_model/types/many_polymorphic.rb
store_model-1.4.0 lib/store_model/types/many_polymorphic.rb
store_model-1.3.0 lib/store_model/types/many_polymorphic.rb
store_model-1.2.0 lib/store_model/types/many_polymorphic.rb
store_model-1.1.0 lib/store_model/types/many_polymorphic.rb