Class: RuboCounsel::CopPrompt

Inherits:
Object
  • Object
show all
Defined in:
lib/rubocounsel/cop_prompt.rb

Overview

Presents a list of actions for a cop and collects the user's choice.

Actions are composable objects with #label, #call, and #reprompt?. The prompt loops until an action returns a result (reprompt? is false).

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(actions:) ⇒ CopPrompt

Returns a new instance of CopPrompt.



27
28
29
# File 'lib/rubocounsel/cop_prompt.rb', line 27

def initialize(actions:)
  @actions = actions
end

Class Method Details

.for(entry, output: $stdout) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rubocounsel/cop_prompt.rb', line 11

def self.for(entry, output: $stdout)
  if entry.configurable?
    actions = [
      CopActions::Configure.new(entry, output: output),
      CopActions::Disable.new,
      CopActions::Skip.new
    ]
  else
    actions = []
    actions << CopActions::ShowExamples.new(entry.examples, output: output) if entry.examples.any?
    actions.push(CopActions::Enable.new, CopActions::Disable.new, CopActions::Skip.new)
  end

  new(actions: actions)
end

Instance Method Details

#promptObject



31
32
33
34
35
36
37
38
39
# File 'lib/rubocounsel/cop_prompt.rb', line 31

def prompt
  loop do
    labels = @actions.map(&:label)
    chosen = ::CLI::UI::Prompt.ask("What would you like to do?", options: labels)
    action = @actions.find { |a| a.label == chosen }
    result = action.call
    return result unless action.reprompt?
  end
end