Sha256: 5955d58166781cf55a0902bf10d97262d3b7ddcba0e02cda50ca19a56f5c8113

Contents?: true

Size: 798 Bytes

Versions: 6

Compression:

Stored size: 798 Bytes

Contents

# You can define arbitrary events on objects that include this module
# It serves as storage for code to be executed later (when fire is
# called)
# For example:
# class Cursor < Lotu::Actor
#   def initialize
#     on(:someone_clicked) do
#       puts 'Someone clicked me!'
#     end
#   end
# end
#
# After that you will be able to call #fire(:someone_clicked) from any
# instance of class Cursor.
# If you pair this with input handling, you will get a nice event
# system. Check out the Cursor class to see it in action.

module Lotu
  module Eventful

    def init_behavior opts
      super if defined? super
      @events = {}
    end

    def on(event, &blk)
      @events[event] = blk
    end

    def fire(event, *args)
      @events[event].call(*args) if @events[event]
    end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
lotu-0.1.22 lib/lotu/behaviors/eventful.rb
lotu-0.1.21 lib/lotu/behaviors/eventful.rb
lotu-0.1.20 lib/lotu/behaviors/eventful.rb
lotu-0.1.19 lib/lotu/behaviors/eventful.rb
lotu-0.1.18 lib/lotu/behaviors/eventful.rb
lotu-0.1.16 lib/lotu/behaviors/eventful.rb