was 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 35789d2f200b0436e67730bf7e2811cd61665f6bef985bc1f2cb091ed1797a5b
4
- data.tar.gz: efd553a27afd03bb2da2443d80945239b7e22487dac6e850982d498ec32d5d01
3
+ metadata.gz: 32828b1a84ec32fd591c751dcb5df91f277af4d6bc840179b9070345f3345e24
4
+ data.tar.gz: 57bfb59c5ee299e3e0ed0376e44b4ad0c62f98ca23574cf366fc5f7461a1dd95
5
5
  SHA512:
6
- metadata.gz: 630fc202315d1b3d90c28214f97d23ba66f7f5daba3a43651cf720d21a7ece94c6d2533715267933edc2f6b18f662301d71d6590c7d9db655aa30dbcc90703d6
7
- data.tar.gz: 9e95ffaddcbae72fba82c4957b7ac20bb6db44bc219c6e13b445281799aafead6a9c722b0a7235975d737f877828da63dfc70e0284b65288e284e0245b54614a
6
+ metadata.gz: e006ec3f57273eafe64336a6ba55efc358bed7064a11be9ff67089a147b3683e8452c447d09caf59f306a573265ab52bc4f1fd411f25e5020b3773a55355df4d
7
+ data.tar.gz: 24f01eb4452fa8b691bbcf1c639cd16ae72d53f10920f2482281ef853ce0b0f9b792aea36c3a7498a2caf22f2ced4aecc3fa5f0e8bf46d9ee2d387c4234cbfeb
data/README.md CHANGED
@@ -30,16 +30,25 @@ class ReportScore < WAS::Score
30
30
  end
31
31
 
32
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"
33
+ context :grade_a, score: 1 do |input|
34
+ input == "A"
35
+ end
36
+
37
+ context :grade_b, score: 0.75 do |input|
38
+ input == "B"
39
+ end
40
+
41
+ context :grade_c, score: 0.5 do |input|
42
+ input == "C"
43
+ end
44
+
45
+ context :flunk do
37
46
  0
38
47
  end
39
48
  end
40
49
 
41
50
  class PracticalScore < WAS::Score
42
- def calculation
51
+ context :score do |input|
43
52
  input / 10.0
44
53
  end
45
54
  end
data/lib/was/score.rb CHANGED
@@ -40,20 +40,51 @@ module WAS
40
40
  @input = input
41
41
  end
42
42
 
43
- def calculate
44
- calculation
43
+ def calculate(option = nil)
44
+ return calculation if option != :tree
45
+
46
+ calc = calculation(:tree)
47
+ tree = if calc.is_a?(Hash)
48
+ calc.merge({ max: self.class.max_score })
49
+ else
50
+ { score: calc }.merge({ max: self.class.max_score })
51
+ end
52
+
53
+ transform_scores_relative_to_max_score(tree)
45
54
  end
46
55
 
47
- def calculation
56
+ def calculation(option = nil)
48
57
  if contexts?
49
58
  context_score_calculation
50
59
  else
51
- nested_score_calcuation
60
+ nested_score_calcuation(option)
52
61
  end
53
62
  end
54
63
 
55
64
  private
56
65
 
66
+ def transform_scores_relative_to_max_score(tree, max_score = nil)
67
+ max_score = max_score || self.class.max_score
68
+ return tree if self.class.max_score == 1
69
+
70
+ tree.each do |key, value|
71
+ next if key != :with
72
+
73
+ value.each do |scorer, branch|
74
+ adjust_branch_score_and_max(branch, max_score)
75
+ end
76
+
77
+ value.transform_values! do |nested_tree|
78
+ transform_scores_relative_to_max_score(nested_tree, nested_tree[:max])
79
+ end
80
+ end
81
+ end
82
+
83
+ def adjust_branch_score_and_max(branch, max_score)
84
+ branch[:max] = branch[:max] * max_score * branch[:weight]
85
+ branch[:score] = branch[:score] * branch[:max]
86
+ end
87
+
57
88
  def contexts?
58
89
  !!self.class.instance_variable_get("@contexts")
59
90
  end
@@ -61,16 +92,33 @@ module WAS
61
92
  def context_score_calculation
62
93
  self.class.instance_variable_get("@contexts").each do |context|
63
94
  output = context[:code].call(input)
64
- next unless output
95
+ next if !output
65
96
  return context[:score] || output
66
97
  end
67
98
  end
68
99
 
69
- def nested_score_calcuation
70
- self.class.scorers.sum do |name, scorer|
100
+ def nested_score_calcuation(option)
101
+ if option == :tree
102
+ { score: sum, with: with_attribute }
103
+ else
104
+ sum
105
+ end
106
+ end
107
+
108
+ def sum
109
+ @sum ||= self.class.scorers.sum do |name, scorer|
71
110
  score = Object.const_get(scorer[:class_name]).new(input[name.to_sym]).calculate
72
111
  score * scorer[:weight]
73
112
  end * self.class.max_score
74
113
  end
114
+
115
+ def with_attribute
116
+ {}.tap do |with|
117
+ self.class.scorers.each do |name, scorer|
118
+ with[name] = Object.const_get(scorer[:class_name]).new(input[name.to_sym]).calculate(:tree)
119
+ with[name][:weight] = scorer[:weight]
120
+ end
121
+ end
122
+ end
75
123
  end
76
124
  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.6.0"
4
+ VERSION = "0.7.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.6.0
4
+ version: 0.7.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-18 00:00:00.000000000 Z
11
+ date: 2025-02-20 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A simple gem/dsl for generating Weighted Average Score calculations.
14
14
  email: