Module: DBGet::Utils

Included in:
Dump
Defined in:
lib/dbget/utils.rb

Class Method Summary collapse

Class Method Details

.compress_file(input_file) ⇒ Object



30
31
32
33
34
35
# File 'lib/dbget/utils.rb', line 30

def self.compress_file(input_file)
  command = "gzip #{input_file}"
  system command

  input_file.concat('.gz')
end

.decode_file(input_file) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/dbget/utils.rb', line 11

def self.decode_file(input_file)
  output_file = input_file.sub(/\.enc$/, "")
  pass_phrase = DBGet::Config.instance['openssl']['passphrase']

  command =  "openssl enc -d -aes-256-cbc -in #{input_file}"
  command += " -pass pass:#{pass_phrase}"
  command += " -out #{output_file}; rm #{input_file}"
  system command

  output_file
end

.decompress_file(input_file) ⇒ Object



23
24
25
26
27
28
# File 'lib/dbget/utils.rb', line 23

def self.decompress_file(input_file)
  command = "gzip -df #{input_file}"
  system command

  input_file.sub(/\.gz$/, "")
end

.get_converted_date(date) ⇒ Object



61
62
63
# File 'lib/dbget/utils.rb', line 61

def self.get_converted_date(date)
  date.to_s.delete('-')
end

.get_files(path) ⇒ Object



7
8
9
# File 'lib/dbget/utils.rb', line 7

def self.get_files(path)
  Dir.new(path).entries - %w{ . .. }
end

.randomize(size) ⇒ Object



50
51
52
53
# File 'lib/dbget/utils.rb', line 50

def self.randomize(size)
  chars = ('a'..'z').to_a + ('A'..'Z').to_a
  (0...size).collect { chars[Kernel.rand(chars.length)] }.join
end

.say(message, subitem = false) ⇒ Object



37
38
39
# File 'lib/dbget/utils.rb', line 37

def self.say(message, subitem = false)
  puts "#{subitem ? "   ->" : "--"} #{message}"
end

.say_with_time(message) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/dbget/utils.rb', line 41

def self.say_with_time(message)
  say(message)
  result = nil
  time = Benchmark.measure { result = yield }
  say "%.4fs" % time.real, :subitem
  say("#{result} rows", :subitem) if result.is_a?(Integer)
  result
end

.to_bool(string) ⇒ Object

Raises:

  • (ArgumentError)


55
56
57
58
59
# File 'lib/dbget/utils.rb', line 55

def self.to_bool(string)
  return true if string == true || string =~ (/(true|t|yes|y|1)$/i)
  return false if string == false || string.empty? || string =~ (/(false|f|no|n|0)$/i)
  raise ArgumentError.new("invalid value for Boolean: \"#{string}\"")
end