Sha256: f3886da409f6d0590a270cd31b58b1adb621285d5af16ad6d3bbf624df136a5e

Contents?: true

Size: 818 Bytes

Versions: 3

Compression:

Stored size: 818 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

3 entries across 3 versions & 1 rubygems

Version Path
boty-1.0.1 lib/boty/eventable.rb
boty-1.0.0 lib/boty/eventable.rb
boty-0.2.0 lib/boty/eventable.rb