Class: Literal::Types::ConstraintType Private

Inherits:
Object
  • Object
show all
Includes:
Literal::Type
Defined in:
lib/literal/types/constraint_type.rb

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object_constraints, property_constraints) ⇒ ConstraintType

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 a new instance of ConstraintType.



7
8
9
10
11
# File 'lib/literal/types/constraint_type.rb', line 7

def initialize(object_constraints, property_constraints)
	@object_constraints = object_constraints
	@property_constraints = property_constraints
	freeze
end

Instance Attribute Details

#object_constraintsObject (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.



13
14
15
# File 'lib/literal/types/constraint_type.rb', line 13

def object_constraints
  @object_constraints
end

#property_constraintsObject (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.



14
15
16
# File 'lib/literal/types/constraint_type.rb', line 14

def property_constraints
  @property_constraints
end

Instance Method Details

#===(value) ⇒ Object

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.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/literal/types/constraint_type.rb', line 20

def ===(value)
	object_constraints = @object_constraints

	i, len = 0, object_constraints.size
	while i < len
		return false unless object_constraints[i] === value
		i += 1
	end

	result = true

	@property_constraints.each do |a, t|
		# We intentionally don’t return early here becuase it triggers an allocation.
		if result && !(t === value.public_send(a))
			result = false
		end
	rescue NoMethodError => e
		raise unless e.name == a && e.receiver == value
		return false
	end

	result
end

#>=(other) ⇒ Object

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.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/literal/types/constraint_type.rb', line 44

def >=(other)
	case other
	when Literal::Types::ConstraintType
		other_object_constraints = other.object_constraints
		return false unless @object_constraints.all? do |constraint|
			other_object_constraints.any? { |c| Literal.subtype?(c, constraint) }
		end

		other_property_constraints = other.property_constraints
		return false unless @property_constraints.all? do |k, v|
			Literal.subtype?(other_property_constraints[k], v)
		end

		true
	when Literal::Types::IntersectionType
		other_object_constraints = other.types
		return false unless @object_constraints.all? do |constraint|
			other_object_constraints.any? { |c| Literal.subtype?(c, constraint) }
		end

		true
	when Literal::Types::FrozenType
		@object_constraints.all? { |constraint| Literal.subtype?(other.type, constraint) }
	else
		false
	end
end

#inspectObject

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.



16
17
18
# File 'lib/literal/types/constraint_type.rb', line 16

def inspect
	"_Constraint(#{inspect_constraints})"
end

#record_literal_type_errors(context) ⇒ Object

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.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/literal/types/constraint_type.rb', line 72

def record_literal_type_errors(context)
	@object_constraints.each do |constraint|
		next if constraint === context.actual

		context.add_child(label: inspect, expected: constraint, actual: context.actual)
	end

	@property_constraints.each do |property, constraint|
		next unless context.actual.respond_to?(property)
		actual = context.actual.public_send(property)
		next if constraint === actual

		context.add_child(label: ".#{property}", expected: constraint, actual:)
	end
end