Sha256: b7abd232fc42a869217c71d576dc1da7b1db6a1512530dcfacc1724fde6e0fe6

Contents?: true

Size: 1.03 KB

Versions: 3

Compression:

Stored size: 1.03 KB

Contents

module Signal
  class << self
    alias trap_without_custom_handlers trap

    def handlers
      @handlers ||= {}
    end

    def trap(signal, &block)
      handlers[int_for_signal(signal)] = block
    end

    def handles?(signal)
      handlers.has_key? int_for_signal(signal)
    end

    def handle(signal)
      if handler = handlers[int_for_signal(signal)]
        handler.call
      end
    end

    protected

    def int_for_signal(signal)
      error_msg = "Check Signal.list for a list of valid signals."
      case signal
      when Numeric
        list.values.include?(signal) ? signal : raise(ArgumentError, "Invalid signal number. #{error_msg}")
      when String
        signal = signal.upcase
        list.keys.include?(signal) ? list[signal] : raise(ArgumentError, "Invalid signal identifier. #{error_msg}")
      else
        raise ArgumentError, error_msg
      end
    end
  end
end

module Kernel
  def self.trap(signal, &block)
    Signal.trap(signal, &block)
  end
end

def trap(signal, &block)
  Signal.trap(signal, &block)
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
social_url_stats-0.0.1 vendor/ruby/1.9.1/gems/ruby-fsevent-0.2.1/lib/fsevent/signal_ext.rb
ruby-fsevent-0.2.1 lib/fsevent/signal_ext.rb
ruby-fsevent-0.2.0 lib/fsevent/signal_ext.rb