Sha256: 31c6ce33f04546a1b7e4e26f1196c6eca11a9ef2be3e674489f8e374bd095ffb

Contents?: true

Size: 1.09 KB

Versions: 20

Compression:

Stored size: 1.09 KB

Contents

module Isolate

  # A simple way to watch and extend the Isolate lifecycle.
  #
  #    Isolate::Events.watch Isolate::Sandbox, :initialized do |sandbox|
  #      puts "A sandbox just got initialized: #{sandbox}"
  #    end
  #
  # Read the source for Isolate::Sandbox and Isolate::Entry to see
  # what sort of events are fired.

  module Events

    # Watch for an event called +name+ from an instance of
    # +klass+. +block+ will be called when the event occurs. Block
    # args vary by event, but usually an instance of the relevant
    # class is passed.

    def self.watch klass, name, &block
      watchers[[klass, name]] << block
    end

    def self.fire klass, name, *args #:nodoc:
      watchers[[klass, name]].each do |block|
        block[*args]
      end
    end

    def self.watchers #:nodoc:
      @watchers ||= Hash.new { |h, k| h[k] = [] }
    end

    def fire name, after = nil, *args, &block #:nodoc:
      Isolate::Events.fire self.class, name, self, *args

      if after && block_given?
        yield self
        Isolate::Events.fire self.class, after, *args
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
isolate-3.5.1 lib/isolate/events.rb
isolate-3.5.0 lib/isolate/events.rb
isolate-3.4.0 lib/isolate/events.rb
isolate-3.3.1 lib/isolate/events.rb
isolate-3.2.4 lib/isolate/events.rb
isolate-3.2.2 lib/isolate/events.rb
isolate-3.2.1 lib/isolate/events.rb
isolate-3.2.0 lib/isolate/events.rb
isolate-3.1.2 lib/isolate/events.rb
isolate-3.1.1 lib/isolate/events.rb
isolate-3.1.0 lib/isolate/events.rb
isolate-3.0.2 lib/isolate/events.rb
isolate-3.0.1 lib/isolate/events.rb
isolate-3.1.0.pre.3 lib/isolate/events.rb
isolate-3.1.0.pre.2 lib/isolate/events.rb
isolate-3.1.0.pre.1 lib/isolate/events.rb
isolate-3.0.0 lib/isolate/events.rb
isolate-2.1.2 lib/isolate/events.rb
isolate-2.1.1 lib/isolate/events.rb
isolate-2.1.0 lib/isolate/events.rb