Module: Algebrick::TypeCheck

Included in:
DSL::TypeDefinitionScope, Matchers::Abstract, ParametrizedType, ProductConstructors::Abstract, Protocol, Serializer, Serializers2::AbstractConverter, Serializers::Abstract, Type, Value
Defined in:
lib/algebrick/type_check.rb

Overview

noinspection RubyInstanceMethodNamingConvention

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) error(value, message, types) (private)

Raises:

  • (TypeError)


53
54
55
56
# File 'lib/algebrick/type_check.rb', line 53

def self.error(value, message, types)
  raise TypeError,
        "Value (#{value.class}) '#{value}' #{message} any of: #{types.join('; ')}."
end

Instance Method Details

- (Object) Child!(value, *types)



45
46
47
48
49
# File 'lib/algebrick/type_check.rb', line 45

def Child!(value, *types)
  Child?(value, *types) or
      TypeCheck.error(value, 'is not child', types)
  value
end

- (Boolean) Child?(value, *types)

Returns:



40
41
42
43
# File 'lib/algebrick/type_check.rb', line 40

def Child?(value, *types)
  Type?(value, Class) &&
      types.any? { |t| value <= t }
end

- (Object) Match!(value, *types)



34
35
36
37
38
# File 'lib/algebrick/type_check.rb', line 34

def Match!(value, *types)
  Match?(value, *types) or
      TypeCheck.error(value, 'is not matching', types)
  value
end

- (Boolean) Match?(value, *types)

Returns:



30
31
32
# File 'lib/algebrick/type_check.rb', line 30

def Match?(value, *types)
  types.any? { |t| t === value }
end

- (Object) Type!(value, *types)



24
25
26
27
28
# File 'lib/algebrick/type_check.rb', line 24

def Type!(value, *types)
  Type?(value, *types) or
      TypeCheck.error(value, 'is not', types)
  value
end

- (Boolean) Type?(value, *types)

FIND: type checking of collections?

Returns:



20
21
22
# File 'lib/algebrick/type_check.rb', line 20

def Type?(value, *types)
  types.any? { |t| value.is_a? t }
end