Sha256: 06b237fbfefda915fcea3487395342274f06f298c3cb3027fefcc7dabd7cb738
Contents?: true
Size: 1.62 KB
Versions: 1
Compression:
Stored size: 1.62 KB
Contents
require 'moosex' require 'moosex/types' module MooseX module Event class EventException < StandardError end class EventListener include MooseX has once: { is: :rw, reader: :once? } has event: { is: :ro, handles: { :respond_to_event? => :== } } has code: { is: :ro, handles: :call } end include MooseX include MooseX::Types has listeners: { is: :private, isa: isArray(EventListener), default: lambda{ [] }, } def has_events nil end def on(event, &block) add_event_listener(event, false, block) end def once(event, &block) add_event_listener(event, true, block) end def emit(event, *args) listeners.select{|l| l.respond_to_event?(event) }.each{|l| l.call(self, *args) } listeners.delete_if {|l| l.once? } end def remove_all_listeners(event) listeners.delete_if {|l| l.respond_to_event?(event) } end def remove_listener(data) data.each_pair do |event, listener| listeners.delete(listener) end end private def add_event_listener(event, once, code) listener = EventListener.new(event: event, once: once, code: code) listeners << listener listener end before(:add_event_listener, :emit) do |obj, event, *rest| if ! obj.has_events.nil? && ! [ obj.has_events ].flatten.include?(event) raise EventException, "Event '#{event.inspect}' not supported, this class only allow: #{obj.has_events.inspect}",caller end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
moosex-0.0.15 | lib/moosex/event.rb |