Sha256: d6da4f30fd3c58148d753f1f41854fd76f040be119feee5d03bf2bc194127990

Contents?: true

Size: 1.84 KB

Versions: 1

Compression:

Stored size: 1.84 KB

Contents

# frozen_string_literal: true

# @api private
# @since 0.2.0
class SmartCore::Types::Primitive::NilableValidator
  require_relative 'nilable_validator/result'

  # @param validator [
  #   SmartCore::Types::Primitive::Validator,
  #   SmartCore::Types::Primitive::SumValidator,
  #   SmartCore::Types::Primitive::MultValidator,
  #   SmartCore::Types::Primitive::NilableValidator
  # ]
  # @return [void]
  #
  # @api private
  # @since 0.2.0
  def initialize(validator)
    @type = nil
    @validator = validator
  end

  # @param type [SmartCore::Types::Primitive]
  # @return [void]
  #
  # @api private
  # @since 0.2.0
  def ___assign_type___(type)
    @type = type
  end

  # @return [Boolean]
  #
  # @api private
  # @since 0.2.0
  def valid?(value)
    (value == nil) ? true : validator.valid?(value)
  end

  # @param value [Any]
  # @return [SmartCore::Types::Primitive::Validator::Result]
  # @return [SmartCore::Types::Primitive::SumValidator::Result]
  # @return [SmartCore::Types::Primitive::MultValidator::Result]
  # @return [SmartCore::Types::Primitive::NilableValidator::Result]
  #
  # @api private
  # @since 0.2.0
  def validate(value)
    return Result.new(type, value) if value == nil
    validator.validate(value)
  end

  # @param value [Any]
  # @return [void]
  #
  # @raise [SmartCore::Types::TypeError]
  #
  # @api private
  # @since 0.2.0
  def validate!(value)
    return if valid?(value)
    raise(SmartCore::Types::TypeError, 'Invalid type')
  end

  private

  # @return [SmartCore::Types::Primitive]
  #
  # @api private
  # @since 0.2.0
  attr_reader :type

  # @return [
  #   SmartCore::Types::Primitive::Validator,
  #   SmartCore::Types::Primitive::SumValidator,
  #   SmartCore::Types::Primitive::MultValidator,
  #   SmartCore::Types::Primitive::NilableValidator
  # ]
  #
  # @api private
  # @since 0.2.0
  attr_reader :validator
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
smart_types-0.8.0 lib/smart_core/types/primitive/nilable_validator.rb