Sha256: 9777a2ebe518b7b2aa9bab637e8d77636988eefccd780d9a6110eb3805b8585b

Contents?: true

Size: 837 Bytes

Versions: 8

Compression:

Stored size: 837 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
      [*is_or_is_not].any? { |klass| value.is_a?(klass) }
    end
  end

end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
lite-validators-1.0.7 lib/lite/validators/type_validator.rb
lite-validators-1.0.6 lib/lite/validators/type_validator.rb
lite-validators-1.0.5 lib/lite/validators/type_validator.rb
lite-validators-1.0.4 lib/lite/validators/type_validator.rb
lite-validators-1.0.3 lib/lite/validators/type_validator.rb
lite-validators-1.0.2 lib/lite/validators/type_validator.rb
lite-validators-1.0.1 lib/lite/validators/type_validator.rb
lite-validators-1.0.0 lib/lite/validators/type_validator.rb