Class: Spellr::InteractiveReplacement

Inherits:
Object
  • Object
show all
Includes:
StringFormat
Defined in:
lib/spellr/interactive_replacement.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from StringFormat

aqua, bold, color_enabled?, green, normal, pluralize, red

Constructor Details

#initialize(token, reporter) ⇒ InteractiveReplacement

Returns a new instance of InteractiveReplacement.



9
10
11
12
13
14
15
# File 'lib/spellr/interactive_replacement.rb', line 9

def initialize(token, reporter)
  @original_token = @token = token
  @token_highlight = red(token)
  @reporter = reporter
  Readline.input = reporter.output.stdin
  Readline.output = reporter.output.stdout
end

Instance Attribute Details

#original_tokenObject (readonly)

Returns the value of attribute original_token.



7
8
9
# File 'lib/spellr/interactive_replacement.rb', line 7

def original_token
  @original_token
end

#reporterObject (readonly)

Returns the value of attribute reporter.



7
8
9
# File 'lib/spellr/interactive_replacement.rb', line 7

def reporter
  @reporter
end

#suffixObject (readonly)

Returns the value of attribute suffix.



7
8
9
# File 'lib/spellr/interactive_replacement.rb', line 7

def suffix
  @suffix
end

#tokenObject (readonly)

Returns the value of attribute token.



7
8
9
# File 'lib/spellr/interactive_replacement.rb', line 7

def token
  @token
end

#token_highlightObject (readonly)

Returns the value of attribute token_highlight.



7
8
9
# File 'lib/spellr/interactive_replacement.rb', line 7

def token_highlight
  @token_highlight
end

Instance Method Details

#complete_replacement(replacement) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/spellr/interactive_replacement.rb', line 29

def complete_replacement(replacement)
  token.replace("#{replacement}#{suffix}")

  reporter.increment(:total_fixed)
  puts "Replaced #{red(token.chomp)} with #{green(replacement)}"
  throw :check_file_from, token
end

#global_replaceObject



17
18
19
# File 'lib/spellr/interactive_replacement.rb', line 17

def global_replace
  replace { |replacement| reporter.global_replacements[token.to_s] = replacement }
end

#replaceObject

rubocop:disable Metrics/MethodLength, Metrics/AbcSize



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/spellr/interactive_replacement.rb', line 37

def replace # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
  readline_editable_print(token.chomp)

  puts "#{aqua '>>'} #{token_highlight}"
  replacement = Readline.readline("#{aqua '=>'} ")

  return reporter.call(token) if replacement.empty?

  yield replacement if block_given?
  complete_replacement(replacement)
rescue Interrupt
  puts '^C again to exit'
  reporter.call(original_token)
end

#replace_lineObject



21
22
23
24
25
26
27
# File 'lib/spellr/interactive_replacement.rb', line 21

def replace_line
  @token = token.line
  @token_highlight = token.highlight(original_token.char_range).chomp
  @suffix = "\n"

  replace
end