From 42e4923c8655fe9f9bd685c6a23a8e6d1ccbe541 Mon Sep 17 00:00:00 2001 From: jonisavo Date: Sat, 5 Sep 2020 20:53:48 +0300 Subject: [PATCH 1/3] HandlerHash#add can now take a block --- Data/Scripts/006_Events and files/002_EventHandlers.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Data/Scripts/006_Events and files/002_EventHandlers.rb b/Data/Scripts/006_Events and files/002_EventHandlers.rb index 101fc4c12..f04a8e5fe 100644 --- a/Data/Scripts/006_Events and files/002_EventHandlers.rb +++ b/Data/Scripts/006_Events and files/002_EventHandlers.rb @@ -99,11 +99,14 @@ class HandlerHash @addIfs.push([condProc,handler]) end - def add(sym,handler) # 'sym' can be an ID or symbol + def add(sym,handler=nil,&handler_block) # 'sym' can be an ID or symbol + if ![Proc,Hash].include?(handler.class) && !block_given? + raise ArgumentError, "#{self.class.name} for #{sym.inspect} has no valid handler" + end id = fromSymbol(sym) - @hash[id] = handler if id + @hash[id] = handler || handler_block if id symbol = toSymbol(sym) - @hash[symbol] = handler if symbol + @hash[symbol] = handler || handler_block if symbol end def copy(src,*dests) From 069b2098516e054d279e405860cc4779b5e68f91 Mon Sep 17 00:00:00 2001 From: jonisavo Date: Sat, 5 Sep 2020 21:24:02 +0300 Subject: [PATCH 2/3] Better error message for the #add method, and renamed handler_block to handlerBlock --- Data/Scripts/006_Events and files/002_EventHandlers.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Data/Scripts/006_Events and files/002_EventHandlers.rb b/Data/Scripts/006_Events and files/002_EventHandlers.rb index f04a8e5fe..32452638f 100644 --- a/Data/Scripts/006_Events and files/002_EventHandlers.rb +++ b/Data/Scripts/006_Events and files/002_EventHandlers.rb @@ -99,14 +99,14 @@ class HandlerHash @addIfs.push([condProc,handler]) end - def add(sym,handler=nil,&handler_block) # 'sym' can be an ID or symbol + def add(sym,handler=nil,&handlerBlock) # 'sym' can be an ID or symbol if ![Proc,Hash].include?(handler.class) && !block_given? - raise ArgumentError, "#{self.class.name} for #{sym.inspect} has no valid handler" + raise ArgumentError, "#{self.class.name} for #{sym.inspect} has no valid handler (#{handler.inspect} was given)" end id = fromSymbol(sym) - @hash[id] = handler || handler_block if id + @hash[id] = handler || handlerBlock if id symbol = toSymbol(sym) - @hash[symbol] = handler || handler_block if symbol + @hash[symbol] = handler || handlerBlock if symbol end def copy(src,*dests) From 4eb705b8011b3617d17235b9a1c627d5f0d6b60d Mon Sep 17 00:00:00 2001 From: jonisavo Date: Sun, 6 Sep 2020 07:01:27 +0300 Subject: [PATCH 3/3] Modified HandlerHash#addIf to accept blocks --- Data/Scripts/006_Events and files/002_EventHandlers.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Data/Scripts/006_Events and files/002_EventHandlers.rb b/Data/Scripts/006_Events and files/002_EventHandlers.rb index 32452638f..009f660bf 100644 --- a/Data/Scripts/006_Events and files/002_EventHandlers.rb +++ b/Data/Scripts/006_Events and files/002_EventHandlers.rb @@ -95,11 +95,14 @@ class HandlerHash return ret end - def addIf(condProc,handler) - @addIfs.push([condProc,handler]) + def addIf(conditionProc,handler=nil,&handlerBlock) + if ![Proc,Hash].include?(handler.class) && !block_given? + raise ArgumentError, "addIf call for #{self.class.name} has no valid handler (#{handler.inspect} was given)" + end + @addIfs.push([conditionProc,handler || handlerBlock]) end - def add(sym,handler=nil,&handlerBlock) # 'sym' can be an ID or symbol + def add(sym,handler=nil,&handlerBlock) # 'sym' can be an ID or symbol if ![Proc,Hash].include?(handler.class) && !block_given? raise ArgumentError, "#{self.class.name} for #{sym.inspect} has no valid handler (#{handler.inspect} was given)" end