Module: Spacialdb::Helpers

Included in:
Auth, Client, Client, Command, Command::Base
Defined in:
lib/spacialdb/helpers.rb

Instance Method Summary collapse

Instance Method Details

#askObject



20
21
22
# File 'lib/spacialdb/helpers.rb', line 20

def ask
  gets.strip
end

#confirm_command(db = db) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/spacialdb/helpers.rb', line 40

def confirm_command(db = db)
  raise(Spacialdb::Command::CommandFailed, "No db specified.\nSet it by adding --db <db name>") unless db

  confirmed_db = extract_option('--confirm', false)
  if confirmed_db
    unless confirmed_db == db
      raise(Spacialdb::Command::CommandFailed, "Confirmed db #{confirmed_db} did not match the selected db #{db}.")
    end
    return true
  else
    display
    display " !    WARNING: Potentially Destructive Action"
    display " !    This command will affect the db: #{db}"
    display " !    To proceed, type \"#{db}\" or re-run this command with --confirm #{db}"
    display
    display "> ", false
    if ask.downcase != db
      display " !    Input did not match #{db}. Aborted."
      false
    else
      true
    end
  end
end

#display(msg = "", newline = true) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/spacialdb/helpers.rb', line 6

def display(msg="", newline=true)
  if newline
    puts(msg)
  else
    print(msg)
    STDOUT.flush
  end
end

#echo_offObject



24
25
26
# File 'lib/spacialdb/helpers.rb', line 24

def echo_off
  system "stty -echo"
end

#echo_onObject



28
29
30
# File 'lib/spacialdb/helpers.rb', line 28

def echo_on
  system "stty echo"
end

#error(msg) ⇒ Object



15
16
17
18
# File 'lib/spacialdb/helpers.rb', line 15

def error(msg)
  STDERR.puts(msg)
  exit 1
end

#home_directoryObject



32
33
34
# File 'lib/spacialdb/helpers.rb', line 32

def home_directory
  ENV['HOME']
end

#json_decode(json) ⇒ Object



75
76
77
78
79
# File 'lib/spacialdb/helpers.rb', line 75

def json_decode(json)
  OkJson.decode(json)
rescue OkJson::ParserError
  nil
end

#json_encode(object) ⇒ Object



69
70
71
72
73
# File 'lib/spacialdb/helpers.rb', line 69

def json_encode(object)
  OkJson.encode(object)
rescue OkJson::ParserError
  nil
end

#longest(items) ⇒ Object



65
66
67
# File 'lib/spacialdb/helpers.rb', line 65

def longest(items)
  items.map { |i| i.to_s.length }.sort.last
end

#redisplay(line, line_break = false) ⇒ Object



36
37
38
# File 'lib/spacialdb/helpers.rb', line 36

def redisplay(line, line_break = false)
  display("\r\e[0K#{line}", line_break)
end