ranking 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,4 @@
1
+ lib/**/*.rb
2
+ README.rdoc
3
+ ChangeLog.rdoc
4
+ LICENSE.txt
@@ -0,0 +1,4 @@
1
+ Gemfile.lock
2
+ html/
3
+ pkg/
4
+ vendor/cache/*.gem
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour --format documentation
@@ -0,0 +1,4 @@
1
+ === 0.1.0 / 2012-07-04
2
+
3
+ * Initial release:
4
+
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source :rubygems
2
+
3
+ gemspec
4
+
5
+ group :development do
6
+
7
+ gem "pry"
8
+ gem 'rubygems-tasks', '~> 0.2'
9
+ gem 'rdoc', '~> 3.0'
10
+ gem 'rspec', '~> 2.4'
11
+ gem 'bundler', '~> 1.0'
12
+ gem 'rake', '~> 0.8'
13
+ end
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Ryan Michael
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,33 @@
1
+ # ranking
2
+
3
+ ## Description
4
+
5
+ Extends `Set` to be ordered by a score.
6
+
7
+ ## Examples
8
+
9
+ ``` ruby
10
+ require 'ranking'
11
+
12
+ ranking = Ranking.new
13
+ ranking << :foo
14
+ ranking << :bar
15
+
16
+ ranking.inc(:foo, 2) # Incremente score of :foo by one
17
+ ranking.dec(:bar) # Decrement, defaults to 1
18
+ ranking.score(:baz, 3) # Set score explicitly, implicitly adds :baz to the set
19
+
20
+ ranking.to_a # => [:baz, :foo, :bar]
21
+ ranking.scores[:foo] # => 2
22
+ ```
23
+
24
+
25
+ ## Install
26
+
27
+ $ gem install ranking
28
+
29
+ ## Copyright
30
+
31
+ Copyright (c) 2012 Ryan Michael
32
+
33
+ See LICENSE.txt for details.
@@ -0,0 +1,36 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+
5
+ begin
6
+ require 'bundler'
7
+ rescue LoadError => e
8
+ warn e.message
9
+ warn "Run `gem install bundler` to install Bundler."
10
+ exit e.status_code
11
+ end
12
+
13
+ begin
14
+ Bundler.setup(:development)
15
+ rescue Bundler::BundlerError => e
16
+ warn e.message
17
+ warn "Run `bundle install` to install missing gems."
18
+ exit e.status_code
19
+ end
20
+
21
+ require 'rake'
22
+
23
+ require 'rubygems/tasks'
24
+ Gem::Tasks.new
25
+
26
+ require 'rdoc/task'
27
+ RDoc::Task.new do |rdoc|
28
+ rdoc.title = "ranking"
29
+ end
30
+ task :doc => :rdoc
31
+
32
+ require 'rspec/core/rake_task'
33
+ RSpec::Core::RakeTask.new
34
+
35
+ task :test => :spec
36
+ task :default => :spec
@@ -0,0 +1,12 @@
1
+ require 'set'
2
+
3
+ class Ranking < Set
4
+ require 'ranking/instance_methods'
5
+ require 'ranking/set_methods'
6
+
7
+ include InstanceMethods
8
+ include SetMethods
9
+
10
+ attr_reader :scores
11
+ end
12
+
@@ -0,0 +1,24 @@
1
+ module Ranking::InstanceMethods
2
+ def initialize(*args)
3
+ @scores = Hash.new(0)
4
+ super *args
5
+ end
6
+
7
+ def inc(object, value=1)
8
+ self << object
9
+ scores[object] += value
10
+ self
11
+ end
12
+
13
+ def dec(object, value=1)
14
+ self << object
15
+ scores[object] -= value
16
+ self
17
+ end
18
+
19
+ def score(object, value)
20
+ self << object
21
+ scores[object] = value
22
+ self
23
+ end
24
+ end
@@ -0,0 +1,94 @@
1
+ module Ranking::SetMethods
2
+ def &(enum)
3
+ raise NotImplementedError
4
+ end
5
+
6
+ def |(enum)
7
+ dup.merge(enum)
8
+ end
9
+
10
+ def -(enum)
11
+ raise NotImplementedError
12
+ end
13
+
14
+ def ==(other)
15
+ other.kind_of?(Ranking) and other.scores == scores and super(other)
16
+ end
17
+
18
+ def ^(other)
19
+ raise NotImplementedError
20
+ end
21
+
22
+ def clear
23
+ @scores = Hash.new(0)
24
+ super
25
+ end
26
+
27
+ def collect
28
+ raise NotImplementedError
29
+ end
30
+
31
+ def delete(object)
32
+ scores.delete(object)
33
+ super object
34
+ end
35
+
36
+ def delete?(object)
37
+ scores.delete(object)
38
+ super object
39
+ end
40
+
41
+ def delete_if(&block)
42
+ raise NotImplementedError
43
+ end
44
+
45
+ def divide(&block)
46
+ raise NotImplementedError
47
+ end
48
+
49
+ def each(&block)
50
+ to_a.each(&block)
51
+ end
52
+
53
+ def flatten
54
+ raise NotImplementedError
55
+ end
56
+
57
+ def inspect
58
+ "<Ranking: #{to_a.map(&:inspect).join(', ')}>"
59
+ end
60
+
61
+ def keep_if(&block)
62
+ raise NotImplementedError
63
+ end
64
+
65
+ def merge(enum)
66
+ if enum.kind_of?(Ranking)
67
+ scores.merge!(enum.scores)
68
+ end
69
+
70
+ super enum
71
+ end
72
+
73
+ def reject!(&block)
74
+ raise NotImplementedError
75
+ end
76
+
77
+ def replace(enum)
78
+ raise NotImplementedError
79
+ end
80
+
81
+ def select!(&block)
82
+ raise NotImplementedError
83
+ end
84
+
85
+ def to_a
86
+ super.sort do |a,b|
87
+ if scores[a] == scores[b]
88
+ a <=> b
89
+ else
90
+ scores[b] <=> scores[a]
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.name = "ranking"
5
+ gem.version = "0.1.0"
6
+ gem.summary = %q{Sorted sets using scores}
7
+ gem.description = %q{Extends Set with a score. Sorts by score on #to_a, #each, etc}
8
+ gem.license = "MIT"
9
+ gem.authors = ["Ryan Michael"]
10
+ gem.email = "kerinin@gmail.com"
11
+ gem.homepage = "https://github.com/kerinin/ranking#readme"
12
+
13
+ gem.files = `git ls-files`.split($/)
14
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
15
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
16
+ gem.require_paths = ['lib']
17
+
18
+ gem.add_development_dependency 'bundler', '~> 1.0'
19
+ end
@@ -0,0 +1,84 @@
1
+ require 'spec_helper'
2
+ require 'ranking'
3
+
4
+ describe Ranking::InstanceMethods do
5
+ before(:each) do
6
+ @instance = Ranking[:foo, :bar]
7
+ end
8
+
9
+ describe "::new" do
10
+ it "creates an empty scores hash" do
11
+ Ranking.new.scores.should == {}
12
+ end
13
+
14
+ it "defaults to score=0" do
15
+ Ranking[:foo].scores[:foo].should == 0
16
+ end
17
+ end
18
+
19
+ describe "#inc" do
20
+ it "returns self" do
21
+ @instance.inc(:foo).should be_a(Ranking)
22
+ end
23
+
24
+ it "adds element if not already there" do
25
+ @instance.inc(:baz).should include(:baz)
26
+ end
27
+
28
+ it "increments by passed value" do
29
+ @instance.inc(:foo, 10).scores[:foo].should == 10
30
+ end
31
+
32
+ it "is idempotent" do
33
+ @instance.inc(:foo, 5).inc(:foo, 5).scores[:foo].should == 10
34
+ end
35
+
36
+ it "defaults to 1" do
37
+ @instance.inc(:foo).scores[:foo].should == 1
38
+ end
39
+ end
40
+
41
+ describe "#dec" do
42
+ it "returns self" do
43
+ @instance.dec(:foo).should be_a(Ranking)
44
+ end
45
+
46
+ it "adds element if not already there" do
47
+ @instance.dec(:baz).should include(:baz)
48
+ end
49
+
50
+ it "decrements by passed value" do
51
+ @instance.dec(:foo, 10).scores[:foo].should == -10
52
+ end
53
+
54
+ it "is idempotent" do
55
+ @instance.dec(:foo, 5).dec(:foo, 5).scores[:foo].should == -10
56
+ end
57
+
58
+ it "defaults to 1" do
59
+ @instance.dec(:foo).scores[:foo].should == -1
60
+ end
61
+ end
62
+
63
+ describe "#score" do
64
+ it "returns self" do
65
+ @instance.score(:foo, 10).should be_a(Ranking)
66
+ end
67
+
68
+ it "adds element if not already there" do
69
+ @instance.score(:baz, 10).should include(:baz)
70
+ end
71
+
72
+ it "scorerements by passed value" do
73
+ @instance.score(:foo, 10).scores[:foo].should == 10
74
+ end
75
+
76
+ it "is idempotent" do
77
+ @instance.score(:foo, 5).score(:foo, 10).scores[:foo].should == 10
78
+ end
79
+
80
+ it "requires a value" do
81
+ expect { @instance.score(:foo) }.to raise_error(ArgumentError)
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,5 @@
1
+ require 'spec_helper'
2
+ require 'ranking'
3
+
4
+ describe Ranking do
5
+ end
@@ -0,0 +1,250 @@
1
+ require 'spec_helper'
2
+ require 'ranking'
3
+
4
+ describe Ranking::SetMethods do
5
+ before(:each) do
6
+ @instance = Ranking[:foo, :bar]
7
+ end
8
+
9
+ describe "#&" do
10
+ it "isn't implemented" do
11
+ expect { @instance & @instance }.to raise_error(NotImplementedError)
12
+ end
13
+ end
14
+
15
+ describe "#|" do
16
+ before(:each) do
17
+ other = Ranking[:bar, :baz].inc(:bar).inc(:baz)
18
+ @union = @instance | other
19
+ end
20
+
21
+ it "creates the union set" do
22
+ @union.to_set.should == [:foo, :bar, :baz].to_set
23
+ end
24
+
25
+ it "merges scores with preference to other" do
26
+ @union.scores.should == {:bar => 1, :baz => 1}
27
+ end
28
+ end
29
+
30
+ describe "#-" do
31
+ it "isn't implemented" do
32
+ expect { @instance - @instance }.to raise_error(NotImplementedError)
33
+ end
34
+ end
35
+
36
+ describe "#==" do
37
+ it "is true if the same" do
38
+ @instance.should == @instance
39
+ end
40
+
41
+ it "is false if scores different" do
42
+ other = Ranking[@instance.to_a].dec(:foo)
43
+ @instance.should_not == @other
44
+ end
45
+
46
+ it "is false if sets different" do
47
+ other = Ranking[:bar, :baz]
48
+ other.instance_variable_set(:@scores, @instance.scores)
49
+ @instance.should_not == other
50
+ end
51
+ end
52
+
53
+ describe "#^" do
54
+ it "isn't implemented" do
55
+ expect { @instance ^ @instance }.to raise_error(NotImplementedError)
56
+ end
57
+ end
58
+
59
+ describe "#add" do
60
+ before(:each) do
61
+ @instance.add :baz
62
+ end
63
+
64
+ it "returns self" do
65
+ @instance.add(:qux).should be_a(Ranking)
66
+ end
67
+
68
+ it "adds to the set" do
69
+ @instance.should include(:baz)
70
+ end
71
+ end
72
+
73
+ describe "#add?" do
74
+ it "calls add" do
75
+ @instance.should_receive(:add).with(:baz)
76
+ @instance.add?(:baz)
77
+ end
78
+
79
+ it "returns nil if already a member" do
80
+ @instance.add?(:foo).should be_false
81
+ end
82
+ end
83
+
84
+ describe "#clear" do
85
+ before(:each) do
86
+ @instance.clear
87
+ end
88
+
89
+ it "removes set elements" do
90
+ @instance.to_set.should == Set.new
91
+ end
92
+
93
+ it "clears scores" do
94
+ @instance.scores.should == {}
95
+ end
96
+ end
97
+
98
+ describe "#collect" do
99
+ it "isn't implemented" do
100
+ expect { @instance.collect {} }.to raise_error(NotImplementedError)
101
+ end
102
+ end
103
+
104
+ describe "#delete" do
105
+ before(:each) do
106
+ @instance.delete(:foo)
107
+ end
108
+
109
+ it "removes from set" do
110
+ @instance.should_not include(:foo)
111
+ end
112
+
113
+ it "removes from scores" do
114
+ @instance.scores.keys.should_not include(:foo)
115
+ end
116
+ end
117
+
118
+ describe "#delete?" do
119
+ it "removes from the set" do
120
+ @instance.delete?(:foo).should_not include(:foo)
121
+ end
122
+
123
+ it "removes from the scores" do
124
+ @instance.delete?(:foo).scores.keys.should_not include(:foo)
125
+ end
126
+
127
+ it "returns nil if not a member" do
128
+ @instance.delete?(:baz).should be_false
129
+ end
130
+ end
131
+
132
+ describe "#delete_if" do
133
+ it "isn't implemented" do
134
+ expect { @instance.delete_if {} }.to raise_error(NotImplementedError)
135
+ end
136
+ end
137
+
138
+ describe "#divide" do
139
+ it "isn't implemented" do
140
+ expect { @instance.divide {} }.to raise_error(NotImplementedError)
141
+ end
142
+ end
143
+
144
+ describe "#each" do
145
+ it "yields in order" do
146
+ @instance.score(:foo, 5)
147
+ @instance.score(:bar, 0)
148
+ @instance.score(:baz, 3)
149
+
150
+ yielder = double
151
+ yielder.should_receive(:call).with(:foo).ordered
152
+ yielder.should_receive(:call).with(:baz).ordered
153
+ yielder.should_receive(:call).with(:bar).ordered
154
+
155
+ @instance.each {|i| yielder.call(i)}
156
+ end
157
+ end
158
+
159
+ describe "#flatten" do
160
+ it "isn't implemented" do
161
+ expect { @instance.flatten }.to raise_error(NotImplementedError)
162
+ end
163
+ end
164
+
165
+ describe "#inspect" do
166
+ it "returns string" do
167
+ @instance.inc(:baz)
168
+ @instance.inspect.should == "<Ranking: :baz, :bar, :foo>"
169
+ end
170
+ end
171
+
172
+ describe "#keep_if" do
173
+ it "isn't implemented" do
174
+ expect { @instance.keep_if {} }.to raise_error(NotImplementedError)
175
+ end
176
+ end
177
+
178
+ describe "#merge" do
179
+ it "adds to set" do
180
+ @instance.merge Ranking[:baz]
181
+ @instance.should include(:baz)
182
+ end
183
+
184
+ it "adds to scores" do
185
+ @instance.merge Ranking[:baz].score(:baz,10)
186
+ @instance.scores[:baz].should == 10
187
+ end
188
+
189
+ it "merges scores with preference to other" do
190
+ @instance.merge Ranking[:bar].score(:bar,10)
191
+ @instance.scores[:bar].should == 10
192
+ end
193
+
194
+ it "accepts enumerable" do
195
+ @instance.merge [:bar, :baz]
196
+ @instance.should include(:baz)
197
+ end
198
+
199
+ it "retains existing scores if enumerable part of set" do
200
+ @instance.score(:bar, 10).merge [:bar, :baz]
201
+ @instance.scores[:bar].should == 10
202
+ end
203
+
204
+ it "defaults to zero if enumerable not part of set" do
205
+ @instance.merge [:bar, :baz]
206
+ @instance.scores[:baz].should == 0
207
+ end
208
+ end
209
+
210
+ describe "#reject!" do
211
+ it "isn't implemented" do
212
+ expect { @instance.reject! {} }.to raise_error(NotImplementedError)
213
+ end
214
+ end
215
+
216
+ describe "#replace" do
217
+ it "isn't implemented" do
218
+ expect { @instance.replace @replace }.to raise_error(NotImplementedError)
219
+ end
220
+ end
221
+
222
+ describe "#select!" do
223
+ it "isn't implemented" do
224
+ expect { @instance.select! {} }.to raise_error(NotImplementedError)
225
+ end
226
+ end
227
+
228
+ describe "#to_a" do
229
+ it "returns an array" do
230
+ @instance.to_a.should be_a(Array)
231
+ end
232
+
233
+ it "sorts by score" do
234
+ @instance.score(:foo, 5)
235
+ @instance.score(:bar, 0)
236
+ @instance.score(:baz, 3)
237
+
238
+ @instance.to_a.should == [:foo, :baz, :bar]
239
+ end
240
+
241
+ it "sorts by value if score equivalent" do
242
+ @instance.score(:foo, 5)
243
+ @instance.score(:bar, 0)
244
+ @instance.score(:qux, 2)
245
+ @instance.score(:baz, 2)
246
+
247
+ @instance.to_a.should == [:foo, :baz, :qux, :bar]
248
+ end
249
+ end
250
+ end
@@ -0,0 +1,3 @@
1
+ require 'rspec'
2
+ require 'pry'
3
+ require 'ranking'
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ranking
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ryan Michael
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.0'
30
+ description: ! 'Extends Set with a score. Sorts by score on #to_a, #each, etc'
31
+ email: kerinin@gmail.com
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - .document
37
+ - .gitignore
38
+ - .rspec
39
+ - ChangeLog.rdoc
40
+ - Gemfile
41
+ - LICENSE.txt
42
+ - README.md
43
+ - Rakefile
44
+ - lib/ranking.rb
45
+ - lib/ranking/instance_methods.rb
46
+ - lib/ranking/set_methods.rb
47
+ - ranking.gemspec
48
+ - spec/instance_methods_spec.rb
49
+ - spec/ranking_spec.rb
50
+ - spec/set_methods_spec.rb
51
+ - spec/spec_helper.rb
52
+ homepage: https://github.com/kerinin/ranking#readme
53
+ licenses:
54
+ - MIT
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ segments:
66
+ - 0
67
+ hash: 2123784621627134409
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ segments:
75
+ - 0
76
+ hash: 2123784621627134409
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 1.8.23
80
+ signing_key:
81
+ specification_version: 3
82
+ summary: Sorted sets using scores
83
+ test_files:
84
+ - spec/instance_methods_spec.rb
85
+ - spec/ranking_spec.rb
86
+ - spec/set_methods_spec.rb
87
+ - spec/spec_helper.rb