Sha256: cd6d768d36592b4a729f26a4a51d06309a7863b8d7b879635adfe980e4a0e69e

Contents?: true

Size: 1.04 KB

Versions: 2

Compression:

Stored size: 1.04 KB

Contents

# Syntactic sugar for adding a listener. Use include or extend to
# add listen/subscribe behaviour to your class instance
# or class body.

# class MyListener
#     include Flirt::Listener
#
#     def initialize
#         subscribe_to :picked, with: :picked_callback
#         # or the alias
#         listen_to :picked, with: :picked_callback
#     end
#
#     def picked_callback(event_data)
#         puts "The #{event_data[:fruit]} has been picked"
#     end
# end
#
# or:
#
# class MyListener
#     extend Flirt::Listener
#
#     subscribe_to :picked, with: :picked_callback
#     # or the alias
#     listen_to :picked, with: :picked_callback
#
#     def self.picked_callback(event_data)
#         puts "The #{event_data[:fruit]} has been picked"
#     end
# end
#

module Flirt
    module Listener
        def subscribe_to(event_name, opts = {})
            raise ArgumentError.new("You must pass a callback") unless opts[:with].is_a? Symbol
            Flirt.subscribe self, event_name, opts
        end
        alias_method :listen_to, :subscribe_to
    end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
flirt-0.0.2 lib/flirt/listener.rb
flirt-0.0.1 lib/flirt/listener.rb