Class: SmartCore::Types::Primitive::SumValidator Private

Inherits:
Object
  • Object
show all
Defined in:
lib/smart_core/types/primitive/sum_validator.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Since:

  • 0.2.0

Version:

  • 0.3.0

Direct Known Subclasses

MultValidator

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Constructor Details

#initialize(*validators) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

Since:

  • 0.2.0



14
15
16
17
# File 'lib/smart_core/types/primitive/sum_validator.rb', line 14

def initialize(*validators)
  @type = nil
  @validators = validators
end

Instance Method Details

#___assign_type___(type) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Parameters:

Since:

  • 0.2.0



35
36
37
# File 'lib/smart_core/types/primitive/sum_validator.rb', line 35

def ___assign_type___(type)
  @type = type
end

#___copy_for___(type) ⇒ SmartCore::Types::Primitive::SumValidator

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

Returns:

Since:

  • 0.3.0



24
25
26
27
28
# File 'lib/smart_core/types/primitive/sum_validator.rb', line 24

def ___copy_for___(type)
  self.class.new(type_checker, invariant_control).tap do |instance_copy|
    instance_copy.___assign_type___(type)
  end
end

#valid?(value) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)

Since:

  • 0.2.0

Version:

  • 0.3.0



44
45
46
47
48
49
50
51
# File 'lib/smart_core/types/primitive/sum_validator.rb', line 44

def valid?(value)
  # NOTE: at this moment type sum does not support invariant checking
  # TODO (0.x.0):
  #   validators.any? { |validator| validator.valid?(value) }
  validators.any? do |validator|
    validator.type_checker.call(value, type.runtime_attributes)
  end
end

#validate(value) ⇒ SmartCore::Types::Primitive::SumValidator::Result

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • value (Any)

Returns:

Since:

  • 0.2.0

Version:

  • 0.3.0



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/smart_core/types/primitive/sum_validator.rb', line 59

def validate(value)
  compile_validation_result do
    SmartCore::Engine::Atom.new.tap do |result|
      validators.each do |validator|
        # NOTE: at this moment type sum does not support invariant checking
        # TODO (0.x.0):
        #   result.swap { validator.validate(value) }
        #   break if result.value.success?
        result.swap { validator.type_checker.call(value, type.runtime_attributes) }
        break if result.value # => boolean
      end
    end
  end
end

#validate!(value) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Parameters:

  • value (Any)

Raises:

Since:

  • 0.2.0



81
82
83
84
# File 'lib/smart_core/types/primitive/sum_validator.rb', line 81

def validate!(value)
  return if valid?(value)
  raise(SmartCore::Types::TypeError, 'Invalid type')
end