Class: Spellr::InteractiveReplacement
- Inherits:
-
Object
- Object
- Spellr::InteractiveReplacement
- Includes:
- StringFormat
- Defined in:
- lib/spellr/interactive_replacement.rb
Instance Attribute Summary collapse
-
#original_token ⇒ Object
readonly
Returns the value of attribute original_token.
-
#reporter ⇒ Object
readonly
Returns the value of attribute reporter.
-
#suffix ⇒ Object
readonly
Returns the value of attribute suffix.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
-
#token_highlight ⇒ Object
readonly
Returns the value of attribute token_highlight.
Instance Method Summary collapse
- #complete_replacement(replacement) ⇒ Object
- #global_replace ⇒ Object
-
#initialize(token, reporter) ⇒ InteractiveReplacement
constructor
A new instance of InteractiveReplacement.
-
#replace ⇒ Object
rubocop:disable Metrics/MethodLength, Metrics/AbcSize.
- #replace_line ⇒ Object
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_token ⇒ Object (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 |
#reporter ⇒ Object (readonly)
Returns the value of attribute reporter.
7 8 9 |
# File 'lib/spellr/interactive_replacement.rb', line 7 def reporter @reporter end |
#suffix ⇒ Object (readonly)
Returns the value of attribute suffix.
7 8 9 |
# File 'lib/spellr/interactive_replacement.rb', line 7 def suffix @suffix end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
7 8 9 |
# File 'lib/spellr/interactive_replacement.rb', line 7 def token @token end |
#token_highlight ⇒ Object (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_replace ⇒ Object
17 18 19 |
# File 'lib/spellr/interactive_replacement.rb', line 17 def global_replace replace { |replacement| reporter.global_replacements[token.to_s] = replacement } end |
#replace ⇒ Object
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_line ⇒ Object
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 |