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.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +33 -23
  3. data/lib/was/score.rb +15 -2
  4. data/lib/was/version.rb +1 -1
  5. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dec886b6732bed51319bb2b25188fdb8bfaebc1a731ed2586b3b048d06e31253
4
- data.tar.gz: 15a9386c210b0b7f996b1c96890bfaf20b5006e9c492f6078ad55195b34654c7
3
+ metadata.gz: 02d43144047e0f7e1da7df05778d6e1b075314ca447ce1f3440c65f38d65f121
4
+ data.tar.gz: 3a858c1928e5408b9134ce98f9471677fe82ea89fa9d6df32ce5c541b172da6e
5
5
  SHA512:
6
- metadata.gz: 2eceb9767ae5b5e96117c59dc5d38170fdeb01947b6a4dc3691040065e750dd5e83c9d18c38c79f10ecb72bf4167de4dcae2702c466f1b5810a71d4122e21369
7
- data.tar.gz: 29b21a7e3e39272fae787029e7e4c73cf2bc74abfe3369142c08b98eb1f0613c40c19738e2741094de743cfa68496ffa416837d29454e856ce2ba9b3b6172dff
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
- require "was"
23
+ require "was"
24
24
 
25
- class ReportScore < WAS::Score
26
- maximum_score 1000
25
+ class ReportScore < WAS::Score
26
+ maximum_score 1000
27
27
 
28
- with "ExamScore", weight: 0.75
29
- with "PracticalScore", weight: 0.25
30
- end
28
+ with "ExamScore", weight: 0.75
29
+ with "PracticalScore", weight: 0.25
30
+ end
31
31
 
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
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
- class PracticalScore < WAS::Score
42
- def calculation
43
- input / 10.0
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
- class ComposedScore < WAS::Score
70
- with "ExamScore", weight: 0.75
71
- with "PracticalScore", weight: 0.25
72
- end
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
- ComposedScore.new({
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module WAS
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: was
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mark Connell