was 0.2.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +33 -23
- data/lib/was/score.rb +15 -2
- data/lib/was/version.rb +1 -1
- data/lib/was.rb +2 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 116f56df4be0366c5cefda0647c4a59f4300ad8f84b76e39c5c3563ef97abd82
|
4
|
+
data.tar.gz: e3b5304f1f46fac185b4a92ec1201c09bdfc864576332a33614a946606a31afc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6ea32808ff515334b197f87ec7dfa8d3ed699b281cff0a041f88ef000884617498eb22a6ce90aec66ee793ef136b4f3939d371b5abff9b4f28b3a3dfbe880f75
|
7
|
+
data.tar.gz: 124c909b89e6ce6b9bb65064bc824c70d8489e9510915a722f8d82919d3e4818f2aebade83d2381f12c121591797eb5937ebaf7b61e05425b6ef93b9e6b12993
|
data/README.md
CHANGED
@@ -20,36 +20,36 @@ Scenario:
|
|
20
20
|
### Define score classes
|
21
21
|
|
22
22
|
```ruby
|
23
|
-
|
23
|
+
require "was"
|
24
24
|
|
25
|
-
|
26
|
-
|
25
|
+
class ReportScore < WAS::Score
|
26
|
+
maximum_score 1000
|
27
27
|
|
28
|
-
|
29
|
-
|
30
|
-
|
28
|
+
with "ExamScore", weight: 0.75
|
29
|
+
with "PracticalScore", weight: 0.25
|
30
|
+
end
|
31
31
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
end
|
32
|
+
class ExamScore < WAS::Score
|
33
|
+
def calculation
|
34
|
+
return 1 if input == "A"
|
35
|
+
return 0.75 if input == "B"
|
36
|
+
return 0.5 if input == "C"
|
37
|
+
0
|
39
38
|
end
|
39
|
+
end
|
40
40
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
end
|
41
|
+
class PracticalScore < WAS::Score
|
42
|
+
def calculation
|
43
|
+
input / 10.0
|
45
44
|
end
|
45
|
+
end
|
46
46
|
```
|
47
47
|
|
48
48
|
### Generate a total score
|
49
49
|
|
50
50
|
```ruby
|
51
51
|
ReportScore.new(
|
52
|
-
exam: "A"
|
52
|
+
exam: "A",
|
53
53
|
practical: 10
|
54
54
|
).calculate
|
55
55
|
#> 1000
|
@@ -66,16 +66,26 @@ ReportScore.new(
|
|
66
66
|
Omitting the `maximum_score` will return a composed percentage represented as a value between `0` and `1`.
|
67
67
|
|
68
68
|
```ruby
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
69
|
+
# report_score.rb
|
70
|
+
class ReportScore < WAS::Score
|
71
|
+
with "ExamScore", weight: 0.75
|
72
|
+
with "PracticalScore", weight: 0.25
|
73
|
+
end
|
73
74
|
```
|
74
75
|
|
75
76
|
```ruby
|
76
|
-
|
77
|
+
# usage
|
78
|
+
ReportScore.new({
|
77
79
|
exam: "A",
|
78
80
|
practical: 5
|
79
81
|
}).calculate
|
80
82
|
#> 0.875
|
81
83
|
```
|
84
|
+
|
85
|
+
### View all weights
|
86
|
+
|
87
|
+
If you want to see all of the weights that are used to compose the score, there is a convenience method `.weights`:
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
ReportScore.weights
|
91
|
+
#> {exam: {weight: 0.75}, practical: {weight: 0.25}}
|
data/lib/was/score.rb
CHANGED
@@ -18,6 +18,19 @@ module WAS
|
|
18
18
|
@klasses ||= {}
|
19
19
|
end
|
20
20
|
|
21
|
+
def self.weights
|
22
|
+
{}.tap do |hash|
|
23
|
+
klasses.each do |klass, weight|
|
24
|
+
hash[klass_name_symbol(klass)] = { weight: weight }
|
25
|
+
if Object.const_get(klass).klasses.length > 0
|
26
|
+
hash[klass_name_symbol(klass)].merge!(
|
27
|
+
with: Object.const_get(klass).weights
|
28
|
+
)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
21
34
|
def initialize(input)
|
22
35
|
@input = input
|
23
36
|
end
|
@@ -28,14 +41,14 @@ module WAS
|
|
28
41
|
|
29
42
|
def calculation
|
30
43
|
self.class.klasses.sum do |klass, weight|
|
31
|
-
score = Object.const_get(klass).new(input[klass_name_symbol(klass)]).calculate
|
44
|
+
score = Object.const_get(klass).new(input[self.class.klass_name_symbol(klass)]).calculate
|
32
45
|
score * weight
|
33
46
|
end * self.class.max_score
|
34
47
|
end
|
35
48
|
|
36
49
|
private
|
37
50
|
|
38
|
-
def klass_name_symbol(klass)
|
51
|
+
def self.klass_name_symbol(klass)
|
39
52
|
klass[/(\w+)Score$/, 1].downcase.to_sym
|
40
53
|
end
|
41
54
|
end
|
data/lib/was/version.rb
CHANGED
data/lib/was.rb
CHANGED