Class: SmartCore::Types::Primitive::Validator Private

Inherits:
Object
  • Object
show all
Defined in:
lib/smart_core/types/primitive/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

Defined Under Namespace

Classes: Result

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type_checker, invariant_control) ⇒ 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



33
34
35
36
37
# File 'lib/smart_core/types/primitive/validator.rb', line 33

def initialize(type_checker, invariant_control)
  @type = nil
  @type_checker = type_checker
  @invariant_control = invariant_control
end

Instance Attribute Details

#invariant_controlSmartCore::Types::Primitive::InvariantControl (readonly)

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.



25
26
27
# File 'lib/smart_core/types/primitive/validator.rb', line 25

def invariant_control
  @invariant_control
end

#typeSmartCore::Type::Primitive (readonly)

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:

  • (SmartCore::Type::Primitive)

Since:

  • 0.2.0



13
14
15
# File 'lib/smart_core/types/primitive/validator.rb', line 13

def type
  @type
end

#type_checkerSmartCore::Types::Primitive::Checker (readonly)

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:

Since:

  • 0.2.0



19
20
21
# File 'lib/smart_core/types/primitive/validator.rb', line 19

def type_checker
  @type_checker
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



55
56
57
# File 'lib/smart_core/types/primitive/validator.rb', line 55

def ___assign_type___(type)
  @type = type
end

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

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



44
45
46
47
48
# File 'lib/smart_core/types/primitive/validator.rb', line 44

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.

Parameters:

  • value (Any)

Returns:

  • (Boolean)

Since:

  • 0.2.0



64
65
66
# File 'lib/smart_core/types/primitive/validator.rb', line 64

def valid?(value)
  validate(value).success?
end

#validate(value) ⇒ SmartCore::Types::Primitive::Validator::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



74
75
76
77
78
79
80
# File 'lib/smart_core/types/primitive/validator.rb', line 74

def validate(value)
  checker_result = type_checker.call(value, type.runtime_attributes) # => Boolean
  return Result.new(type, value, checker_result) unless checker_result
  invariant_result = invariant_control.check(value, type.runtime_attributes)
  invariant_errors = invariant_result.invariant_errors.map { |error| "#{type.name}.#{error}" }
  Result.new(type, value, checker_result, invariant_errors)
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



89
90
91
92
# File 'lib/smart_core/types/primitive/validator.rb', line 89

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