Sha256: a4b1572627c5216e8621f05f16c2550feeedf6f36b1336c493d2d60edafd1251

Contents?: true

Size: 841 Bytes

Versions: 14

Compression:

Stored size: 841 Bytes

Contents

# frozen_string_literal: true

# NOTE: Implements pseudo-boolean class
class Boolean; end

class TypeValidator < BaseValidator

  def validate_each(record, attribute, value)
    assert_valid_is_or_is_not!
    super
  end

  private

  def assert_valid_is_or_is_not!
    return if options.key?(:is) || options.key?(:is_not)

    raise ArgumentError, 'Missing ":is" or ":is_not" attribute for comparison.'
  end

  # rubocop:disable Naming/PredicateName
  def is_or_is_not
    options[:is] || options[:is_not]
  end
  # rubocop:enable Naming/PredicateName

  def valid_attr?
    return !valid_type? if options.key?(:is_not)

    valid_type?
  end

  def valid_type?
    if is_or_is_not == Boolean
      value.is_a?(TrueClass) || value.is_a?(FalseClass)
    else
      Array(is_or_is_not).any? { |klass| value.is_a?(klass) }
    end
  end

end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
lite-validators-1.8.0 lib/lite/validators/type_validator.rb
lite-validators-1.7.2 lib/lite/validators/type_validator.rb
lite-validators-1.7.1 lib/lite/validators/type_validator.rb
lite-validators-1.7.0 lib/lite/validators/type_validator.rb
lite-validators-1.6.0 lib/lite/validators/type_validator.rb
lite-validators-1.5.0 lib/lite/validators/type_validator.rb
lite-validators-1.4.0 lib/lite/validators/type_validator.rb
lite-validators-1.3.2 lib/lite/validators/type_validator.rb
lite-validators-1.3.1 lib/lite/validators/type_validator.rb
lite-validators-1.3.0 lib/lite/validators/type_validator.rb
lite-validators-1.2.0 lib/lite/validators/type_validator.rb
lite-validators-1.1.2 lib/lite/validators/type_validator.rb
lite-validators-1.1.1 lib/lite/validators/type_validator.rb
lite-validators-1.1.0 lib/lite/validators/type_validator.rb