Module: Crabfarm::Utils::Console

Extended by:
Console
Included in:
Console
Defined in:
lib/crabfarm/utils/console.rb

Constant Summary collapse

COLOR_INFO =
'888888'
COLOR_QUESTION =
'888888'
COLOR_WARNING =
:yellow
COLOR_ERROR =
:red
COLOR_HIGHLIGHT =
'00FF00'

Instance Method Summary collapse

Instance Method Details

#backtrace(_exc) ⇒ Object



54
55
56
# File 'lib/crabfarm/utils/console.rb', line 54

def backtrace(_exc)
  _exc.backtrace.each { |i| info(i) }
end

#error(_message) ⇒ Object



45
46
47
# File 'lib/crabfarm/utils/console.rb', line 45

def error(_message)
  puts _message.color COLOR_ERROR
end

#exception(_exc) ⇒ Object



49
50
51
52
# File 'lib/crabfarm/utils/console.rb', line 49

def exception(_exc)
  error "#{_exc.class.to_s}: #{_exc.to_s}"
  backtrace _exc
end

#info(_message) ⇒ Object



25
26
27
# File 'lib/crabfarm/utils/console.rb', line 25

def info(_message)
  puts _message.color COLOR_INFO
end

#json_result(_data) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/crabfarm/utils/console.rb', line 33

def json_result(_data)
  if _data.nil?
    result 'nil'
  else
    result JSON.pretty_generate(_data).gsub(/(^|\\n)/, '  ')
  end
end

#operation(_message) ⇒ Object



21
22
23
# File 'lib/crabfarm/utils/console.rb', line 21

def operation(_message)
  puts _message
end

#question(_question) ⇒ Object



58
59
60
# File 'lib/crabfarm/utils/console.rb', line 58

def question(_question)
  Ask.input(_question.color COLOR_QUESTION)
end

#result(_message) ⇒ Object



29
30
31
# File 'lib/crabfarm/utils/console.rb', line 29

def result(_message)
  puts _message.color COLOR_HIGHLIGHT
end

#system(_message) ⇒ Object



17
18
19
# File 'lib/crabfarm/utils/console.rb', line 17

def system(_message)
  puts _message
end

#trap_errors(_context) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/crabfarm/utils/console.rb', line 62

def trap_errors(_context)
  begin
    yield
  rescue SystemExit, Interrupt
    raise
  rescue Exception => e
    error = "Error #{_context}"
    warning error + ', check log for more information'
    Crabfarm.logger.error error
    Crabfarm.logger.error e
  end
end

#warning(_message) ⇒ Object



41
42
43
# File 'lib/crabfarm/utils/console.rb', line 41

def warning(_message)
  puts _message.color COLOR_WARNING
end