Module: Util
Instance Method Summary collapse
- #absolute_path_for(*args) ⇒ Object
- #absolute_prefix_for(*args) ⇒ Object
- #columnize(buf, opts = {}) ⇒ Object
- #getopt(opt, hash, default = nil) ⇒ Object
- #hostname ⇒ Object
- #indent(chunk, n = 2) ⇒ Object
- #normalize_path(arg, *args) ⇒ Object
- #path_for(*args) ⇒ Object
- #paths_for(*args) ⇒ Object
- #pid ⇒ Object
- #ppid ⇒ Object
- #prefix_for(*args) ⇒ Object
- #thread_id ⇒ Object
- #tmpdir(*args, &block) ⇒ Object
- #unindent(chunk) ⇒ Object
Instance Method Details
#absolute_path_for(*args) ⇒ Object
60 61 62 |
# File 'lib/fbomb/util.rb', line 60 def absolute_path_for(*args) ('/' + paths_for(*args).join('/')).squeeze('/') end |
#absolute_prefix_for(*args) ⇒ Object
64 65 66 |
# File 'lib/fbomb/util.rb', line 64 def absolute_prefix_for(*args) absolute_path_for(*args) + '/' end |
#columnize(buf, opts = {}) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/fbomb/util.rb', line 122 def columnize(buf, opts = {}) width = Util.getopt 'width', opts, 80 indent = Util.getopt 'indent', opts indent = Fixnum === indent ? (' ' * indent) : "#{ indent }" column = [] words = buf.split %r/\s+/o row = "#{ indent }" while((word = words.shift)) if((row.size + word.size) < (width - 1)) row << word else column << row row = "#{ indent }" row << word end row << ' ' unless row.size == (width - 1) end column << row unless row.strip.empty? column.join "\n" end |
#getopt(opt, hash, default = nil) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/fbomb/util.rb', line 143 def getopt(opt, hash, default = nil) keys = opt.respond_to?('each') ? opt : [opt] keys.each do |key| return hash[key] if hash.has_key? key key = "#{ key }" return hash[key] if hash.has_key? key key = key.intern return hash[key] if hash.has_key? key end return default end |
#hostname ⇒ Object
2 3 4 |
# File 'lib/fbomb/util.rb', line 2 def hostname @hostname ||= (Socket.gethostname rescue 'localhost') end |
#indent(chunk, n = 2) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/fbomb/util.rb', line 96 def indent(chunk, n = 2) lines = chunk.split %r/\n/ re = nil s = ' ' * n lines.map! do |line| unless re margin = line[%r/^\s*/] re = %r/^#{ margin }/ end line.gsub re, s end.join("\n") end |
#normalize_path(arg, *args) ⇒ Object
76 77 78 |
# File 'lib/fbomb/util.rb', line 76 def normalize_path(arg, *args) absolute_path_for(arg, *args) end |
#path_for(*args) ⇒ Object
68 69 70 |
# File 'lib/fbomb/util.rb', line 68 def path_for(*args) paths_for(*args).join('/').squeeze('/') end |
#paths_for(*args) ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/fbomb/util.rb', line 51 def paths_for(*args) path = args.flatten.compact.join('/') path.gsub!(%r|[.]+/|, '/') path.squeeze!('/') path.sub!(%r|^/|, '') path.sub!(%r|/$|, '') paths = path.split('/') end |
#pid ⇒ Object
6 7 8 |
# File 'lib/fbomb/util.rb', line 6 def pid @pid ||= Process.pid end |
#ppid ⇒ Object
10 11 12 |
# File 'lib/fbomb/util.rb', line 10 def ppid @ppid ||= Process.ppid end |
#prefix_for(*args) ⇒ Object
72 73 74 |
# File 'lib/fbomb/util.rb', line 72 def prefix_for(*args) path_for(*args) + '/' end |
#thread_id ⇒ Object
14 15 16 |
# File 'lib/fbomb/util.rb', line 14 def thread_id Thread.current.object_id.abs end |
#tmpdir(*args, &block) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/fbomb/util.rb', line 18 def tmpdir(*args, &block) = Hash === args.last ? args.pop : {} dirname = Dir.tmpdir return dirname unless block turd = ['turd'] || [:turd] basename = [ hostname, ppid, pid, thread_id, rand ].join('-') made = false 42.times do |n| pathname = File.join(dirname, "#{ basename }-n=#{ n }") begin FileUtils.mkdir_p(pathname) made = true return Dir.chdir(pathname, &block) rescue Object sleep(rand) :retry ensure unless turd FileUtils.rm_rf(pathname) if made end end end raise "failed to make tmpdir in #{ dirname.inspect }" end |
#unindent(chunk) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/fbomb/util.rb', line 109 def unindent(chunk) lines = chunk.split %r/\n/ indent = nil re = %r/^/ lines.map! do |line| unless indent indent = line[%r/^\s*/] re = %r/^#{ indent }/ end line.gsub re, '' end.join("\n") end |