-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtype.rb
More file actions
35 lines (28 loc) · 775 Bytes
/
Copy pathtype.rb
File metadata and controls
35 lines (28 loc) · 775 Bytes
1
2
3
4
5
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
32
33
34
35
class Type
@@types = []
attr_accessor :sym, :klass, :condition
private_class_method :new
def initialize(sym, klass, &condition)
@sym, @klass, @condition = sym, klass, condition
@@types << self
end
def match?(val)
(@sym == :void || val != nil) && (@klass === val) && (!@condition || @condition.(val))
end
class << self
def define_new(sym, klass, &condition)
Module.class_eval do
define_method(sym) do |meth|
define_typed_method(meth, Type.find_or_create(sym, klass, &condition))
end
private sym
end
end
def find(sym)
@@types.find { |type| type.sym == sym }
end
def find_or_create(sym, klass, &condition)
find(sym) || new(sym, klass, &condition)
end
end
end