Sha256: 926754b6b8c4eb113baa7af7410e7fe98325a8376a47416d0586129d4d47de7f

Contents?: true

Size: 1.6 KB

Versions: 25

Compression:

Stored size: 1.6 KB

Contents

# frozen_string_literal: true

#
# Copyright 2013 whiteleaf. All rights reserved.
#

module Narou::Eventable
  EVENTS_CONTAINER = {}

  def self.included(base)
    super
    base.extend(ClassMethods)
    EVENTS_CONTAINER[base] = {}
  end

  class NonBlockError < StandardError; end

  module ClassMethods
    def add_event_listener(event_name, &block)
      raise NonBlockError unless block_given?
      stack = EVENTS_CONTAINER[self][event_name] ||= []
      stack.push(block)
    end

    def trigger_event(event_name, *argv)
      stack = EVENTS_CONTAINER[self][event_name]
      return unless stack
      stack.each do |block|
        block.call(*argv)
      end
    end
  end

  def add_event_listener(event_name, once = false, &block)
    raise NonBlockError unless block_given?
    @__events_container ||= {}
    stack = @__events_container[event_name] ||= []
    stack.push([block, once])
  end

  def remove_event_listener(event_name, &block)
    @__events_container ||= {}
    if block_given?
      events = @__events_container[event_name]
      if events
        events.reject! do |values|
          values[0] == block
        end
      end
    else
      @__events_container.delete(event_name)
    end
  end

  def trigger_event(event_name, *argv)
    @__events_container ||= {}
    stack = @__events_container[event_name]
    return unless stack
    stack.reject! do |block, once|
      block.call(*argv)
      once
    end
  end

  alias :on :add_event_listener
  alias :off :remove_event_listener
  alias :trigger :trigger_event

  def one(event_name, &block)
    add_event_listener(event_name, true, &block)
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
narou-3.9.1 lib/eventable.rb
narou-3.9.0 lib/eventable.rb
narou-3.8.2 lib/eventable.rb
narou-3.8.1 lib/eventable.rb
narou-3.8.0 lib/eventable.rb
narou-3.7.2 lib/eventable.rb
narou-3.7.1 lib/eventable.rb
narou-3.7.0 lib/eventable.rb
narou-3.6.0 lib/eventable.rb
narou-3.5.1 lib/eventable.rb
narou-3.5.0.1 lib/eventable.rb
narou-3.5.0 lib/eventable.rb
narou-3.4.8 lib/eventable.rb
narou-3.4.7.1 lib/eventable.rb
narou-3.4.7 lib/eventable.rb
narou-3.4.6.1 lib/eventable.rb
narou-3.4.6 lib/eventable.rb
narou-3.4.5 lib/eventable.rb
narou-3.4.3 lib/eventable.rb
narou-3.4.2 lib/eventable.rb