Class: WAS::Score

Inherits:
Object
  • Object
show all
Defined in:
lib/was/score.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Score

Returns a new instance of Score.



39
40
41
# File 'lib/was/score.rb', line 39

def initialize(input)
  @input = input
end

Instance Attribute Details

#inputObject (readonly)

Returns the value of attribute input.



3
4
5
# File 'lib/was/score.rb', line 3

def input
  @input
end

Class Method Details

.context(name, score: nil, &block) ⇒ Object



34
35
36
37
# File 'lib/was/score.rb', line 34

def self.context(name, score: nil, &block)
  @contexts ||= []
  @contexts.push({ name: name, score: score, code: block })
end

.max_scoreObject



9
10
11
# File 'lib/was/score.rb', line 9

def self.max_score
  @maximum_score || 1
end

.maximum_score(value) ⇒ Object



5
6
7
# File 'lib/was/score.rb', line 5

def self.maximum_score(value)
  @maximum_score = value
end

.scorersObject



17
18
19
# File 'lib/was/score.rb', line 17

def self.scorers
  @scorers ||= {}
end

.weightsObject



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/was/score.rb', line 21

def self.weights
  {}.tap do |hash|
    scorers.each do |name, scorer|
      hash[name] = { weight: scorer[:weight] }
      if Object.const_get(scorer[:class_name]).scorers.length > 0
        hash[name].merge!(
          with: Object.const_get(scorer[:class_name]).weights
        )
      end
    end
  end
end

.with(name, class_name: klass, weight: 0) ⇒ Object



13
14
15
# File 'lib/was/score.rb', line 13

def self.with(name, class_name: klass, weight: 0)
  scorers.merge!(name => { class_name: class_name, weight: weight })
end

Instance Method Details

#calculate(option = nil) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/was/score.rb', line 43

def calculate(option = nil)
  return calculation if option != :tree

  calc = calculation(:tree)
  tree = if calc.is_a?(Hash)
    calc.merge(additional_score_attributes(calc[:score]))
  else
    {}.tap do |t|
      t.merge!(score: calc)
      t.merge!(additional_score_attributes(calc))
    end
  end

  transform_scores_relative_to_max_score(tree)
end

#calculation(option = nil) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/was/score.rb', line 59

def calculation(option = nil)
  if contexts?
    context_score_calculation
  else
    nested_score_calcuation(option)
  end
end