weighted_score 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +120 -0
- data/Rakefile +14 -0
- data/lib/weighted_score/score.rb +13 -0
- data/lib/weighted_score/version.rb +3 -0
- data/lib/weighted_score/weighted_score.rb +23 -0
- data/lib/weighted_score.rb +6 -0
- data/spec/examples.rb +26 -0
- data/spec/spec_helper.rb +3 -0
- data/spec/weighted_score_spec.rb +53 -0
- data/weighted_score.gemspec +24 -0
- metadata +104 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: bf9b789f6fe7b367bb6e5d93f8f2530be7a73bd9
|
4
|
+
data.tar.gz: fe9f3e164943751f0aa47e35b0fb29a7e0181af5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cb13747fb4259622252300f5d10275ec830bf9482f8a41b44d81da1cacbfae2a4686e34ec19e69bd5442d1b7759967ab9456ccea833890f2ef6c6938591b5ba6
|
7
|
+
data.tar.gz: 5a417b1f8c45f70472bf3b4037b3e916e9e05157605d145ee7ffb94668f44b947f9aaaaaf11ba5cf9395ef2d59085c44cc5b4fba139cfbd1aa4d918f9589d87d
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 alexpeachey
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
# WeightedScore
|
2
|
+
|
3
|
+
Weighted Score provides a light weight framework for creating weighted
|
4
|
+
scoring systems. It allows you to calculate weighted scores using score
|
5
|
+
calculator classes for each of the weighted components.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'weighted_score'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install weighted_score
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Start by creating one or more score calculating classes.
|
24
|
+
You can optionally subclass `WeightedScore::Score`.
|
25
|
+
If you do not subclass the provided `Score` class then
|
26
|
+
you your initializer should accept one argument which represents
|
27
|
+
the scorable item and you must implement a `calculate` method.
|
28
|
+
|
29
|
+
**Either way the `calculate` method must return a numeric value.**
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
require 'weighted_score'
|
33
|
+
|
34
|
+
class CriteriaOne < WeightedScore::Score
|
35
|
+
def calculate
|
36
|
+
return 1 if scorable >= 10
|
37
|
+
0
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
class CriteriaTwo < WeightedScore::Score
|
42
|
+
def calculate
|
43
|
+
return 1 if scorable.even?
|
44
|
+
0
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
class CriteriaThree < WeightedScore::Score
|
49
|
+
def calculate
|
50
|
+
return 1 if scorable % 4 == 0
|
51
|
+
0
|
52
|
+
end
|
53
|
+
end
|
54
|
+
```
|
55
|
+
|
56
|
+
Then create a weighted score calculator class:
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
require 'weighted_score'
|
60
|
+
|
61
|
+
class Calculator < WeightedScore::WeightedScore
|
62
|
+
score CriteriaOne => 0.5
|
63
|
+
score CriteriaTwo => 0.25
|
64
|
+
score CriteriaThree => 0.25
|
65
|
+
end
|
66
|
+
```
|
67
|
+
|
68
|
+
Finally, you can then use it like this:
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
Calculator.new(5).calculate #=> 0
|
72
|
+
Calculator.new(2).calculate #=> 0.25
|
73
|
+
Calculator.new(4).calculate #=> 0.5
|
74
|
+
Calculator.new(12).calculate #=> 1
|
75
|
+
Calculator.new(13).calculate #=> 0.5
|
76
|
+
Calculator.new(14).calculate #=> 0.75
|
77
|
+
```
|
78
|
+
|
79
|
+
## An Example Customer Valuation Calculator
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
require 'weighted_score'
|
83
|
+
|
84
|
+
class EmailScore < WeightedScore::Score
|
85
|
+
def calculate
|
86
|
+
return 1 if scorable.email.present?
|
87
|
+
0
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
class NewsletterScore < WeightedScore::Score
|
92
|
+
def calculate
|
93
|
+
return 1 if scorable.newsletter_status == 'subscribed'
|
94
|
+
0
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
class OrdersScore < WeightedScore::Score
|
99
|
+
def calculate
|
100
|
+
scorable.orders.size - scorable.returns.size
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
class CustomerValuation < WeightedScore::WeightedScore
|
105
|
+
score EmailScore => 5
|
106
|
+
score NewsletterScore => 10
|
107
|
+
score OrdersScore => 1
|
108
|
+
end
|
109
|
+
|
110
|
+
CustomerValuation.new(user).calculate
|
111
|
+
```
|
112
|
+
|
113
|
+
|
114
|
+
## Contributing
|
115
|
+
|
116
|
+
1. Fork it ( https://github.com/Originate/weighted_score/fork )
|
117
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
118
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
119
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
120
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
module WeightedScore
|
2
|
+
class WeightedScore
|
3
|
+
attr_reader :scorable
|
4
|
+
|
5
|
+
def initialize(scorable)
|
6
|
+
@scorable = scorable
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.score_map
|
10
|
+
@score_map ||= {}
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.score(score_value)
|
14
|
+
@score_map = score_map.merge score_value
|
15
|
+
end
|
16
|
+
|
17
|
+
def calculate
|
18
|
+
self.class.score_map.keys.reduce(0) do |total, score|
|
19
|
+
total += score.new(scorable).calculate * self.class.score_map[score]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/spec/examples.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
class CriteriaOne < WeightedScore::Score
|
2
|
+
def calculate
|
3
|
+
return 1 if scorable >= 10
|
4
|
+
0
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
class CriteriaTwo < WeightedScore::Score
|
9
|
+
def calculate
|
10
|
+
return 1 if scorable.even?
|
11
|
+
0
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class CriteriaThree < WeightedScore::Score
|
16
|
+
def calculate
|
17
|
+
return 1 if scorable % 4 == 0
|
18
|
+
0
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class Calculator < WeightedScore::WeightedScore
|
23
|
+
score CriteriaOne => 0.5
|
24
|
+
score CriteriaTwo => 0.25
|
25
|
+
score CriteriaThree => 0.25
|
26
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Exercising the scoring system' do
|
4
|
+
subject(:calculator) { Calculator.new(test_case) }
|
5
|
+
|
6
|
+
describe '#calculate' do
|
7
|
+
context 'when the test_case is 5' do
|
8
|
+
let(:test_case) { 5 }
|
9
|
+
|
10
|
+
specify { expect(calculator.calculate).to eq 0 }
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe '#calculate' do
|
15
|
+
context 'when the test_case is 2' do
|
16
|
+
let(:test_case) { 2 }
|
17
|
+
|
18
|
+
specify { expect(calculator.calculate).to eq 0.25 }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe '#calculate' do
|
23
|
+
context 'when the test_case is 4' do
|
24
|
+
let(:test_case) { 4 }
|
25
|
+
|
26
|
+
specify { expect(calculator.calculate).to eq 0.5 }
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe '#calculate' do
|
31
|
+
context 'when the test_case is 12' do
|
32
|
+
let(:test_case) { 12 }
|
33
|
+
|
34
|
+
specify { expect(calculator.calculate).to eq 1 }
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe '#calculate' do
|
39
|
+
context 'when the test_case is 13' do
|
40
|
+
let(:test_case) { 13 }
|
41
|
+
|
42
|
+
specify { expect(calculator.calculate).to eq 0.5 }
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe '#calculate' do
|
47
|
+
context 'when the test_case is 14' do
|
48
|
+
let(:test_case) { 14 }
|
49
|
+
|
50
|
+
specify { expect(calculator.calculate).to eq 0.75 }
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'weighted_score/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'weighted_score'
|
8
|
+
spec.version = WeightedScore::VERSION
|
9
|
+
spec.authors = ['alexpeachey']
|
10
|
+
spec.email = ['alex.peachey@gmail.com']
|
11
|
+
spec.summary = %q{Calculate Weighted Scores}
|
12
|
+
spec.description = %q{Calculate Weighted Scores}
|
13
|
+
spec.homepage = 'https://github.com/Originate/weighted_score'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.6'
|
22
|
+
spec.add_development_dependency 'rake'
|
23
|
+
spec.add_development_dependency 'rspec'
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: weighted_score
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- alexpeachey
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-07-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Calculate Weighted Scores
|
56
|
+
email:
|
57
|
+
- alex.peachey@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- lib/weighted_score.rb
|
70
|
+
- lib/weighted_score/score.rb
|
71
|
+
- lib/weighted_score/version.rb
|
72
|
+
- lib/weighted_score/weighted_score.rb
|
73
|
+
- spec/examples.rb
|
74
|
+
- spec/spec_helper.rb
|
75
|
+
- spec/weighted_score_spec.rb
|
76
|
+
- weighted_score.gemspec
|
77
|
+
homepage: https://github.com/Originate/weighted_score
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
metadata: {}
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.2.2
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: Calculate Weighted Scores
|
101
|
+
test_files:
|
102
|
+
- spec/examples.rb
|
103
|
+
- spec/spec_helper.rb
|
104
|
+
- spec/weighted_score_spec.rb
|