class StoreModel::Types::ManyPolymorphic

Implements ActiveModel::Type::Value type for handling an array of StoreModel::Model

Public Class Methods

new(model_wrapper) click to toggle source

Initializes type for model class

@param model_wrapper [Proc] class to handle

@return [StoreModel::Types::PolymorphicArrayType ]

# File lib/store_model/types/many_polymorphic.rb, line 17
def initialize(model_wrapper)
  @model_wrapper = model_wrapper
end

Public Instance Methods

type() click to toggle source

Returns type

@return [Symbol]

# File lib/store_model/types/many_polymorphic.rb, line 24
def type
  :polymorphic_array
end

Private Instance Methods

cast_model_type_value(value) click to toggle source
# File lib/store_model/types/many_polymorphic.rb, line 38
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
ensure_model_class(array) click to toggle source
# File lib/store_model/types/many_polymorphic.rb, line 30
def ensure_model_class(array)
  array.map do |object|
    next object if implements_model?(object.class)

    cast_model_type_value(object)
  end
end
raise_cast_error(value) click to toggle source
# File lib/store_model/types/many_polymorphic.rb, line 46
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