callback - Ruby method as block -
currently there class in rails application calls blocks event handlers (callbacks):
class myclass #event handler hooks: def on_event1(&block) @on_event1 = block end def on_event2(&block) @on_event2 = block end #event triggers: def do_event1 @on_event1.call if @on_event1 end def do_event2 @on_event2.call if @on_event2 end end
initializer:
mc = myclass.new #===== event handlers: ===== mc.on_event1 #do stuff end mc.on_event2 #do stuff end
i set event handlers separate class transforming them blocks methods:
class myclasshandlers def self.event1_handler #do stuff end def self.event2_handler #do stuff end end
how phone call method in place block should given?
i see event binding like:
mc = myclass.new mc.on_event1 = myclasshandlers.event1_handler mc.on_event2 = myclasshandlers.event2_handler
you utilize lamdas:
mc = myclass.new mc.on_event1 = lambda { myclasshandlers.event1_handler } mc.on_event2 = lambda { myclasshandlers.event2_handler }
ruby-on-rails callback event-handling
No comments:
Post a Comment