Class: HeadMusic::Instruments::InstrumentCatalog

Inherits:
Object
  • Object
show all
Includes:
CatalogLookup
Defined in:
lib/head_music/instruments/instrument_catalog.rb

Overview

Finds an instrument’s catalog record by key, localized name, or alias. Wraps the instruments.yml data (a Hash of name_key => record) so an Instrument need not know how a raw record is located, only how to build itself from one.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(records) ⇒ InstrumentCatalog

Returns a new instance of InstrumentCatalog.



11
12
13
# File 'lib/head_music/instruments/instrument_catalog.rb', line 11

def initialize(records)
  @records = records
end

Instance Attribute Details

#recordsObject (readonly, private)

Returns the value of attribute records.



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

def records
  @records
end

Instance Method Details

#catalogObject (private)

CatalogLookup hook: the hash its #key_for_name searches by translation.



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

def catalog
  records
end

#key_for_name(name) ⇒ Object (private) Originally defined in module CatalogLookup

#record_for(name) ⇒ Object

The record matching a name given as a key, a localized (translated) name, or an alias, with its own name_key merged in; nil when nothing matches.



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

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

#record_for_alias(name) ⇒ Object (private)



39
40
41
42
43
44
45
46
47
# File 'lib/head_music/instruments/instrument_catalog.rb', line 39

def record_for_alias(name)
  normalized_name = HeadMusic::Utilities::HashKey.for(name).to_s
  records.each do |name_key, data|
    data["alias_name_keys"]&.each do |alias_key|
      return data.merge("name_key" => name_key) if HeadMusic::Utilities::HashKey.for(alias_key).to_s == normalized_name
    end
  end
  nil
end

#record_for_key(key) ⇒ Object (private)



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

def record_for_key(key)
  records.each do |name_key, data|
    return data.merge("name_key" => name_key) if name_key.to_s == key.to_s
  end
  nil
end