Class: WtfRuby::CommandLineInteface

Inherits:
Object
  • Object
show all
Defined in:
lib/wtf_ruby/command_line_interface.rb

Instance Method Summary collapse

Instance Method Details

#display_method(class_instance, chosen_method_name) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
# 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_classesObject



125
126
127
128
129
# 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_listObject



132
133
134
135
136
137
138
139
140
141
142
# 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) ⇒ Object



112
113
114
115
116
117
118
119
120
121
# 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

#runObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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) ⇒ Object



93
94
95
# 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) ⇒ Object



89
90
91
# 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) ⇒ Object

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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# 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_textObject



80
81
82
83
84
85
# 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_checkObject



72
73
74
75
76
77
# 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