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.

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.



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

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.



14
15
16
# File 'lib/head_music/instruments/instrument_family.rb', line 14

def classification_keys
  @classification_keys
end

#nameObject

Returns the value of attribute name.



15
16
17
# File 'lib/head_music/instruments/instrument_family.rb', line 15

def name
  @name
end

#name_keyObject (readonly)

Returns the value of attribute name_key.



14
15
16
# File 'lib/head_music/instruments/instrument_family.rb', line 14

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.



14
15
16
# File 'lib/head_music/instruments/instrument_family.rb', line 14

def orchestra_section_key
  @orchestra_section_key
end

Class Method Details

.allObject



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

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

.get(name) ⇒ Object



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

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)



69
70
71
# File 'lib/head_music/instruments/instrument_family.rb', line 69

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

#initialize_data_from_record(record) ⇒ Object (private)



62
63
64
65
66
67
# File 'lib/head_music/instruments/instrument_family.rb', line 62

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)



45
46
47
48
49
50
51
52
53
# File 'lib/head_music/instruments/instrument_family.rb', line 45

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)



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

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)



40
41
42
43
# File 'lib/head_music/instruments/instrument_family.rb', line 40

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