was 0.2.0 → 0.3.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.
- checksums.yaml +4 -4
- data/README.md +33 -23
- data/lib/was/score.rb +15 -2
- data/lib/was/version.rb +1 -1
- 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: 02d43144047e0f7e1da7df05778d6e1b075314ca447ce1f3440c65f38d65f121
|
4
|
+
data.tar.gz: 3a858c1928e5408b9134ce98f9471677fe82ea89fa9d6df32ce5c541b172da6e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5b18c6c1dd741eeb28df70a71a1a25f3c41cd4d7b01372115087c50c6b8f85fa748cced6d33b5792aabf960801b6fd86d145d89535b7b59d954a188125e608f6
|
7
|
+
data.tar.gz: dff3ba39633f5baf30fd853da49fb09ed3ab56e586a6e3d528859c723e92ed1dabf197cb50c897499f4112141dbfd136bb584a276893f8cd283da0f68e22b4da
|
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