Class: Exekutor::Configuration::SerializerValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/exekutor/configuration.rb

Overview

Validates the value for a serializer, which must implement dump & load

Class Method Summary collapse

Class Method Details

.convert!(serializer) ⇒ #dump&#load

Tries to convert the specified value to a serializer, raises an error if the conversion fails.

Parameters:

  • serializer (Any)

    the value to convert

Returns:

  • (#dump&#load)

Raises:

  • (Error)

    if the serializer has not implemented dump & load



387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/exekutor/configuration.rb', line 387

def self.convert!(serializer)
  return serializer if SerializerValidator.valid? serializer

  if serializer.respond_to?(:call)
    serializer = serializer.call
    return serializer if SerializerValidator.valid? serializer
  end
  if serializer.respond_to?(:new)
    serializer = serializer.new
    return serializer if SerializerValidator.valid? serializer
  end

  raise Error, <<~MSG.squish
    The configured serializer (#{serializer.class}) does not respond to #dump and #load
  MSG
end

.valid?(serializer) ⇒ Boolean

Returns whether the serializer has implemented dump & load.

Parameters:

  • serializer (Any)

    the value to validate

Returns:

  • (Boolean)

    whether the serializer has implemented dump & load



379
380
381
# File 'lib/exekutor/configuration.rb', line 379

def self.valid?(serializer)
  serializer.respond_to?(:dump) && serializer.respond_to?(:load)
end