Module: Util

Extended by:
Util
Included in:
Util
Defined in:
lib/fbomb/util.rb

Instance Method Summary collapse

Instance Method Details

#absolute_path_for(*args) ⇒ Object



68
69
70
# File 'lib/fbomb/util.rb', line 68

def absolute_path_for(*args)
  ('/' + paths_for(*args).join('/')).squeeze('/')
end

#absolute_prefix_for(*args) ⇒ Object



72
73
74
# File 'lib/fbomb/util.rb', line 72

def absolute_prefix_for(*args)
  absolute_path_for(*args) + '/'
end

#columnize(buf, opts = {}) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/fbomb/util.rb', line 130

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



151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/fbomb/util.rb', line 151

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

#hostnameObject



10
11
12
# File 'lib/fbomb/util.rb', line 10

def hostname
  @hostname ||= (Socket.gethostname rescue 'localhost')
end

#indent(chunk, n = 2) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/fbomb/util.rb', line 104

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



84
85
86
# File 'lib/fbomb/util.rb', line 84

def normalize_path(arg, *args)
  absolute_path_for(arg, *args)
end

#path_for(*args) ⇒ Object



76
77
78
# File 'lib/fbomb/util.rb', line 76

def path_for(*args)
  paths_for(*args).join('/').squeeze('/')
end

#paths_for(*args) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/fbomb/util.rb', line 59

def paths_for(*args)
  path = args.flatten.compact.join('/')
  path.gsub!(%r|[.]+/|, '/')
  path.squeeze!('/')
  path.sub!(%r|^/|, '')
  path.sub!(%r|/$|, '')
  paths = path.split('/')
end

#pidObject



14
15
16
# File 'lib/fbomb/util.rb', line 14

def pid
  @pid ||= Process.pid
end

#ppidObject



18
19
20
# File 'lib/fbomb/util.rb', line 18

def ppid
  @ppid ||= Process.ppid
end

#prefix_for(*args) ⇒ Object



80
81
82
# File 'lib/fbomb/util.rb', line 80

def prefix_for(*args)
  path_for(*args) + '/'
end

#thread_idObject



22
23
24
# File 'lib/fbomb/util.rb', line 22

def thread_id
  Thread.current.object_id.abs
end

#tmpdir(*args, &block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fbomb/util.rb', line 26

def tmpdir(*args, &block)
  options = Hash === args.last ? args.pop : {}

  dirname = Dir.tmpdir

  return dirname unless block

  turd = options['turd'] || options[: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



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/fbomb/util.rb', line 117

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

#which_rubyObject



2
3
4
5
6
7
8
# File 'lib/fbomb/util.rb', line 2

def which_ruby
  c                 = RbConfig::CONFIG
  bindir            = c["bindir"] || c['BINDIR']
  ruby_install_name = c['ruby_install_name'] || c['RUBY_INSTALL_NAME'] || 'ruby'
  ruby_ext          = c['EXEEXT'] || ''
  ruby              = File.join(bindir, (ruby_install_name + ruby_ext))
end