rubywm/type_dispatcher.rb

29 lines
536 B
Ruby
Raw Normal View History

2024-01-21 01:05:37 +01:00
class TypeDispatcher
def initialize(target)
@target = target
@on = {}
end
def name_to_event(ob)
name = ob.to_s.split("::").last.split(/([A-Z][a-z]+)/).join("_").downcase
"on_#{name}".gsub(/__+/,"_").to_sym
end
def on(event, &block)
@on[name_to_event(event)] = block
end
def call(ob, *args)
sym = name_to_event(ob)
if @on[sym]
@on[sym].(*args)
elsif @target.respond_to?(sym)
arity = @target.method(sym).arity
@target.send(sym, *args[0...arity])
end
end
end