Class: Dialekt::RubyTypeChecker

Inherits:
BasicTypeChecker show all
Includes:
Singleton
Defined in:
lib/dialekt/ruby_type_checker.rb

Overview

Ruby type checker

Instance Method Summary collapse

Methods inherited from BasicTypeChecker

#check!, #format

Instance Method Details

#union_type(types:) ⇒ Object

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
# File 'lib/dialekt/ruby_type_checker.rb', line 10

def union_type(types:)
  union_type = Set.new(types.flatten.uniq)

  raise ArgumentError, "Types must not be empty" if union_type.empty?

  union_type.size == 1 ? union_type.first : union_type
end

#valid?(type:, value:) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
26
27
# File 'lib/dialekt/ruby_type_checker.rb', line 18

def valid?(type:, value:)
  case type
  when Array, Set
    type.any? { |t| valid?(type: t, value: value) }
  when Class
    value.is_a?(type)
  else
    raise TypeError, "Illegal type '#{type}'"
  end
end