lib/head_music/instrument.rb in head_music-0.24.0 vs lib/head_music/instrument.rb in head_music-0.24.1

- old
+ new

@@ -2,46 +2,59 @@ # An instrument can be assigned to a staff. class HeadMusic::Instrument include HeadMusic::Named - INSTRUMENTS = { - violin: { - name: 'violin', - family: :string, - default_clef: :treble, - }, - piano: { - name: 'piano', - family: :string, - default_system: %i[treble bass], - }, - }.freeze + INSTRUMENTS = YAML.load_file(File.expand_path('data/instruments.yml', __dir__)).freeze def self.get(name) - get_by_name(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 - def initialize(name) - @name = name.to_s + attr_reader :name_key, :family, :default_clefs + + def ==(other) + to_s == other.to_s end - def data - @data ||= INSTRUMENTS[hash_key] + private_class_method :new + + private + + def initialize(name) + record = record_for_name(name) + if record + initialize_data_from_record(record) + else + self.name = name + end end - def family - data[:family] + def record_for_name(name) + key = HeadMusic::Utilities::HashKey.for(name) + record_for_key(key) || record_for_key(key_for_name(name)) end - def default_system - data[:default_system] + def key_for_name(name) + INSTRUMENTS.each do |instrument| + I18n.config.available_locales.each do |locale| + translation = I18n.t("instruments.#{instrument['name_key']}", locale: locale) + return instrument['name_key'] if translation.downcase == name.downcase + end + end + nil end - def default_clef - data[:default_clef] + def record_for_key(key) + INSTRUMENTS.detect { |instrument| instrument['name_key'] == key } end - def ==(other) - to_s == other.to_s + def initialize_data_from_record(record) + @family = record['family'] + @default_clefs = record['default_clefs'] + @name_key = record['name_key'].to_sym + self.name = I18n.translate(name_key, scope: 'instruments', locale: 'en') end end