class WtfRuby::CommandLineInteface

Public Instance Methods

display_method(class_instance, chosen_method_name) click to toggle source
# File lib/wtf_ruby/command_line_interface.rb, line 97
def display_method(class_instance, chosen_method_name)
    m_to_show = set_method(class_instance, chosen_method_name)
    puts ''
    puts '  Classy: ' + class_instance.name.colorize(:mode => :bold) + ' Method: ' + m_to_show.name.colorize(:color => :red, :mode => :bold)
    printf("\t %s \n", m_to_show.mini_description.colorize(:mode => :italic))
    m_to_show.headings.each do |heading|
     printf("\t#{heading.colorize(:light_blue)}\n")
    end
    m_to_show.sample_code.each do |code|
        line = code.colorize(:color => :white, :background => :green)
        printf("\t %-50s \n", line)
    end
end
make_classes() click to toggle source
# File lib/wtf_ruby/command_line_interface.rb, line 125
def make_classes
  class_list = WtfRuby::Scraper.scrape_class
  WtfRuby::Classy.create_from_collection(class_list)

end
printf_class_list() click to toggle source
# File lib/wtf_ruby/command_line_interface.rb, line 132
def printf_class_list
  counter = 0
  rows = WtfRuby::Classy.all.count/4
  rows.ceil.times do
    printf(" %2d.%-15s %2d.%-15s %2d.%-15s %2d.%-15s \n",
      counter += 1, WtfRuby::Classy.all[counter - 1].name,
      counter += 1, WtfRuby::Classy.all[counter - 1].name,
      counter += 1, WtfRuby::Classy.all[counter - 1].name,
      counter += 1, WtfRuby::Classy.all[counter - 1].name)
  end
end
printf_method_list(selected_class) click to toggle source
# File lib/wtf_ruby/command_line_interface.rb, line 112
def printf_method_list(selected_class)
  counter = 0
  rows = selected_class.methods.count/3
  rows.ceil.times do
    printf(" %2d.%-25s %2d.%-25s %2d.%-25s \n",
      counter += 1, selected_class.methods[counter - 1].name,
      counter += 1, selected_class.methods[counter - 1].name,
      counter += 1, selected_class.methods[counter - 1].name)
  end
end
run() click to toggle source
# File lib/wtf_ruby/command_line_interface.rb, line 11
  def run
  make_classes
  width_check
  WtfRuby::Scraper.store_offline
  WtfRuby::Classy.create_methods_for_all_classes


    welcome_text
    first_choice = gets.strip
    if validate_first_choice_input(first_choice) == { "valid class" => true, "valid method" => true, "inputs" => 2}
      class_choice = first_choice.split(',')[0].strip
      selected_class = set_class(class_choice)
      method_choice = first_choice.split(',')[1].strip
      display_method(selected_class, method_choice)
    elsif validate_first_choice_input(first_choice) == { "valid class" => true, "valid method" => false, "inputs" => 1}
      selected_class = set_class(first_choice)
      printf_method_list(selected_class)
      puts 'Please enter the method you wish to view:'
      method_choice = gets.strip
      display_method(selected_class, method_choice)
    elsif validate_first_choice_input(first_choice) == { "valid class" => true, "valid method" => false, "inputs" => 2}
      selected_class = set_class(first_choice.split(',')[0].strip)
      printf_method_list(selected_class)
      puts 'Please enter the method you wish to view:'
      method_choice = gets.strip
      display_method(selected_class, method_choice)
    else
      puts "Hmmmm... I couldn't make sense of your input.  Let's try this" if first_choice != ''
      printf_class_list
      puts 'Please enter the name of the class for which you wish to view available methods:'
      class_choice= gets.strip
      selected_class = set_class(class_choice)
      printf_method_list(selected_class)
      puts 'Please enter the method you wish to view:'
      method_choice = gets.strip
      display_method(selected_class, method_choice)

    end
end
set_class(string) click to toggle source
# File lib/wtf_ruby/command_line_interface.rb, line 93
def set_class(string)
  WtfRuby::Classy.all.select { |c| c.name == string}.first
end
set_method(class_instance, chosen_method_name) click to toggle source
# File lib/wtf_ruby/command_line_interface.rb, line 89
def set_method(class_instance, chosen_method_name)
  class_instance.methods.select { |m| m.name == chosen_method_name}.first
end
validate_first_choice_input(string) click to toggle source

return a hash that identifies whether the user provided class and method are valid.

# File lib/wtf_ruby/command_line_interface.rb, line 52
def validate_first_choice_input(string)
  inputs = { "valid class" => false, "valid method" => false, "inputs" => 0 }
  if set_class(string)
    inputs["valid class"] = true
    inputs["inputs"] = 1
  elsif string.include?(',')
    array = string.split(',')
    pot_class = array[0].strip
    pot_method = array[1].strip
    inputs["inputs"] = array.count
    if set_class(pot_class)
      inputs["valid class"] = true
      if set_method(set_class(pot_class), pot_method)
        inputs["valid method"] = true
      end
    end
  end
  inputs
end
welcome_text() click to toggle source
# File lib/wtf_ruby/command_line_interface.rb, line 80
def welcome_text
  puts "Hello, welcome to what_dis_ruby?.  If you know the class AND method  OR just the class that you wish to look up please enter them as shown below:"
  puts "Classy and Method: Classy, Method"
  puts "Just Classy: Classy"
  puts "Othwerwise, hit enter to see a list of available classes"
end
width_check() click to toggle source
# File lib/wtf_ruby/command_line_interface.rb, line 72
def width_check
  puts 'This gem displays some pretty big lists.  To make sure your terminal window is wide enough expand the view until you have a continuous line on the right side'
  5.times do
    printf(" %80s \n", "|")
  end
end