Class: HeadMusic::Instruments::InstrumentFamily
- Inherits:
-
Object
- Object
- HeadMusic::Instruments::InstrumentFamily
- 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.
Constant Summary collapse
- INSTRUMENT_FAMILIES =
YAML.load_file(File.("instrument_families.yml", __dir__)).freeze
Instance Attribute Summary collapse
-
#alias_name_keys ⇒ Object
included
from Named
readonly
Returns the value of attribute alias_name_keys.
-
#classification_keys ⇒ Object
readonly
Returns the value of attribute classification_keys.
-
#name ⇒ Object
Returns the value of attribute name.
-
#name_key ⇒ Object
readonly
Returns the value of attribute name_key.
-
#name_key ⇒ Object
included
from Named
readonly
Returns the value of attribute name_key.
-
#orchestra_section_key ⇒ Object
readonly
Returns the value of attribute orchestra_section_key.
Class Method Summary collapse
Instance Method Summary collapse
- #ensure_localized_name(name:, locale_code: Locale::DEFAULT_CODE, abbreviation: nil) ⇒ Object included from Named
- #inferred_name ⇒ Object private
-
#initialize(name) ⇒ InstrumentFamily
constructor
private
A new instance of InstrumentFamily.
- #initialize_data_from_record(record) ⇒ Object private
- #key_for_name(name) ⇒ Object private
- #localized_name(locale_code: Locale::DEFAULT_CODE) ⇒ Object included from Named
- #localized_name_in_default_locale ⇒ Object included from Named private
- #localized_name_in_locale_matching_language(locale) ⇒ Object included from Named private
- #localized_name_in_matching_locale(locale) ⇒ Object included from Named private
-
#localized_names ⇒ Object
included
from Named
Returns an array of LocalizedName instances that are synonymous with the name.
- #record_for_key(key) ⇒ Object private
- #record_for_name(name) ⇒ Object private
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_keys ⇒ Object (readonly) Originally defined in module Named
Returns the value of attribute alias_name_keys.
#classification_keys ⇒ Object (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 |
#name ⇒ Object
Returns the value of attribute name.
26 27 28 |
# File 'lib/head_music/instruments/instrument_family.rb', line 26 def name @name end |
#name_key ⇒ Object (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_key ⇒ Object (readonly) Originally defined in module Named
Returns the value of attribute name_key.
#orchestra_section_key ⇒ Object (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
.all ⇒ Object
33 34 35 36 |
# File 'lib/head_music/instruments/instrument_family.rb', line 33 def self.all @all ||= INSTRUMENT_FAMILIES.map { |key, _data| get(key) }.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
#ensure_localized_name(name:, locale_code: Locale::DEFAULT_CODE, abbreviation: nil) ⇒ Object Originally defined in module Named
#inferred_name ⇒ Object (private)
80 81 82 |
# File 'lib/head_music/instruments/instrument_family.rb', line 80 def inferred_name name_key.to_s.tr("_", " ") end |
#initialize_data_from_record(record) ⇒ Object (private)
73 74 75 76 77 78 |
# File 'lib/head_music/instruments/instrument_family.rb', line 73 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)
56 57 58 59 60 61 62 63 64 |
# File 'lib/head_music/instruments/instrument_family.rb', line 56 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_locale ⇒ Object (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_names ⇒ Object Originally defined in module Named
Returns an array of LocalizedName instances that are synonymous with the name.
#record_for_key(key) ⇒ Object (private)
66 67 68 69 70 71 |
# File 'lib/head_music/instruments/instrument_family.rb', line 66 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 |