Sha256: b08688910efeba29bc23e12675e2476ecb3bf254c4c507b019df8116685aa0b9

Contents?: true

Size: 676 Bytes

Versions: 3

Compression:

Stored size: 676 Bytes

Contents

# frozen_string_literal: true

class SingletonValidator < ActiveModel::EachValidator
  def validate_each(obj, attribute, value)
    with_option = Array.wrap(options[:with] || options[:in])

    unless value.is_a?(Module)
      return obj.errors.add(attribute, options[:message] || "is not a class or module")
    end

    is_valid = with_option.any? do |type|
      type.is_a?(Module) or raise ArgumentError, "#{type.inspect} is not a class or module"

      value == type || (value < type || value.is_a?(type))
    end

    expectation = with_option.map(&:name).join(" | ")

    is_valid or obj.errors.add(attribute, (options[:message] || "is not #{expectation}"))
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
solid-process-0.3.0 lib/solid/validators/singleton_validator.rb
solid-process-0.2.0 lib/solid/validators/singleton_validator.rb
solid-process-0.1.0 lib/solid/validators/singleton_validator.rb