Sha256: 12b5e7310d4971a6ce3a7ae327d3169d4f25a4ab3a1653d3251c71c00e218962

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

module Vedeu
  class Events
    def initialize(&block)
      @handlers = Hash.new do |hash, key|
        hash[key] = {
          delay:     0,
          events:    [],
          last_exec: 0,
        }
      end
      self.instance_eval(&block) if block_given?
      self
    end

    def add(object, &block)
      @self_before_instance_eval = eval 'self', block.binding

      self.instance_eval(&block)
    end

    def on(event, delay = 0, &block)
      handlers[event][:events] << block
      handlers[event][:delay]  = delay
      handlers[event]
    end

    def trigger(event, *args)
      elapsed = Time.now.to_f - handlers[event][:last_exec]

      if elapsed > handlers[event][:delay]
        handlers[event][:last_exec] = Time.now.to_f

        handlers[event][:events].each do |handler|
          handler.call(*args)
        end
      end
    end

    # :nocov:
    def method_missing(method, *args, &block)
      @self_before_instance_eval.send method, *args, &block
    end
    # :nocov:

    private

    attr_reader :handlers, :throttles
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vedeu-0.1.2 lib/vedeu/support/events.rb