Sha256: 41ebb93be2065f5946d0c84039a94cc798710269f181ef6107b19119e64fbbb9
Contents?: true
Size: 1.09 KB
Versions: 1
Compression:
Stored size: 1.09 KB
Contents
module Vedeu class Events def initialize(&block) @handlers = Hash.new { |h, k| h[k] = [] } log("self: #{self.object_id}") self.instance_eval(&block) if block_given? self end def add(object, &block) log("self: #{self.object_id}") log("object: #{object.object_id}") @self_before_instance_eval = eval 'self', block.binding self.instance_eval(&block) end def on(event, &block) log("self: #{self.object_id}") log("block: #{block.object_id}") log("storing: #{event.inspect}") handlers[event] << block end def trigger(event, *args) handlers[event].each do |handler| log("self: #{self.object_id}") log("handler: #{handler.object_id}") log("running: #{event.inspect}") handler.call(*args) end end # :nocov: def method_missing(method, *args, &block) @self_before_instance_eval.send method, *args, &block end # :nocov: private attr_reader :handlers def log(message) Vedeu.log(sprintf("%42s%s", '', message)) end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
vedeu-0.0.40 | lib/vedeu/support/events.rb |