was 0.1.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.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +48 -17
  3. data/lib/was/score.rb +16 -3
  4. data/lib/was/version.rb +1 -1
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 99421529aa08811e583cff20a0e7d41e110aaad70976b8ef6e52a7bda38feab6
4
- data.tar.gz: 70b2035d1d5b2ee5937810efd7f337b9133e0b87b5f68434ece9bc796ffb16dd
3
+ metadata.gz: 02d43144047e0f7e1da7df05778d6e1b075314ca447ce1f3440c65f38d65f121
4
+ data.tar.gz: 3a858c1928e5408b9134ce98f9471677fe82ea89fa9d6df32ce5c541b172da6e
5
5
  SHA512:
6
- metadata.gz: d5cff77d12a2a61eff5c1a0c131bfbc831e43c0b67d9761b9f44146a1307d6d2c08165f28e1a4d644b19c29b582e0479390135818dade2f344a24c323b8fb3eb
7
- data.tar.gz: cbfe391a566ffe9b0ce431d3975e4abfdace7dc3194f3fa9b35430a50d571a81e7c0b9baadd1dfe2f29f2e1c8ad59001e6165d41cf9a3943f25e2615c9f42769
6
+ metadata.gz: 5b18c6c1dd741eeb28df70a71a1a25f3c41cd4d7b01372115087c50c6b8f85fa748cced6d33b5792aabf960801b6fd86d145d89535b7b59d954a188125e608f6
7
+ data.tar.gz: dff3ba39633f5baf30fd853da49fb09ed3ab56e586a6e3d528859c723e92ed1dabf197cb50c897499f4112141dbfd136bb584a276893f8cd283da0f68e22b4da
data/README.md CHANGED
@@ -20,34 +20,36 @@ Scenario:
20
20
  ### Define score classes
21
21
 
22
22
  ```ruby
23
- class ReportScore < WAS::Score
24
- maximum_score 1000
23
+ require "was"
25
24
 
26
- with "ExamScore", weight: 0.75
27
- with "PracticalScore", weight: 0.25
28
- end
25
+ class ReportScore < WAS::Score
26
+ maximum_score 1000
27
+
28
+ with "ExamScore", weight: 0.75
29
+ with "PracticalScore", weight: 0.25
30
+ end
29
31
 
30
- class ExamScore < WAS::Score
31
- def calculation
32
- return 1 if input == "A"
33
- return 0.75 if input == "B"
34
- return 0.5 if input == "C"
35
- 0
36
- 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
37
38
  end
39
+ end
38
40
 
39
- class PracticalScore < WAS::Score
40
- def calculation
41
- input / 10.0
42
- end
41
+ class PracticalScore < WAS::Score
42
+ def calculation
43
+ input / 10.0
43
44
  end
45
+ end
44
46
  ```
45
47
 
46
48
  ### Generate a total score
47
49
 
48
50
  ```ruby
49
51
  ReportScore.new(
50
- exam: "A"
52
+ exam: "A",
51
53
  practical: 10
52
54
  ).calculate
53
55
  #> 1000
@@ -58,3 +60,32 @@ ReportScore.new(
58
60
  ).calculate
59
61
  #> 250
60
62
  ````
63
+
64
+ ### Compose without generating a total score
65
+
66
+ Omitting the `maximum_score` will return a composed percentage represented as a value between `0` and `1`.
67
+
68
+ ```ruby
69
+ # report_score.rb
70
+ class ReportScore < WAS::Score
71
+ with "ExamScore", weight: 0.75
72
+ with "PracticalScore", weight: 0.25
73
+ end
74
+ ```
75
+
76
+ ```ruby
77
+ # usage
78
+ ReportScore.new({
79
+ exam: "A",
80
+ practical: 5
81
+ }).calculate
82
+ #> 0.875
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
@@ -7,7 +7,7 @@ module WAS
7
7
  end
8
8
 
9
9
  def self.max_score
10
- @maximum_score
10
+ @maximum_score || 1
11
11
  end
12
12
 
13
13
  def self.with(klass, weight: 0)
@@ -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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module WAS
4
- VERSION = "0.1.0"
4
+ VERSION = "0.3.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: was
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mark Connell
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-02-10 00:00:00.000000000 Z
11
+ date: 2025-02-11 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A simple gem/dsl for generating Weighted Average Score calculations.
14
14
  email: