Class: HeadMusic::Instruments::InstrumentName

Inherits:
Object
  • Object
show all
Defined in:
lib/head_music/instruments/instrument_name.rb

Overview

Builds an instrument’s localized display name from its catalog keys.

The name is, in order of preference: 1. an explicit translation of the instrument’s own key, 2. a name composed from the parent instrument and pitch for a child instrument (e.g. “Clarinet in B♭”), or 3. a plain inference from the key (“bass_clarinet” -> “bass clarinet”).

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name_key:, parent_key:, pitch_designation:) ⇒ InstrumentName

Returns a new instance of InstrumentName.



17
18
19
20
21
# File 'lib/head_music/instruments/instrument_name.rb', line 17

def initialize(name_key:, parent_key:, pitch_designation:)
  @name_key = name_key
  @parent_key = parent_key
  @pitch_designation = pitch_designation
end

Instance Attribute Details

#name_keyObject (readonly, private)

Returns the value of attribute name_key.



29
30
31
# File 'lib/head_music/instruments/instrument_name.rb', line 29

def name_key
  @name_key
end

#parent_keyObject (readonly, private)

Returns the value of attribute parent_key.



29
30
31
# File 'lib/head_music/instruments/instrument_name.rb', line 29

def parent_key
  @parent_key
end

#pitch_designationObject (readonly, private)

Returns the value of attribute pitch_designation.



29
30
31
# File 'lib/head_music/instruments/instrument_name.rb', line 29

def pitch_designation
  @pitch_designation
end

Class Method Details

.translate(key, locale: "en", default: nil) ⇒ Object

Localized name for an instrument key under the head_music.instruments scope.



13
14
15
# File 'lib/head_music/instruments/instrument_name.rb', line 13

def self.translate(key, locale: "en", default: nil)
  I18n.translate(key, scope: %i[head_music instruments], locale: locale, default: default)
end

Instance Method Details

#child_instrument_nameObject (private)

Name built from parent + pitch for child instruments, e.g. “Clarinet in B♭”.



32
33
34
35
36
# File 'lib/head_music/instruments/instrument_name.rb', line 32

def child_instrument_name
  return nil unless parent_key && pitch_designation

  "#{parent_translation} in #{format_pitch_name(pitch_designation)}"
end

#format_pitch_name(pitch_designation) ⇒ Object (private)



46
47
48
# File 'lib/head_music/instruments/instrument_name.rb', line 46

def format_pitch_name(pitch_designation)
  pitch_designation.to_s.tr("b", "").tr("#", "")
end

#inferred_nameObject (private)



42
43
44
# File 'lib/head_music/instruments/instrument_name.rb', line 42

def inferred_name
  name_key.to_s.tr("_", " ")
end

#parent_translationObject (private)



38
39
40
# File 'lib/head_music/instruments/instrument_name.rb', line 38

def parent_translation
  self.class.translate(parent_key, default: parent_key.to_s.tr("_", " "))
end

#to_sObject



23
24
25
# File 'lib/head_music/instruments/instrument_name.rb', line 23

def to_s
  self.class.translate(name_key) || child_instrument_name || inferred_name
end