class EasyRspec::FilePathFinder

Public Class Methods

new(klass_name) click to toggle source
# File lib/easy_rspec/file_path_finder.rb, line 5
def initialize(klass_name)
  @klass_name = klass_name
end

Public Instance Methods

file_path() click to toggle source
# File lib/easy_rspec/file_path_finder.rb, line 9
def file_path
  file_path = if matching_file_paths.one?
    matching_file_paths.first
  elsif matching_file_paths.size > 1
    user_selected_file
  else
    nil
  end
  raise "File path not found" unless file_path
  file_path
end

Private Instance Methods

app_file_paths() click to toggle source
# File lib/easy_rspec/file_path_finder.rb, line 59
def app_file_paths
  Find.find('app/').select { |file| File.extname(file) == '.rb' }
end
file_name() click to toggle source
# File lib/easy_rspec/file_path_finder.rb, line 43
def file_name
  "#{file_name_components.join('/')}.rb"
end
file_name_components() click to toggle source
# File lib/easy_rspec/file_path_finder.rb, line 47
def file_name_components
  klass_name_components.map{ |component| component.gsub(/(.)([A-Z])/,'\1_\2').downcase }
end
klass_name_components() click to toggle source
# File lib/easy_rspec/file_path_finder.rb, line 51
def klass_name_components
  @klass_name.to_s.split('::')
end
matching_file_paths() click to toggle source
# File lib/easy_rspec/file_path_finder.rb, line 55
def matching_file_paths
  @matching_file_paths ||= app_file_paths.select{ |file_path| file_path.include? "/#{file_name}" }
end
user_selected_file() click to toggle source
# File lib/easy_rspec/file_path_finder.rb, line 23
def user_selected_file
  max_index = matching_file_paths.size - 1
  index = user_selected_index
  if index && index <= max_index
    matching_file_paths[index]
  else
    nil
  end
end
user_selected_index() click to toggle source
# File lib/easy_rspec/file_path_finder.rb, line 33
def user_selected_index
  puts "\nWhich number represents your file path?\n\n"
  matching_file_paths.each_with_index do |file_path, index|
    puts "#{index}. #{file_path}"
  end
  puts "\n"
  user_provided_index = STDIN.gets.gsub(/[^\d]/, '')
  user_provided_index ? user_provided_index.to_i : nil
end