Class: Sunshine::TrapStack

Inherits:
Object
  • Object
show all
Defined in:
lib/sunshine/trap_stack.rb

Overview

The TrapStack class handles setting multiple trap blocks as a stack. Once a trap block is triggered, it gets popped off the stack.

Class Method Summary collapse

Class Method Details

.add_trap(desc = nil, &block) ⇒ Object

Adds an INT signal trap with its description on the stack. Returns a trap_item Array.



13
14
15
16
# File 'lib/sunshine/trap_stack.rb', line 13

def self.add_trap desc=nil, &block
  @trap_stack.unshift [desc, block]
  @trap_stack.first
end

.call_trap(trap_item) {|msg| ... } ⇒ Object

Call a trap item and display it’s message.

Yields:

  • (msg)


23
24
25
26
27
28
29
30
31
# File 'lib/sunshine/trap_stack.rb', line 23

def self.call_trap trap_item
  return unless trap_item

  msg, trap_block = trap_item

  yield msg if block_given?

  trap_block.call
end

.delete_trap(trap_item) ⇒ Object

Remove a trap_item from the stack.



37
38
39
# File 'lib/sunshine/trap_stack.rb', line 37

def self.delete_trap trap_item
  @trap_stack.delete trap_item
end

.trap_signal(sig, &block) ⇒ Object

Sets the default trap.



45
46
47
48
49
50
51
52
# File 'lib/sunshine/trap_stack.rb', line 45

def self.trap_signal sig, &block
  @trap_stack = []

  trap sig do
    call_trap @trap_stack.shift, &block
    exit 1
  end
end