Method: Qrb::TypeFactory#type

Defined in:
lib/qrb/support/type_factory.rb

#type(type, name = nil, &bl) ⇒ Object

Factory



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/qrb/support/type_factory.rb', line 6

def type(type, name = nil, &bl)
  return subtype(type(type, name), bl) if bl
  case type
  when Type
    type
  when Module
    BuiltinType.new(type, name || type.name.to_s)
  when Hash
    tuple(type, name)
  when Array
    fail!("Array of arity 1 expected, got `#{type}`") unless type.size==1
    seq(type.first, name)
  when Set
    fail!("Set of arity 1 expected, got `#{type}`") unless type.size==1
    sub = type(type.first)
    sub.is_a?(TupleType) ? relation(sub.heading, name) : set(sub, name)
  when Range
    clazz = [type.begin, type.end].map(&:class).uniq
    fail!("Unsupported range `#{type}`") unless clazz.size==1
    subtype(clazz.first, type)
  when Regexp
    subtype(String, type)
  else
    fail!("Unable to factor a Qrb::Type from `#{type}`")
  end
end