Sha256: 6174fbec2bf3f7eddc9ccfc30411fc8eea0fa96adc97eff6912e5f4e4dea489d

Contents?: true

Size: 1.51 KB

Versions: 23

Compression:

Stored size: 1.51 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)
            @event_handlers ||= {}
            @event_handlers[event_name] ||= []
            debugger unless proc
            @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

23 entries across 23 versions & 1 rubygems

Version Path
spiderfw-0.6.16 lib/spiderfw/utils/events/event_source.rb
spiderfw-0.6.15 lib/spiderfw/utils/events/event_source.rb
spiderfw-0.6.14 lib/spiderfw/utils/events/event_source.rb
spiderfw-0.6.13 lib/spiderfw/utils/events/event_source.rb
spiderfw-0.6.12 lib/spiderfw/utils/events/event_source.rb
spiderfw-0.6.11 lib/spiderfw/utils/events/event_source.rb
spiderfw-0.6.10 lib/spiderfw/utils/events/event_source.rb
spiderfw-0.6.9 lib/spiderfw/utils/events/event_source.rb
spiderfw-0.6.8 lib/spiderfw/utils/events/event_source.rb
spiderfw-0.6.7 lib/spiderfw/utils/events/event_source.rb
spiderfw-0.6.6 lib/spiderfw/utils/events/event_source.rb
spiderfw-0.6.5 lib/spiderfw/utils/events/event_source.rb
spiderfw-0.6.4 lib/spiderfw/utils/events/event_source.rb
spiderfw-0.6.3 lib/spiderfw/utils/events/event_source.rb
spiderfw-0.6.2 lib/spiderfw/utils/events/event_source.rb
spiderfw-0.6.1 lib/spiderfw/utils/events/event_source.rb
spiderfw-0.6.0 lib/spiderfw/utils/events/event_source.rb
spiderfw-0.5.19 lib/spiderfw/utils/events/event_source.rb
spiderfw-0.5.18 lib/spiderfw/utils/events/event_source.rb
spiderfw-0.5.17 lib/spiderfw/utils/events/event_source.rb