Class: ChangePrompt

Inherits:
Object show all
Defined in:
lib/change_prompt.rb

Constant Summary collapse

LINES_OF_CONTEXT =
2
USE_EDITOR =
"E"
HORIZONTAL_LINE =
"-" * 100

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_input, replacements) ⇒ ChangePrompt

Returns a new instance of ChangePrompt.



38
39
40
41
# File 'lib/change_prompt.rb', line 38

def initialize(user_input, replacements)
  @user_input = user_input
  @replacements = replacements
end

Instance Attribute Details

#user_inputObject (readonly)

Returns the value of attribute user_input.



9
10
11
# File 'lib/change_prompt.rb', line 9

def user_input
  @user_input
end

Class Method Details

.get_the_input(replacements) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/change_prompt.rb', line 23

def self.get_the_input(replacements)
  user_input = gets
  puts("")
  prompt = ChangePrompt.new(user_input, replacements)

  until(prompt.valid?)
    puts "Yo, #{prompt.instance_variable_get(:@user_input)} is not a good answer.  Try again"
    user_input = gets
    puts("")
    prompt = ChangePrompt.new(user_input, replacements)
  end
  prompt
end

.prompt(pattern, file_line, replacements) ⇒ Object



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

def self.prompt(pattern, file_line, replacements)
  present_line(file_line, pattern)
  puts("How would you like to update the above line? Options below.  Hit return to do nothing\n\n")
  puts HORIZONTAL_LINE
  puts("#{USE_EDITOR}:  Edit in Vi")
  replacements.each_with_index do |replacement, index|
    puts "#{one_indexed(index)}:  #{replacement.highlighted_suggest(file_line.raw_contents)}"
  end
  puts HORIZONTAL_LINE
  get_the_input(replacements)
end

Instance Method Details

#chose_editor?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/change_prompt.rb', line 43

def chose_editor?
  user_input.downcase.match(USE_EDITOR.downcase)
end

#selected_replacementObject



47
48
49
# File 'lib/change_prompt.rb', line 47

def selected_replacement
  replacements[zero_indexed(integer_input)] if integer_input
end

#valid?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/change_prompt.rb', line 51

def valid?
  chose_editor? || valid_integer_input || user_input.chomp.empty?
end