Class: HeadMusic::Notation::MusicXML::Preflight

Inherits:
Object
  • Object
show all
Includes:
PreflightChecks
Defined in:
lib/head_music/notation/music_xml/preflight.rb

Overview

Rejects compositions that cannot be expressed in the supported MusicXML subset.

Whole-composition problems (no voices, positional gaps, barline-crossing notes, forbidden control characters) raise RenderError here, before the Writer assembles any output — so a successful check! is the Writer’s guarantee that assembly cannot fail on these grounds.

Constant Summary collapse

FORBIDDEN_TEXT_CHARACTERS =

XML 1.0 forbids the C0 control characters other than tab, newline, and carriage return, even as character references.

/[\u0000-\u0008\u000B\u000C\u000E-\u001F]/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(composition) ⇒ Preflight

Returns a new instance of Preflight.



21
22
23
# File 'lib/head_music/notation/music_xml/preflight.rb', line 21

def initialize(composition)
  @composition = composition
end

Instance Attribute Details

#compositionObject (readonly, private)

Returns the value of attribute composition.



34
35
36
# File 'lib/head_music/notation/music_xml/preflight.rb', line 34

def composition
  @composition
end

Class Method Details

.check!(composition) ⇒ Object



17
18
19
# File 'lib/head_music/notation/music_xml/preflight.rb', line 17

def self.check!(composition)
  new(composition).check!
end

Instance Method Details

#check!Object



25
26
27
28
29
30
# File 'lib/head_music/notation/music_xml/preflight.rb', line 25

def check!
  ensure_voices
  ensure_renderable_text
  ensure_contiguous_voices(composition)
  ensure_notes_within_barlines(composition)
end

#ensure_contiguous_voices(composition) ⇒ Object (private) Originally defined in module PreflightChecks

#ensure_notes_within_barlines(composition) ⇒ Object (private) Originally defined in module PreflightChecks

#ensure_renderable_textObject (private)



42
43
44
45
46
47
48
49
# File 'lib/head_music/notation/music_xml/preflight.rb', line 42

def ensure_renderable_text
  texts = [composition.name, composition.composer] + composition.voices.map(&:role)
  texts.compact.each do |text|
    next unless text.to_s.match?(FORBIDDEN_TEXT_CHARACTERS)

    raise RenderError, "cannot render control characters in #{text.to_s.inspect} as XML text"
  end
end

#ensure_voicesObject (private)

Raises:



36
37
38
39
40
# File 'lib/head_music/notation/music_xml/preflight.rb', line 36

def ensure_voices
  return unless composition.voices.empty?

  raise RenderError, "cannot render a composition with no voices as MusicXML"
end

#raise_gap_error(voice, expected_position, found_placement) ⇒ Object (private) Originally defined in module PreflightChecks

Raises:

  • (render_error_class)

#render_error_classObject (private)



51
52
53
# File 'lib/head_music/notation/music_xml/preflight.rb', line 51

def render_error_class
  RenderError
end