Class: HeadMusic::Instruments::InstrumentConfiguration

Inherits:
Object
  • Object
show all
Defined in:
lib/head_music/instruments/instrument_configuration.rb

Overview

A configurable aspect of an instrument, such as a leadpipe, mute, or attachment.

Examples:

  • Piccolo trumpet "leadpipe" configuration with options: b_flat (default), a
  • Trumpet "mute" configuration with options: open (default), straight, cup, harmon
  • Bass trombone "f_attachment" with options: disengaged (default), engaged

Constant Summary collapse

CONFIGURATIONS =
YAML.load_file(File.expand_path("instrument_configurations.yml", __dir__)).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name_key:, instrument_key:, options_data: {}) ⇒ InstrumentConfiguration

Returns a new instance of InstrumentConfiguration.



29
30
31
32
33
# File 'lib/head_music/instruments/instrument_configuration.rb', line 29

def initialize(name_key:, instrument_key:, options_data: {})
  @name_key = name_key.to_sym
  @instrument_key = instrument_key.to_sym
  @options = build_options(options_data)
end

Instance Attribute Details

#instrument_keyObject (readonly)

Returns the value of attribute instrument_key.



12
13
14
# File 'lib/head_music/instruments/instrument_configuration.rb', line 12

def instrument_key
  @instrument_key
end

#name_keyObject (readonly)

Returns the value of attribute name_key.



12
13
14
# File 'lib/head_music/instruments/instrument_configuration.rb', line 12

def name_key
  @name_key
end

#optionsObject (readonly)

Returns the value of attribute options.



12
13
14
# File 'lib/head_music/instruments/instrument_configuration.rb', line 12

def options
  @options
end

Class Method Details

.for_instrument(instrument_key) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/head_music/instruments/instrument_configuration.rb', line 15

def for_instrument(instrument_key)
  instrument_key = instrument_key.to_s
  return [] unless CONFIGURATIONS.key?(instrument_key)

  CONFIGURATIONS[instrument_key].map do |config_name, config_data|
    new(
      name_key: config_name,
      instrument_key: instrument_key,
      options_data: config_data["options"] || {}
    )
  end
end

Instance Method Details

#==(other) ⇒ Object



43
44
45
46
47
# File 'lib/head_music/instruments/instrument_configuration.rb', line 43

def ==(other)
  return false unless other.is_a?(self.class)

  name_key == other.name_key && instrument_key == other.instrument_key
end

#build_options(options_data) ⇒ Object (private)



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/head_music/instruments/instrument_configuration.rb', line 55

def build_options(options_data)
  options_data.map do |option_name, option_attrs|
    attrs = option_attrs || {}
    HeadMusic::Instruments::InstrumentConfigurationOption.new(
      name_key: option_name,
      default: attrs["default"],
      transposition_semitones: attrs["transposition_semitones"],
      lowest_pitch_semitones: attrs["lowest_pitch_semitones"]
    )
  end
end

#default_optionObject



35
36
37
# File 'lib/head_music/instruments/instrument_configuration.rb', line 35

def default_option
  @default_option ||= options.find(&:default?) || options.first
end

#option(option_key) ⇒ Object



39
40
41
# File 'lib/head_music/instruments/instrument_configuration.rb', line 39

def option(option_key)
  options.find { |opt| opt.name_key == option_key.to_sym }
end

#to_sObject



49
50
51
# File 'lib/head_music/instruments/instrument_configuration.rb', line 49

def to_s
  name_key.to_s
end