Class: FootballStatsFilterTool::CommandLineInterface

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#filter_listObject

Returns the value of attribute filter_list.



6
7
8
# File 'lib/football_stats_filter_tool/command_line_interface.rb', line 6

def filter_list
  @filter_list
end

#player_dataObject

Returns the value of attribute player_data.



6
7
8
# File 'lib/football_stats_filter_tool/command_line_interface.rb', line 6

def player_data
  @player_data
end

#sort_statObject

Returns the value of attribute sort_stat.



6
7
8
# File 'lib/football_stats_filter_tool/command_line_interface.rb', line 6

def sort_stat
  @sort_stat
end

#sort_valueObject

Returns the value of attribute sort_value.



6
7
8
# File 'lib/football_stats_filter_tool/command_line_interface.rb', line 6

def sort_value
  @sort_value
end

#stats_listObject

Returns the value of attribute stats_list.



6
7
8
# File 'lib/football_stats_filter_tool/command_line_interface.rb', line 6

def stats_list
  @stats_list
end

Class Method Details

.runObject



9
10
11
12
# File 'lib/football_stats_filter_tool/command_line_interface.rb', line 9

def self.run
    user_cli = FootballStatsFilterTool::CommandLineInterface.new
    user_cli.start
end

Instance Method Details

#allows_atribute?(attribute) ⇒ Boolean

Returns:

  • (Boolean)


292
293
294
295
# File 'lib/football_stats_filter_tool/command_line_interface.rb', line 292

def allows_atribute?(attribute)
    first_data_instance = player_data[0]
    first_data_instance.has_attribute?(attribute)
end

#determine_table_tabs(stat_symbol, value) ⇒ Object

returns the appropriate number of tabs for a given table entry



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/football_stats_filter_tool/command_line_interface.rb', line 125

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_dataObject



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/football_stats_filter_tool/command_line_interface.rb', line 48

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_dataObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/football_stats_filter_tool/command_line_interface.rb', line 68

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) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/football_stats_filter_tool/command_line_interface.rb', line 228

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) ⇒ Object



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/football_stats_filter_tool/command_line_interface.rb', line 268

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) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/football_stats_filter_tool/command_line_interface.rb', line 207

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


297
298
299
300
301
302
303
304
305
306
# File 'lib/football_stats_filter_tool/command_line_interface.rb', line 297

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_listObject



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/football_stats_filter_tool/command_line_interface.rb', line 164

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_sortObject



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/football_stats_filter_tool/command_line_interface.rb', line 186

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_listObject



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/football_stats_filter_tool/command_line_interface.rb', line 143

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_yearObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/football_stats_filter_tool/command_line_interface.rb', line 94

def request_year
    FootballStatsFilterTool::Player.delete_all
    puts "Please select a year to load"
    year = gets.chomp.to_i

    begin 
        FootballStatsFilterTool::Player.import(year) 
        @player_data = FootballStatsFilterTool::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_queryObject



39
40
41
42
43
44
45
46
# File 'lib/football_stats_filter_tool/command_line_interface.rb', line 39

def run_query 
    request_stats_list
    request_filter_list
    request_sort
    filter_player_data
    sort_player_data
    display_current_player_data
end

#sort_player_dataObject



60
61
62
63
64
65
66
# File 'lib/football_stats_filter_tool/command_line_interface.rb', line 60

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

#startObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/football_stats_filter_tool/command_line_interface.rb', line 14

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 = FootballStatsFilterTool::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_headerObject



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/football_stats_filter_tool/command_line_interface.rb', line 112

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