Skip to content

Commit eba6ccc

Browse files
committed
Caching Type obj
1 parent 54eb80c commit eba6ccc

1 file changed

Lines changed: 18 additions & 5 deletions

File tree

lib/java.rb

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,31 @@ module Boolean; end
5151
FalseClass.send(:include, Boolean)
5252

5353
class Type
54+
@@types = []
55+
5456
attr_accessor :sym, :klass, :condition
5557

5658
def initialize(sym, klass, &condition)
5759
@sym, @klass, @condition = sym, klass, condition
60+
@@types << self
5861
end
5962

60-
def self.define_new(sym, klass, &condition)
61-
Module.class_eval do
62-
define_method(sym) do |meth|
63-
define_typed_method(meth, Type.new(sym, klass, &condition))
63+
class << self
64+
def define_new(sym, klass, &condition)
65+
Module.class_eval do
66+
define_method(sym) do |meth|
67+
define_typed_method(meth, Type.find_or_create(sym, klass, &condition))
68+
end
69+
private sym
6470
end
65-
private sym
71+
end
72+
73+
def find_type(sym)
74+
@@types.find { |type| type.sym == sym }
75+
end
76+
77+
def find_or_create(sym, klass, &condition)
78+
find_type(sym) || new(sym, klass, &condition)
6679
end
6780
end
6881
end

0 commit comments

Comments
 (0)