Class: Spellr::InteractiveAdd

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from StringFormat

aqua, bold, green, key, lighten, normal, pluralize, red

Constructor Details

#initialize(token, reporter) ⇒ InteractiveAdd

Returns a new instance of InteractiveAdd.



12
13
14
15
16
17
18
# File 'lib/spellr/interactive_add.rb', line 12

def initialize(token, reporter)
  @token = token
  @reporter = reporter

  puts ''
  ask_wordlist
end

Instance Attribute Details

#reporterObject (readonly)

Returns the value of attribute reporter.



10
11
12
# File 'lib/spellr/interactive_add.rb', line 10

def reporter
  @reporter
end

#tokenObject (readonly)

Returns the value of attribute token.



10
11
12
# File 'lib/spellr/interactive_add.rb', line 10

def token
  @token
end

Instance Method Details

#add_to_wordlist(choice) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/spellr/interactive_add.rb', line 56

def add_to_wordlist(choice)
  wordlist = find_wordlist(choice)
  wordlist << token
  reporter.increment(:total_added)
  puts "\nAdded #{red(token)} to the #{bold wordlist.name} wordlist"
  throw :check_file_from, token
end

#addable_languagesObject



24
25
26
# File 'lib/spellr/interactive_add.rb', line 24

def addable_languages
  languages.select(&:addable?)
end

#ask_wordlistObject



32
33
34
35
36
37
38
39
# File 'lib/spellr/interactive_add.rb', line 32

def ask_wordlist
  addable_languages.each { |l| puts "  #{key l.key} #{l.name}" }
  puts "  [^#{bold 'C'}] to go back"
  print "  Add #{red(token)} to which wordlist? "
  reporter.prompt_for_key

  handle_wordlist_choice
end

#find_wordlist(key) ⇒ Object



64
65
66
# File 'lib/spellr/interactive_add.rb', line 64

def find_wordlist(key)
  addable_languages.find { |w| w.key == key }.project_wordlist
end

#handle_ctrl_cObject



41
42
43
44
# File 'lib/spellr/interactive_add.rb', line 41

def handle_ctrl_c
  reporter.clear_line(language_keys.length + 5)
  reporter.call(token, only_prompt: true)
end

#handle_wordlist_choiceObject



46
47
48
49
50
51
52
53
54
# File 'lib/spellr/interactive_add.rb', line 46

def handle_wordlist_choice
  choice = reporter.stdin_getch("#{language_keys.join}\u0003")
  # :nocov:
  case choice
  # :nocov:
  when "\u0003" then handle_ctrl_c
  when *language_keys then add_to_wordlist(choice)
  end
end

#language_keysObject



28
29
30
# File 'lib/spellr/interactive_add.rb', line 28

def language_keys
  @language_keys ||= addable_languages.map(&:key)
end

#languagesObject



20
21
22
# File 'lib/spellr/interactive_add.rb', line 20

def languages
  @languages ||= Spellr.config.languages_for(token.location.file)
end

#puts(str) ⇒ Object



68
69
70
# File 'lib/spellr/interactive_add.rb', line 68

def puts(str)
  reporter.puts(str)
end