Module: Gonzui::Util

Defined Under Namespace

Classes: AssertionFailed, CommandNotFoundError

Constant Summary collapse

@@verbosity =
false

Class Method Summary collapse

Class Method Details

.assert(bool) ⇒ Object

Raises:



368
369
370
# File 'lib/gonzui/util.rb', line 368

def assert(bool)
  raise AssertionFailed.new unless bool
end

.assert_equal(expected, value) ⇒ Object

Raises:



357
358
359
# File 'lib/gonzui/util.rb', line 357

def assert_equal(expected, value)
  raise AssertionFailed.new unless expected == value
end

.assert_equal_all(*values) ⇒ Object



361
362
363
364
365
366
# File 'lib/gonzui/util.rb', line 361

def assert_equal_all(*values)
  first = values.first
  unless values.all? {|value| first == value }
    raise AssertionFailed.new
  end
end

.assert_non_nil(object) ⇒ Object

Raises:



349
350
351
# File 'lib/gonzui/util.rb', line 349

def assert_non_nil(object)
  raise AssertionFailed.new if object.nil?
end

.assert_not_reachedObject

Raises:



353
354
355
# File 'lib/gonzui/util.rb', line 353

def assert_not_reached
  raise AssertionFailed.new
end

.benchmarkObject



318
319
320
321
322
323
324
325
326
# File 'lib/gonzui/util.rb', line 318

def benchmark
  result = nil
  Benchmark.bm {|x|
    x.report { 
      result = yield 
    }
  }
  return result
end

.command_exist?(command) ⇒ Boolean

Returns:

  • (Boolean)


249
250
251
252
253
254
255
256
257
# File 'lib/gonzui/util.rb', line 249

def command_exist?(command)
  paths = (ENV['PATH'] or "").split(File::PATH_SEPARATOR)
  paths.each {|path|
    return true if File.executable?(File.join(path, command))
		# windows
    return true if File.executable?(File.join(path, command + ".exe"))
  }
  return false
end

.commify(number) ⇒ Object



300
301
302
303
304
# File 'lib/gonzui/util.rb', line 300

def commify(number)
  numstr = number.to_s
  true while numstr.sub!(/^([-+]?\d+)(\d{3})/, '\1,\2')
  return numstr
end

.eprintf(format, *args) ⇒ Object



286
287
288
289
# File 'lib/gonzui/util.rb', line 286

def eprintf(format, *args)
  wprintf(format, *args)
  exit(1)
end

.format_bytes(bytes) ⇒ Object



306
307
308
309
310
311
312
313
314
315
316
# File 'lib/gonzui/util.rb', line 306

def format_bytes(bytes)
  if bytes < 1024
    sprintf("%dB", bytes)
  elsif bytes < 1024 * 1000 # 1000kb
    sprintf("%.1fKB", bytes.to_f / 1024)
  elsif bytes < 1024 * 1024 * 1000  # 1000mb
    sprintf("%.1fMB", bytes.to_f / 1024 / 1024)
  else
    sprintf("%.1fGB", bytes.to_f / 1024 / 1024 / 1024)
  end
end

.program_nameObject



237
238
239
# File 'lib/gonzui/util.rb', line 237

def program_name
  File.basename($0)
end

.protect_from_signalsObject



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/gonzui/util.rb', line 330

def protect_from_signals
  $protect_from_signals_mutex.synchronize {
    interrupted = false
    previous_handlers = {}
    signals = ["INT", "TERM"]

    signals.each {|signal|
      previous_handlers[signal] = trap(signal) { interrupted = true }
    }
    yield
    previous_handlers.each {|signal, handler|
      trap(signal, handler)
    }
    raise Interrupt if interrupted
  }
end

.require_command(command) ⇒ Object



260
261
262
263
264
# File 'lib/gonzui/util.rb', line 260

def require_command(command)
  raise CommandNotFoundError.new("#{command}: command not found") unless 
    command_exist?(command)
  return true
end

.set_verbosity(verbosity) ⇒ Object



292
293
294
# File 'lib/gonzui/util.rb', line 292

def set_verbosity(verbosity)
  @@verbosity = verbosity
end

.shell_escape(file_name) ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/gonzui/util.rb', line 266

def shell_escape(file_name)
  if (/mswin|mingw|bccwin/ =~ RUBY_PLATFORM)
    fn = file_name
    fn.gsub!(/["]/, "")
    if /^\w+:\/\// =~ fn
      # file://c/temp => file:///c:/temp
      fn.sub!(/^file:\/\/\/?(\w)\//, "file:///\\1:/")
    else
      fn.gsub!(/[\/]/, "\\")
    end
    '"' + fn + '"'
  else
    '"' + file_name.gsub(/([$"\\`])/, "\\\\\\1") + '"'
  end
end

.unix?Boolean

Returns:

  • (Boolean)


245
246
247
# File 'lib/gonzui/util.rb', line 245

def unix?
  not windows?
end

.vprintf(format, *args) ⇒ Object



296
297
298
# File 'lib/gonzui/util.rb', line 296

def vprintf(format, *args)
  printf(format + "\n", *args) if @@verbosity
end

.windows?Boolean

Returns:

  • (Boolean)


241
242
243
# File 'lib/gonzui/util.rb', line 241

def windows?
  /-(mswin32|cygwin|mingw|bccwin)/.match(RUBY_PLATFORM)
end

.wprintf(format, *args) ⇒ Object



282
283
284
# File 'lib/gonzui/util.rb', line 282

def wprintf(format, *args)
  STDERR.printf(program_name + ": " + format + "\n", *args)
end