Sha256: 0ad25d7a4618e7950a70e2a9e7e4fc7691202dc1e05391072e326d829205056e

Contents?: true

Size: 1.49 KB

Versions: 25

Compression:

Stored size: 1.49 KB

Contents

module Spider
    
    module EventSource
        
        def self.included(klass)
            klass.extend(ClassMethods)
        end
        
        module ClassMethods
            
            def inherited(sub)
                if @event_handlers
                    @event_handlers.each do |event_name, arr|
                        arr.each do |proc|
                            sub.on(event_name, &proc)
                        end
                    end
                end
                super
            end
        
            def on(event_name, &proc)
                @event_handlers ||= {}
                @event_handlers[event_name] ||= []
                @event_handlers[event_name] << proc
            end
        
            def trigger(event_name, *params)
                return unless @event_handlers && @event_handlers[event_name]
                @event_handlers[event_name].each do |h|
                    h.call(*params)
                end
            end
            
        end
        
        def on(event_name, *params, &proc)
            @event_handlers ||= {}
            @event_handlers[event_name] ||= []
            @event_handlers[event_name] << proc
        end
        
        def trigger(event_name, *params)
            self.class.trigger(event_name, *([self]+params))
            return unless @event_handlers && @event_handlers[event_name]
            @event_handlers[event_name].each do |h|
                h.call(*params)
            end
        end
        
    end
    
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
spiderfw-0.6.21 lib/spiderfw/utils/events/event_source.rb
spiderfw-0.6.20 lib/spiderfw/utils/events/event_source.rb
spiderfw-0.6.19 lib/spiderfw/utils/events/event_source.rb
spiderfw-0.6.18 lib/spiderfw/utils/events/event_source.rb
spiderfw-0.6.17 lib/spiderfw/utils/events/event_source.rb