Class: HeadMusic::Instruments::InstrumentFamily

Inherits:
Object
  • Object
show all
Includes:
Named
Defined in:
lib/head_music/instruments/instrument_family.rb

Overview

An InstrumentFamily is a species of instrument that may exist in a variety of keys or other variations. For example:

  • saxophone is an instrument family, while alto saxophone and baritone saxophone are specific instruments.
  • oboe is an instrument family, while oboe d'amore and English horn are specific instruments.

Instrument families are categorized by:

  • orchestra section (e.g. woodwind, brass, percussion, strings)
  • classification (e.g. bowed string, plucked string, double reed, single reed, brass, keyboard, electronic, percussion)

Instrument families are defined in lib/head_music/instruments/instrument_families.yml.

See Also:

Constant Summary collapse

INSTRUMENT_FAMILIES =
YAML.load_file(File.expand_path("instrument_families.yml", __dir__)).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ InstrumentFamily (private)

Returns a new instance of InstrumentFamily.



41
42
43
44
45
46
47
48
# File 'lib/head_music/instruments/instrument_family.rb', line 41

def initialize(name)
  record = record_for_name(name)
  if record
    initialize_data_from_record(record)
  else
    self.name = name.to_s
  end
end

Instance Attribute Details

#alias_name_keysObject (readonly) Originally defined in module Named

Returns the value of attribute alias_name_keys.

#classification_keysObject (readonly)

Returns the value of attribute classification_keys.



24
25
26
# File 'lib/head_music/instruments/instrument_family.rb', line 24

def classification_keys
  @classification_keys
end

#nameObject

Returns the value of attribute name.



25
26
27
# File 'lib/head_music/instruments/instrument_family.rb', line 25

def name
  @name
end

#name_keyObject (readonly)

Returns the value of attribute name_key.



24
25
26
# File 'lib/head_music/instruments/instrument_family.rb', line 24

def name_key
  @name_key
end

#name_keyObject (readonly) Originally defined in module Named

Returns the value of attribute name_key.

#orchestra_section_keyObject (readonly)

Returns the value of attribute orchestra_section_key.



24
25
26
# File 'lib/head_music/instruments/instrument_family.rb', line 24

def orchestra_section_key
  @orchestra_section_key
end

Class Method Details

.allObject



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

def self.all
  INSTRUMENT_FAMILIES.map { |key, _data| get(key) }
  @all ||= @instances.values.compact.sort_by(&:name)
end

.get(name) ⇒ Object



27
28
29
30
# File 'lib/head_music/instruments/instrument_family.rb', line 27

def self.get(name)
  result = get_by_name(name) || get_by_name(key_for_name(name))
  result || new(name)
end

Instance Method Details

#ensure_localized_name(name:, locale_code: Locale::DEFAULT_CODE, abbreviation: nil) ⇒ Object Originally defined in module Named

#inferred_nameObject (private)



79
80
81
# File 'lib/head_music/instruments/instrument_family.rb', line 79

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

#initialize_data_from_record(record) ⇒ Object (private)



72
73
74
75
76
77
# File 'lib/head_music/instruments/instrument_family.rb', line 72

def initialize_data_from_record(record)
  @name_key = record["name_key"].to_sym
  @orchestra_section_key = record["orchestra_section_key"]
  @classification_keys = record["classification_keys"] || []
  self.name = I18n.translate(name_key, scope: "head_music.instruments", locale: "en", default: inferred_name)
end

#key_for_name(name) ⇒ Object (private)



55
56
57
58
59
60
61
62
63
# File 'lib/head_music/instruments/instrument_family.rb', line 55

def key_for_name(name)
  INSTRUMENT_FAMILIES.each do |key, _data|
    I18n.config.available_locales.each do |locale|
      translation = I18n.t("head_music.instruments.#{key}", locale: locale)
      return key if translation.downcase == name.downcase
    end
  end
  nil
end

#localized_name(locale_code: Locale::DEFAULT_CODE) ⇒ Object Originally defined in module Named

#localized_name_in_default_localeObject (private) Originally defined in module Named

#localized_name_in_locale_matching_language(locale) ⇒ Object (private) Originally defined in module Named

#localized_name_in_matching_locale(locale) ⇒ Object (private) Originally defined in module Named

#localized_namesObject Originally defined in module Named

Returns an array of LocalizedName instances that are synonymous with the name.

#record_for_key(key) ⇒ Object (private)



65
66
67
68
69
70
# File 'lib/head_music/instruments/instrument_family.rb', line 65

def record_for_key(key)
  INSTRUMENT_FAMILIES.each do |name_key, data|
    return data.merge!("name_key" => name_key) if name_key.to_s == key.to_s
  end
  nil
end

#record_for_name(name) ⇒ Object (private)



50
51
52
53
# File 'lib/head_music/instruments/instrument_family.rb', line 50

def record_for_name(name)
  record_for_key(HeadMusic::Utilities::HashKey.for(name)) ||
    record_for_key(key_for_name(name))
end