class Pain::Cli

Attributes

model[R]
option_parser[R]
options[R]

Public Class Methods

new() click to toggle source
# File lib/pain/cli.rb, line 9
def initialize
  @options = {}
  @model = Pain::Model.new
  @option_parser = Options.create(@options, @model)
end

Public Instance Methods

run!() click to toggle source
# File lib/pain/cli.rb, line 15
def run!
  @option_parser.parse!

  %i[bug_type likelihood impact].each { |input| fill_question(input) }

  puts
  print_report_for options
end

Private Instance Methods

ask_question_for(input) click to toggle source
# File lib/pain/cli.rb, line 107
def ask_question_for(input)
  color_output model.input_message(input)
end
case_danger(danger) click to toggle source
# File lib/pain/cli.rb, line 58
def case_danger(danger)
  if danger < 0.21
    :trivial
  elsif danger < 0.41
    :minor
  elsif danger < 0.65
    :standard
  elsif danger < 0.81
    :major
  else
    :critical
  end
end
color_output(msg, danger = :normal) click to toggle source
# File lib/pain/cli.rb, line 86
def color_output(msg, danger = :normal)
  level = case danger
          when :normal
            :default
          when Symbol
            danger
          else
            case_danger(danger)
          end
  puts msg.color(get_color(level))
end
display_choices_for(input) click to toggle source
# File lib/pain/cli.rb, line 98
def display_choices_for(input)
  choices = model.choices_for(input)
  ceiling = choices.count
  choices.each do |v, msg|
    color_output("#{v}: #{msg}", (v / ceiling.to_f))
  end
  puts
end
fill_question(input, first: true) click to toggle source
# File lib/pain/cli.rb, line 28
def fill_question(input, first: true)
  return unless options[input].nil?

  ask_question_for input
  display_choices_for(input) if first

  response       = gets.chomp
  options[input] = model.normalize(response, input)

  fill_question(input, first: false) if options[input].nil?
end
get_color(value) click to toggle source
# File lib/pain/cli.rb, line 40
def get_color(value)
  offset = case value
           when :trivial
             40
           when :minor
             69
           when :standard
             184
           when :major
             202
           when :critical
             9
           else
             252
           end
  Term::ANSIColor::Attribute[offset]
end
print_report_for(scores) click to toggle source

rubocop:disable Metrics/AbcSize

total_danger(danger) click to toggle source
# File lib/pain/cli.rb, line 72
def total_danger(danger)
  if danger < 21
    :trivial
  elsif danger < 31
    :minor
  elsif danger < 51
    :standard
  elsif danger < 75
    :major
  else
    :critical
  end
end