Sha256: 61403358e830dbdd6faf799affc034ca25b043fb780321eeadf7722825ccf729

Contents?: true

Size: 817 Bytes

Versions: 2

Compression:

Stored size: 817 Bytes

Contents

module Boty
  module Eventable
    include Boty::Logger
    def events
      @events ||= {}
    end

    def event(data)
      type = event_type data
      return unless type

      events[type].each do |action|
        action.call data
      end
    end

    def on(event_type, &block)
      events[event_type.to_sym] ||= []
      events[event_type.to_sym] << block
    end

    def off(event_type, &block)
      if block_given?
        events[event_type].delete_if { |b| b == block }
      else
        events[event_type] = []
      end
    end

    private

    def event_type(data)
      type = data["type"].to_sym
      logger.debug do "event[#{type}] arrived: #{data}" end
      unless events[type]
        logger.debug do "no action binded to #{type}" end
        return
      end
      type
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
boty-0.1.2 lib/boty/eventable.rb
boty-0.1.1 lib/boty/eventable.rb