Class: HeadMusic::Instruments::InstrumentFamily

Inherits:
Object
  • Object
show all
Includes:
CatalogLookup, 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.



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

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.



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

def classification_keys
  @classification_keys
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#name_keyObject (readonly)

Returns the value of attribute name_key.



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

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.



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

def orchestra_section_key
  @orchestra_section_key
end

Class Method Details

.allObject



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

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

.get(name) ⇒ Object



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

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

Instance Method Details

#catalogObject (private)



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

def catalog
  INSTRUMENT_FAMILIES
end

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

#inferred_nameObject (private)



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

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

#initialize_data_from_record(record) ⇒ Object (private)



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

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) Originally defined in module CatalogLookup

#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)



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

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)



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

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