Sha256: acacb3773489834f9188ca375f903441f80fd91d57780302cb80a2982720d072

Contents?: true

Size: 891 Bytes

Versions: 2

Compression:

Stored size: 891 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 is_eventful
      include InstanceMethods
    end

    module InstanceMethods
      def init_behavior
        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
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lotu-0.1.1 lib/lotu/behaviors/eventful.rb
lotu-0.1.0 lib/lotu/behaviors/eventful.rb