class HeadMusic::Instrument

An instrument can be assigned to a staff.

Constants

INSTRUMENTS

Attributes

classifications[R]
family[R]
name_key[R]
standard_staves[R]

Public Class Methods

all() click to toggle source
# File lib/head_music/instrument.rb, line 16
def self.all
  INSTRUMENTS.map { |key, _data| get(key) }.sort_by(&:name)
end
get(name) click to toggle source
# File lib/head_music/instrument.rb, line 9
def self.get(name)
  return get_by_name(name) if get_by_name(name)
  return get_by_name(key_for_name(name)) if key_for_name(name)

  new(name)
end
new(name) click to toggle source
# File lib/head_music/instrument.rb, line 36
def initialize(name)
  record = record_for_name(name)
  if record
    initialize_data_from_record(record)
  else
    self.name = name.to_s
  end
end

Public Instance Methods

==(other) click to toggle source
# File lib/head_music/instrument.rb, line 22
def ==(other)
  to_s == other.to_s
end
translation(locale = :en) click to toggle source
# File lib/head_music/instrument.rb, line 26
def translation(locale = :en)
  return name unless name_key

  I18n.translate(name_key, scope: [:instruments], locale: locale)
end

Private Instance Methods

inferred_name() click to toggle source
# File lib/head_music/instrument.rb, line 75
def inferred_name
  name_key.to_s.gsub(/_/, ' ')
end
initialize_data_from_record(record) click to toggle source
# File lib/head_music/instrument.rb, line 67
def initialize_data_from_record(record)
  @name_key = record['name_key'].to_sym
  @family = record['family']
  @standard_staves = record['standard_staves'] || []
  @classifications = record['classifications'] || []
  self.name = I18n.translate(name_key, scope: 'instruments', locale: 'en', default: inferred_name)
end
key_for_name(name) click to toggle source
# File lib/head_music/instrument.rb, line 50
def key_for_name(name)
  INSTRUMENTS.each do |key, _data|
    I18n.config.available_locales.each do |locale|
      translation = I18n.t("instruments.#{key}", locale: locale)
      return key if translation.downcase == name.downcase
    end
  end
  nil
end
record_for_key(key) click to toggle source
# File lib/head_music/instrument.rb, line 60
def record_for_key(key)
  INSTRUMENTS.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) click to toggle source
# File lib/head_music/instrument.rb, line 45
def record_for_name(name)
  record_for_key(HeadMusic::Utilities::HashKey.for(name)) ||
    record_for_key(key_for_name(name))
end