Sha256: c5b3519d6c75f9dc083899ccb8ccdbeff0792065e9842298410a121235cc0a0b

Contents?: true

Size: 907 Bytes

Versions: 2

Compression:

Stored size: 907 Bytes

Contents

# frozen_string_literal: true

class Serega
  class Attribute
    class CheckOptSerializer
      class << self
        #
        # Checks attribute :serializer option
        #
        # @param opts [Hash] Attribute options
        #
        # @raise [Error] Error that option has invalid value
        #
        # @return [void]
        #
        def call(opts)
          return unless opts.key?(:serializer)

          value = opts[:serializer]
          return if valid_serializer?(value)

          raise Error, "Invalid option :serializer => #{value.inspect}." \
            " Can be a Serega subclass, a String or a Proc without arguments"
        end

        private

        def valid_serializer?(value)
          value.is_a?(String) ||
            (value.is_a?(Proc) && (value.parameters.count == 0)) ||
            (value.is_a?(Class) && (value < Serega))
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
serega-0.1.1 lib/serega/validations/attribute/check_opt_serializer.rb
serega-0.1.0 lib/serega/validations/attribute/check_opt_serializer.rb