Module: Mentor::OutputHelper

Included in:
Colorize, Outputs
Defined in:
lib/helpers/output_helper.rb

Instance Method Summary collapse

Instance Method Details

#a_an(word) ⇒ Object



5
6
7
# File 'lib/helpers/output_helper.rb', line 5

def a_an(word)
  %w(A E I O U).include?(word[0]) ? 'an' : 'a'
end

#and_sentence(words) ⇒ Object



25
26
27
# File 'lib/helpers/output_helper.rb', line 25

def and_sentence(words)
  words.join(' and ')
end

#culprit_lineObject



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

def culprit_line
  lines_from_file[tp.lineno]
end

#home_to_tilde(path) ⇒ Object



29
30
31
# File 'lib/helpers/output_helper.rb', line 29

def home_to_tilde(path)
  path.sub(ENV['HOME'], '~')
end

#indent_lines(*lines, indent: 2) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/helpers/output_helper.rb', line 33

def indent_lines(*lines, indent: 2)
  lines.flatten!

  indent.downto(1).each do |number_of_spaces|
    if lines.all? { |line| line.size + indent * 2 <= terminal_width }
      return lines.map { |line| ' ' * number_of_spaces + line }
    end
  end

  lines
end

#lines_from_fileObject



45
46
47
48
49
# File 'lib/helpers/output_helper.rb', line 45

def lines_from_file
  return @lines_from_file if @lines_from_file
  file = File.new(Mentor.tp.path)
  @lines_from_file = file.map { |line| [file.lineno, line.chomp] }.to_h
end

#or_sentence(words) ⇒ Object



21
22
23
# File 'lib/helpers/output_helper.rb', line 21

def or_sentence(words)
  words.join(' or ')
end

#pluralize(word) ⇒ Object



13
14
15
# File 'lib/helpers/output_helper.rb', line 13

def pluralize(word)
  word.to_s + (word.to_s == 'Hash' ? 'es' : 's')
end

#pluralize_words(words) ⇒ Object



17
18
19
# File 'lib/helpers/output_helper.rb', line 17

def pluralize_words(words)
  words.map { |word| pluralize word }
end

#terminal_widthObject



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

def terminal_width
  `tput cols`.to_i
end

#valid_var_nameObject



55
56
57
# File 'lib/helpers/output_helper.rb', line 55

def valid_var_name
  /(@@|@|\$)?[a-zA-Z][a-zA-Z_0-9]*/
end