Class: HeadMusic::Style::Analysis

Inherits:
Object
  • Object
show all
Defined in:
lib/head_music/style/analysis.rb

Overview

An analysis of music according to a style guide.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(guide, voice) ⇒ Analysis

Returns a new instance of Analysis.



8
9
10
11
# File 'lib/head_music/style/analysis.rb', line 8

def initialize(guide, voice)
  @guide = guide
  @voice = voice
end

Instance Attribute Details

#guideObject (readonly)

Returns the value of attribute guide.



6
7
8
# File 'lib/head_music/style/analysis.rb', line 6

def guide
  @guide
end

#voiceObject (readonly)

Returns the value of attribute voice.



6
7
8
# File 'lib/head_music/style/analysis.rb', line 6

def voice
  @voice
end

Instance Method Details

#adherent?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/head_music/style/analysis.rb', line 31

def adherent?
  annotations.all?(&:adherent?)
end

#annotationsObject



18
19
20
# File 'lib/head_music/style/analysis.rb', line 18

def annotations
  @annotations ||= @guide.analyze(voice)
end

#fitnessObject

The grade: sufficiency gates multiply against a weighted average of the rubric rules, so an insufficient exercise scales the whole grade down while ordinary rules trade off against each other by weight.



25
26
27
28
29
# File 'lib/head_music/style/analysis.rb', line 25

def fitness
  return 1.0 if annotations.empty?

  @fitness ||= gate_factor * rubric_fitness
end

#gate_factorObject (private)



37
38
39
# File 'lib/head_music/style/analysis.rb', line 37

def gate_factor
  gates.map(&:fitness).reduce(1, :*)
end

#gatesObject (private)



49
50
51
# File 'lib/head_music/style/analysis.rb', line 49

def gates
  annotations.select(&:gate?)
end

#messagesObject Also known as: annotation_messages



13
14
15
# File 'lib/head_music/style/analysis.rb', line 13

def messages
  annotations.reject(&:adherent?).map(&:message)
end

#rubric_fitnessObject (private)



41
42
43
44
45
46
47
# File 'lib/head_music/style/analysis.rb', line 41

def rubric_fitness
  rubric = annotations.reject(&:gate?)
  total_weight = rubric.sum(&:weight)
  return 1.0 if rubric.empty? || total_weight.zero?

  rubric.sum { |annotation| annotation.weight * annotation.fitness } / total_weight
end