seed_list 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -86,10 +86,55 @@ SeedList::Strategy::Playoff.new(t.players).seed.map { |p| p.seed }
86
86
  => [1, 4, 2, 3]
87
87
 
88
88
  # An Amateur tournament matches by skill similarity straight down
89
- SeedList::Strategy::Playoff.new(t.players).seed.map { |p| p.seed }
89
+ SeedList::Strategy::Amateur.new(t.players).seed.map { |p| p.seed }
90
90
  => [1, 2, 3, 4]
91
91
 
92
92
  ```
93
+
94
+ ## Utilities
95
+
96
+ SeedList includes a few [Thor](https://github.com/wycats/thor/) tasks to
97
+ make seeds easy to manage from the command line.
98
+
99
+ ### Setup
100
+
101
+ Thor must load the Rails environment to import tasks from SeedList and other gems.
102
+
103
+ **lib/tasks/environment.thor**
104
+ ```ruby
105
+ require File.expand_path('config/environment')
106
+
107
+ ```
108
+
109
+ $ thor list
110
+ seed_list
111
+ ---------
112
+ thor seed_list:edit TOURNAMENT_ID # Reposition seeds using EDITOR (interactive)
113
+ thor seed_list:export TOURNAMENT_ID # Export line-separated seeds to STDOUT
114
+ thor seed_list:import TOURNAMENT_ID # Import line-separated seeds from STDIN
115
+
116
+
117
+ Your Player model is assumed to have a #login attribute to use these tasks.
118
+
119
+ ### Examples
120
+
121
+ Export seeds to a file
122
+
123
+ $ thor seed_list:export TOURNAMENT_ID > ./seeds
124
+ $ cat seeds
125
+ albafunk
126
+ deborahbayerii
127
+ ramonalockman
128
+ danlind
129
+
130
+ Import seeds from a file
131
+
132
+ $ cat ./seeds | thor seed_list:import TOURNAMENT_ID
133
+
134
+ Edit seeds in [VIM](http://www.vim.org/)
135
+
136
+ $ EDITOR=vim thor seed_list:edit TOURNAMENT_ID
137
+
93
138
  ## License
94
139
 
95
140
  See the [MIT-LICENSE](https://github.com/agoragames/seed_list/blob/master/MIT-LICENSE) file.
data/Rakefile CHANGED
@@ -20,8 +20,10 @@ RDoc::Task.new(:rdoc) do |rdoc|
20
20
  rdoc.rdoc_files.include('lib/**/*.rb')
21
21
  end
22
22
 
23
+ Bundler::GemHelper.install_tasks
23
24
 
25
+ require 'rspec/core/rake_task'
24
26
 
27
+ RSpec::Core::RakeTask.new(:spec)
25
28
 
26
- Bundler::GemHelper.install_tasks
27
-
29
+ task :default => :spec
@@ -2,6 +2,8 @@ require 'seed_list/version'
2
2
  require 'seed_list/list'
3
3
  require 'seed_list/model'
4
4
  require 'seed_list/strategy'
5
+ require 'seed_list/cli'
5
6
 
6
7
  module SeedList
8
+ mattr_accessor :tournament_class_name, :player_class_name
7
9
  end
@@ -0,0 +1,115 @@
1
+ require 'thor'
2
+
3
+ module SeedList
4
+ class CLI < Thor
5
+ namespace :seed_list
6
+
7
+ desc 'edit TOURNAMENT_ID', 'Reposition seeds using EDITOR (interactive)'
8
+ def edit(tournament_id)
9
+ @tournament_id = tournament_id
10
+ trap_signals
11
+ say "Using #{ENV['EDITOR']} (set $EDITOR to change)", :green
12
+ say 'Loading environment...', :green
13
+ say 'Exporting seed list...', :green
14
+
15
+ old_list = players.map { |p| p.login }
16
+
17
+ new_list = editor "tournament-#{tournament.id}-edit-seeds" do
18
+ buffer = "# Seed list for \"#{tournament.title}\" (##{tournament.id})\n\n"
19
+ buffer << "# Organize the players below by skill from best to worst.\n"
20
+ buffer << "# Save and quit to replace the seed list for this tournament.\n\n"
21
+
22
+ buffer << old_list.join("\n")
23
+ end.split("\n")
24
+ system('clear')
25
+
26
+ if new_list == old_list
27
+ say 'Nothing changed.', :yellow
28
+ else
29
+ say 'Seed list updated, saving...', :green
30
+ replace_seed_list new_list
31
+ end
32
+ end
33
+
34
+ desc 'import TOURNAMENT_ID', 'Import line-separated seeds from STDIN'
35
+ def import(tournament_id)
36
+ @tournament_id = tournament_id
37
+ trap_signals
38
+ replace_seed_list STDIN.read.split("\n")
39
+ end
40
+
41
+ desc 'export TOURNAMENT_ID', 'Export line-separated seeds to STDOUT'
42
+ def export(tournament_id)
43
+ @tournament_id = tournament_id
44
+ trap_signals
45
+ players.each { |p| puts p.login }
46
+ end
47
+
48
+ no_tasks do
49
+
50
+ def tournament
51
+ begin
52
+ @tournament ||= SeedList.tournament_class_name.constantize.find @tournament_id
53
+ rescue ActiveRecord::RecordNotFound => e
54
+ say e, :red
55
+ end
56
+ end
57
+
58
+ def players
59
+ eval("tournament.#{SeedList.player_class_name.tableize}")
60
+ .sort_by { |tp| tp.seed }
61
+ end
62
+
63
+ def get_player_id(login)
64
+ players.select { |p| p.login == login }.first.id
65
+ end
66
+
67
+ def replace_seed_list(logins)
68
+ @list = SeedList::List.new
69
+
70
+ logins.each do |login|
71
+ begin
72
+ @list.push get_player_id(login)
73
+ rescue ActiveRecord::RecordNotFound => e
74
+ say e, :red
75
+ end
76
+ end
77
+
78
+ tournament.tournament_players_seed_list = @list
79
+ tournament.save
80
+ end
81
+
82
+ # Terminate the program on SIGINT without printing a trace
83
+ def trap_signals
84
+ Signal.trap('INT') do
85
+ say "\nQuitting...", :red
86
+ Kernel.exit
87
+ end
88
+ end
89
+
90
+ # Interactively manipulate data in a text editor.
91
+ # @param [String] 'edit' Prefix for the tempfile
92
+ # @param [Block] A block returning a text string for the buffer
93
+ # @return [String] The updated text string (stripped of #comments and empty lines)
94
+ # @example Change "hello" to "goodbye"
95
+ # editor 'edit-greeting' do
96
+ # "# This line is a comment\n\nhello"
97
+ # end => "goodbye"
98
+ def editor(buffer_name = 'edit', &block)
99
+ require 'tempfile'
100
+ buffer = Tempfile.new(buffer_name + '-')
101
+ buffer << yield
102
+ buffer.flush
103
+ system("$EDITOR #{buffer.path}")
104
+ buffer.rewind
105
+ output = buffer.read.split("\n")
106
+ .reject { |line| line =~ /^\s*#.*$/ }
107
+ .reject { |line| line.empty? }
108
+ buffer.unlink
109
+ output.join("\n")
110
+ end
111
+
112
+ end
113
+
114
+ end
115
+ end
@@ -6,6 +6,9 @@ module SeedList
6
6
  def seed(assoc_name)
7
7
  assoc_name = assoc_name.to_s
8
8
 
9
+ SeedList.tournament_class_name = self.name
10
+ SeedList.player_class_name = assoc_name.camelize
11
+
9
12
  eval "serialize :#{assoc_name}_seed_list, SeedList::List"
10
13
 
11
14
  assoc_name.classify.constantize.class_eval <<-CODE
@@ -1,3 +1,3 @@
1
1
  module SeedList
2
- VERSION = "0.0.1"
2
+ VERSION = '0.1.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: seed_list
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-21 00:00:00.000000000 Z
12
+ date: 2012-10-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &6890660 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,31 @@ dependencies:
21
21
  version: 3.2.6
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *6890660
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 3.2.6
30
+ - !ruby/object:Gem::Dependency
31
+ name: thor
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
25
46
  - !ruby/object:Gem::Dependency
26
47
  name: sqlite3
27
- requirement: &6889020 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
28
49
  none: false
29
50
  requirements:
30
51
  - - ! '>='
@@ -32,10 +53,15 @@ dependencies:
32
53
  version: '0'
33
54
  type: :development
34
55
  prerelease: false
35
- version_requirements: *6889020
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
36
62
  - !ruby/object:Gem::Dependency
37
63
  name: rspec-rails
38
- requirement: &6887580 !ruby/object:Gem::Requirement
64
+ requirement: !ruby/object:Gem::Requirement
39
65
  none: false
40
66
  requirements:
41
67
  - - ! '>='
@@ -43,10 +69,15 @@ dependencies:
43
69
  version: '0'
44
70
  type: :development
45
71
  prerelease: false
46
- version_requirements: *6887580
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
47
78
  - !ruby/object:Gem::Dependency
48
79
  name: capybara
49
- requirement: &7428560 !ruby/object:Gem::Requirement
80
+ requirement: !ruby/object:Gem::Requirement
50
81
  none: false
51
82
  requirements:
52
83
  - - ! '>='
@@ -54,10 +85,15 @@ dependencies:
54
85
  version: '0'
55
86
  type: :development
56
87
  prerelease: false
57
- version_requirements: *7428560
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
58
94
  - !ruby/object:Gem::Dependency
59
95
  name: guard-rspec
60
- requirement: &7427160 !ruby/object:Gem::Requirement
96
+ requirement: !ruby/object:Gem::Requirement
61
97
  none: false
62
98
  requirements:
63
99
  - - ! '>='
@@ -65,10 +101,15 @@ dependencies:
65
101
  version: '0'
66
102
  type: :development
67
103
  prerelease: false
68
- version_requirements: *7427160
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
69
110
  - !ruby/object:Gem::Dependency
70
111
  name: guard-spork
71
- requirement: &7426500 !ruby/object:Gem::Requirement
112
+ requirement: !ruby/object:Gem::Requirement
72
113
  none: false
73
114
  requirements:
74
115
  - - ! '>='
@@ -76,10 +117,15 @@ dependencies:
76
117
  version: '0'
77
118
  type: :development
78
119
  prerelease: false
79
- version_requirements: *7426500
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
80
126
  - !ruby/object:Gem::Dependency
81
127
  name: factory_girl_rails
82
- requirement: &7425200 !ruby/object:Gem::Requirement
128
+ requirement: !ruby/object:Gem::Requirement
83
129
  none: false
84
130
  requirements:
85
131
  - - ! '>='
@@ -87,10 +133,15 @@ dependencies:
87
133
  version: '0'
88
134
  type: :development
89
135
  prerelease: false
90
- version_requirements: *7425200
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
91
142
  - !ruby/object:Gem::Dependency
92
143
  name: pry-rails
93
- requirement: &7423820 !ruby/object:Gem::Requirement
144
+ requirement: !ruby/object:Gem::Requirement
94
145
  none: false
95
146
  requirements:
96
147
  - - ! '>='
@@ -98,7 +149,12 @@ dependencies:
98
149
  version: '0'
99
150
  type: :development
100
151
  prerelease: false
101
- version_requirements: *7423820
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ! '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
102
158
  description: SeedList is designed for Rails-powered tournament engines that need to
103
159
  persist a 1-indexed ordered list of players (ranked low-to-high by skill or past
104
160
  performance) and then match them up appropriately in the first round of a bracket.
@@ -108,11 +164,11 @@ executables: []
108
164
  extensions: []
109
165
  extra_rdoc_files: []
110
166
  files:
111
- - lib/seed_list/strategy.rb
167
+ - lib/seed_list/cli.rb
112
168
  - lib/seed_list/list.rb
113
- - lib/seed_list/version.rb
114
169
  - lib/seed_list/model.rb
115
- - lib/tasks/seed_list_tasks.rake
170
+ - lib/seed_list/strategy.rb
171
+ - lib/seed_list/version.rb
116
172
  - lib/seed_list.rb
117
173
  - MIT-LICENSE
118
174
  - Rakefile
@@ -131,7 +187,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
131
187
  version: '0'
132
188
  segments:
133
189
  - 0
134
- hash: 1687580428413640907
190
+ hash: 2524279118631964387
135
191
  required_rubygems_version: !ruby/object:Gem::Requirement
136
192
  none: false
137
193
  requirements:
@@ -140,10 +196,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
140
196
  version: '0'
141
197
  segments:
142
198
  - 0
143
- hash: 1687580428413640907
199
+ hash: 2524279118631964387
144
200
  requirements: []
145
201
  rubyforge_project:
146
- rubygems_version: 1.8.17
202
+ rubygems_version: 1.8.24
147
203
  signing_key:
148
204
  specification_version: 3
149
205
  summary: Seed management for tournament brackets
@@ -1,4 +0,0 @@
1
- # desc "Explaining what the task does"
2
- # task :seed_list do
3
- # # Task goes here
4
- # end