Class: TrickySignals
- Inherits:
-
Object
- Object
- TrickySignals
- Includes:
- MonitorMixin
- Defined in:
- lib/tricky_signals.rb,
lib/tricky_signals/global.rb,
lib/tricky_signals/version.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Global
Constant Summary collapse
- VERSION =
"0.2.0"
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize ⇒ TrickySignals
constructor
A new instance of TrickySignals.
- #start! ⇒ Object
- #started? ⇒ Boolean
- #stop! ⇒ Object
- #trap(signal, command = nil) ⇒ Object
- #untrap(signal) ⇒ Object
- #untrap_all ⇒ Object
Constructor Details
#initialize ⇒ TrickySignals
Returns a new instance of TrickySignals.
19 20 21 22 |
# File 'lib/tricky_signals.rb', line 19 def initialize super @started = false end |
Class Method Details
.global ⇒ Object
6 7 8 |
# File 'lib/tricky_signals/global.rb', line 6 def global Global.instance end |
.start! ⇒ Object
6 7 8 9 10 11 12 13 14 |
# File 'lib/tricky_signals.rb', line 6 def start! obj = new.start! if block_given? yield obj obj.stop! else obj end end |
Instance Method Details
#start! ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/tricky_signals.rb', line 24 def start! synchronize do fail "cannot start #{self}: already started!" if @started @started = true @handlers = {} @previous = {} @io_thread = start_io self end end |
#started? ⇒ Boolean
51 52 53 |
# File 'lib/tricky_signals.rb', line 51 def started? @started end |
#stop! ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/tricky_signals.rb', line 36 def stop! synchronize do fail "cannot stop #{self}: not started or stopped" unless @started untrap_all stop_io @previous = nil @handlers = nil @started = false nil end end |
#trap(signal, command = nil) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/tricky_signals.rb', line 55 def trap(signal, command = nil) synchronize do check_started! signal = stringify_signal(signal) prev_handler = if block_given? trap_with_block(signal, &proc) else trap_with_command(signal, command) end @previous[signal] = prev_handler unless @previous.key? signal prev_handler end end |
#untrap(signal) ⇒ Object
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/tricky_signals.rb', line 70 def untrap(signal) synchronize do check_started! signal = stringify_signal(signal) if @previous.key? signal trap_with_command(signal, @previous[signal]) @previous.delete signal end end end |
#untrap_all ⇒ Object
81 82 83 84 85 86 |
# File 'lib/tricky_signals.rb', line 81 def untrap_all synchronize do check_started! @previous.keys.each { |signal| untrap signal } end end |