class CommandLineInterface
Attributes
filter_list[RW]
player_data[RW]
sort_stat[RW]
sort_value[RW]
stats_list[RW]
user[RW]
Public Class Methods
new(user)
click to toggle source
# File lib/command_line_interface.rb, line 7 def initialize(user) @user = user end
run()
click to toggle source
# File lib/command_line_interface.rb, line 12 def self.run user_cli = CommandLineInterface.new("ADMIN") user_cli.start end
Public Instance Methods
allows_atribute?(attribute)
click to toggle source
# File lib/command_line_interface.rb, line 295 def allows_atribute?(attribute) first_data_instance = player_data[0] first_data_instance.has_attribute?(attribute) end
determine_table_tabs(stat_symbol, value)
click to toggle source
# File lib/command_line_interface.rb, line 128 def determine_table_tabs(stat_symbol, value)#returns the appropriate number of tabs for a given table entry value_length = value.to_s.length stat_symbol_length = stat_symbol.to_s.length tab_length = 8; name_defaults_tabs =3; num_tabs = 0; if(stat_symbol == :name) num_tabs = name_defaults_tabs-(value_length)/tab_length else num_tabs = 1 + (stat_symbol_length)/tab_length - (value_length)/tab_length end out = "" num_tabs.times{out += "\t"} return out end
display_current_player_data()
click to toggle source
# File lib/command_line_interface.rb, line 51 def display_current_player_data puts stats_list_header @player_data.each.with_index do |player, i| out = "#{i+1}.\t" @stats_list.each do |stat| value = player.instance_variable_get("@#{stat}") out+= "#{value}#{determine_table_tabs(stat, value)}" end puts out end end
filter_player_data()
click to toggle source
# File lib/command_line_interface.rb, line 71 def filter_player_data out = [] @filter_list.each do |filter_entry| stat = filter_entry[:stat] value = filter_entry[:value] compare_type = filter_entry[:compare_type] @player_data.each do |player_entry| entry_value = player_entry.instance_variable_get("@#{stat}") if(entry_value!=nil) if(compare_type == ">" && entry_value>value) out.push(player_entry) end if(compare_type == "<" && entry_value<value) out.push(player_entry) end if(compare_type == "=" && entry_value==value) out.push(player_entry) end end end @player_data = out out = [] end end
interpret_filter_selection(input)
click to toggle source
# File lib/command_line_interface.rb, line 231 def interpret_filter_selection(input) list = input.strip.split(/,+/) out = [] valid_entries = [] invalid_entries = [] list.each do |entry| hash = {} compare_type = entry.match(/[=<>]/) if(compare_type == nil)#indicates no compare operator (>, <, =) was found hash[:type] = "invalid compare statement" hash[:string] = entry else compare_type = compare_type[0] entry_data = entry.gsub(/\s+/, "").split(compare_type) if(entry_data.length != 2)#indicates either no stat, no value or too many compare operators hash[:type] = "invalid compare statement" hash[:string] = entry elsif(!allows_atribute?(entry_data[0])) hash[:type] = "invalid compare statement" hash[:string] = entry else hash[:type] = "valid compare statement" hash[:compare_type] = compare_type hash[:stat] = entry_data[0].gsub(/\s+/, "").to_sym hash[:value] = entry_data[1].gsub(/\s+/, "").to_f end end if(hash[:type] == "invalid compare statement") invalid_entries.push(hash) elsif(hash[:type] == "valid compare statement") valid_entries.push(hash) end out.push(hash[:type] == "invalid compare statement") end return { :valid_data => valid_entries, :invalid_data => invalid_entries } end
interpret_sort_selection(input)
click to toggle source
# File lib/command_line_interface.rb, line 271 def interpret_sort_selection(input) out = input.strip.split(/[\s,]+/) output_data = {} if(out.length>2 || !allows_atribute?(out[0])) output_data[:return_type] = "invalid data" elsif(out.length == 2) sort_priority = out[1].upcase if(sort_priority != "ASC" && sort_priority != "DESC") output_data[:return_type] = "invalid data" else output_data[:return_type] = "valid data" output_data[:sort_stat] = out[0].to_sym output_data[:sort_value] = sort_priority end elsif(out.length == 1) output_data[:sort_stat] = out[0].to_sym output_data[:sort_value] = "ASC" else output_data[:sort_stat] = :name output_data[:sort_value] = "ASC" end output_data end
interpret_stat_selection(input)
click to toggle source
# File lib/command_line_interface.rb, line 210 def interpret_stat_selection(input) out = input.strip.split(/[\s,]+/) out.unshift("name") valid_entries = [] invalid_entries = [] out.each do |attribute| if(allows_atribute?(attribute)) valid_entries.push(attribute.to_sym) else invalid_entries.push(attribute) end end return { :valid_data => valid_entries.uniq, :invalid_data => invalid_entries.uniq } end
print_allowable_attributes()
click to toggle source
# File lib/command_line_interface.rb, line 300 def print_allowable_attributes first_data_instance = player_data[0] puts "The following attributes exist in this data set" puts "\n" current_string = ""; first_data_instance.class.allowable_attributes.each_with_index do |attribute, i| puts attribute end puts "\n" end
request_filter_list()
click to toggle source
# File lib/command_line_interface.rb, line 167 def request_filter_list puts "Give a list of conditions that should be met with the player to be included (e.g sacks > 8, pass_touchdowns > 25, team = SEA)" filters_input = gets.chomp filters_interpreted = interpret_filter_selection(filters_input) if(filters_interpreted[:invalid_data].length>0) puts "The following entries were invalid and will be discarded" filters_interpreted[:invalid_data].each do |entry| puts entry[:string] end puts "Would you like to repeat this step? (y/n)" input = gets.chomp if(input == "y") request_filter_list else @filter_list = filters_interpreted[:valid_data] end else @filter_list = filters_interpreted[:valid_data] end end
request_sort()
click to toggle source
# File lib/command_line_interface.rb, line 189 def request_sort puts "How would you like to sort the data, give a stat and ASC or DESC" sort_input = gets.chomp sort_interpreted = interpret_sort_selection(sort_input) if(sort_interpreted[:return_type] == "invalid_data") "Your entry was invlalid and will be discarded" puts "Would you like to repeat this step? (y/n)" if(input == "y") request_sort else #default to sort by name ascending @sort_stat = "name" @sort_value = "ASC" end else @sort_stat = sort_interpreted[:sort_stat] @sort_value = sort_interpreted[:sort_value] end end
request_stats_list()
click to toggle source
# File lib/command_line_interface.rb, line 146 def request_stats_list puts "Give a list of the stats you would like display along with the player name (e.g. team, sacks, tackles)" stats_input = gets.chomp stats_interpreted = interpret_stat_selection(stats_input) if(stats_interpreted[:invalid_data].length>0) puts "The following entries were invalid and will be discarded" stats_interpreted[:invalid_data].each do |entry| puts entry end puts "Would you like to repeat this step? (y/n)" input = gets.chomp if(input == "y") request_stats_list else @stats_list = stats_interpreted[:valid_data] end else @stats_list = stats_interpreted[:valid_data] end end
request_year()
click to toggle source
# File lib/command_line_interface.rb, line 97 def request_year Player.delete_all puts "Please select a year to load" year = gets.chomp.to_i begin Player.import(year) @player_data = Player.all rescue OpenURI::HTTPError => error if error.message == '404 Not Found' puts "Invalid year selection" request_year else raise error end end end
run_query()
click to toggle source
# File lib/command_line_interface.rb, line 42 def run_query request_stats_list request_filter_list request_sort filter_player_data sort_player_data display_current_player_data end
sort_player_data()
click to toggle source
# File lib/command_line_interface.rb, line 63 def sort_player_data if(@sort_value == "ASC") @player_data.sort!{|player_a, player_b| player_a.instance_variable_get("@#{@sort_stat}") <=> player_b.instance_variable_get("@#{@sort_stat}")} elsif(@sort_value == "DESC") @player_data.sort!{|player_a, player_b| player_b.instance_variable_get("@#{@sort_stat}") <=> player_a.instance_variable_get("@#{@sort_stat}")} end end
start()
click to toggle source
# File lib/command_line_interface.rb, line 17 def start puts "Welcome to Football Stats Filter Tool" puts "Would you like to load some football stats (y/n)" input = gets.chomp while(input == "y") request_year print_allowable_attributes loop do run_query puts "Would you like to query another list from this year? (y/n)" input = gets.chomp if(input != "y") break end @player_data = Player.all end puts "Would you like to load another year? (y/n)" input = gets.chomp end puts "Thank you for using the Football stats filter tool, have a nice day" end
stats_list_header()
click to toggle source
# File lib/command_line_interface.rb, line 115 def stats_list_header out ="\tname\t\t\t" tab_length = 8; @stats_list.each do |entry| if(entry!=:name) out += entry.to_s out+="\t" end end out end