Module: MiscHacks
- Defined in:
- lib/mischacks.rb,
lib/mischacks.rb
Overview
mischacks – Miscellaneous methods that may or may not be useful Copyright © 2009 Johan Kiviniemi
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Defined Under Namespace
Modules: ExceptionMixin
Classes: ChildError
Constant Summary
collapse
- VERSION =
'0.0.3'
Class Method Summary
collapse
Class Method Details
.catching_exit(final_proc, fallthrough_status) ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/mischacks.rb', line 39
def self.catching_exit final_proc, fallthrough_status
status = fallthrough_status
begin
yield
rescue SystemExit => e
status = e.status
ensure
final_proc.call status
end
status
end
|
.do_and_exit(status = 1, &block) ⇒ Object
53
54
55
|
# File 'lib/mischacks.rb', line 53
def self.do_and_exit status=1, &block
catching_exit method(:exit), status, &block
end
|
.do_and_exit!(status = 1, &block) ⇒ Object
57
58
59
|
# File 'lib/mischacks.rb', line 57
def self.do_and_exit! status=1, &block
catching_exit method(:exit!), status, &block
end
|
.fork_and_check ⇒ Object
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/mischacks.rb', line 28
def self.fork_and_check
fork do
yield
end.tap do |pid|
_, status = Process.wait2 pid
raise ChildError, status.exitstatus if status.exitstatus != 0
end
nil
end
|
.sh(cmd, *args) ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/mischacks.rb', line 61
def self.sh cmd, *args
env = if args.last.is_a? Hash then args.pop else {} end
fork_and_check do
do_and_exit! do
begin
env.each_pair do |k, v| ENV[k.to_s] = v.to_s end
exec *(%W{sh -e -c #{cmd} sh} + args.map {|a| a.to_s })
rescue Exception => e
warn e.to_formatted_string
end
end
end
nil
end
|