-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
80 lines (71 loc) · 2.15 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require(File.join(File.dirname(__FILE__), 'config', 'boot'))
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'
require 'tasks/rails'
namespace :player do
desc "Pick a random product as the prize"
task :parse => :environment do
file = File.new("players", "r")
current_team = nil
current_player = nil
while (line = file.gets)
team = team_name(line)
if team
current_team = Team.find_or_create_by_name(team)
puts "Created team #{team}"
elsif line.gsub(/\s+/, '').length != 0
if !has_dash(line)
current_player = Player.find_or_create_by_name(player_name(line))
puts "Created player #{current_player.name} on #{current_team.name}"
current_player.team = current_team
current_player.save
else
twitter_name = line.gsub('@', '').gsub(/\s+/, '')
twitter_name = remove_dash(twitter_name)
current_player.twitter_name = twitter_name
puts "with twitter_name #{twitter_name}"
current_player.save
end
end
end
file.close
end
desc "Get the team name"
task :all => [:parse]
def team_name(line)
stuff = line.gsub(/\s+/, '')
array = stuff.split(//u)
index = array.index("\226") || array.index('-')
if index && index > 0
team = array[0..(index-1)].join('')
team_name = team.split(/(?=[A-Z])/).join('_').gsub('_', ' ')
else
nil
end
end
desc "does the line have a dash"
task :all => [:parse]
def has_dash(line)
stuff = line.gsub(/\s+/, '')
array = stuff.split(//u)
array.index("\226") || array.index('-')
end
desc "Get the team name"
task :all => [:parse]
def player_name(line)
stuff = line.gsub(/\s+/, '')
stuff.split(/(?=[A-Z])/).join('_').gsub('_', ' ')
end
desc "Get the team name"
task :all => [:parse]
def remove_dash(line)
line.gsub!(/\s+/, '')
array = line.split(//u)
array.delete("\226")
array.delete('-')
array.join('')
end
end