Class: InciScore::Computer

Inherits:
Object
  • Object
show all
Defined in:
lib/inci_score/computer.rb

Constant Summary collapse

TOLERANCE =
30.0
DECIMALS =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(src:, rules: Normalizer::DEFAULT_RULES) ⇒ Computer



10
11
12
13
14
15
16
17
18
# File 'lib/inci_score/computer.rb', line 10

def initialize(src:, rules: Normalizer::DEFAULT_RULES)
  @unrecognized = []
  @precisions = []
  @src = src
  @rules = rules
  @ingredients = Normalizer.new(src: src, rules: rules).call
  @components = fetch_components
  freeze
end

Instance Attribute Details

#componentsObject (readonly)

Returns the value of attribute components.



8
9
10
# File 'lib/inci_score/computer.rb', line 8

def components
  @components
end

#ingredientsObject (readonly)

Returns the value of attribute ingredients.



8
9
10
# File 'lib/inci_score/computer.rb', line 8

def ingredients
  @ingredients
end

#precisionsObject (readonly)

Returns the value of attribute precisions.



8
9
10
# File 'lib/inci_score/computer.rb', line 8

def precisions
  @precisions
end

#rulesObject (readonly)

Returns the value of attribute rules.



8
9
10
# File 'lib/inci_score/computer.rb', line 8

def rules
  @rules
end

#srcObject (readonly)

Returns the value of attribute src.



8
9
10
# File 'lib/inci_score/computer.rb', line 8

def src
  @src
end

#unrecognizedObject (readonly)

Returns the value of attribute unrecognized.



8
9
10
# File 'lib/inci_score/computer.rb', line 8

def unrecognized
  @unrecognized
end

Instance Method Details

#callObject



20
21
22
23
24
25
# File 'lib/inci_score/computer.rb', line 20

def call
  Response.new(components: components,
               unrecognized: unrecognized,
               score: score,
               precision: precision)
end

#precisionObject



31
32
33
# File 'lib/inci_score/computer.rb', line 31

def precision
  (precisions.sum / ingredients.size).round(DECIMALS)
end

#scoreObject



27
28
29
# File 'lib/inci_score/computer.rb', line 27

def score
  Scorer.new(components.map(&:hazard)).call.round(DECIMALS)
end

#valid?Boolean



35
36
37
# File 'lib/inci_score/computer.rb', line 35

def valid?
  precision >= TOLERANCE
end