Class: HeadMusic::Instruments::InstrumentConfiguration

Inherits:
Object
  • Object
show all
Includes:
ValueEquality
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.



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

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.



14
15
16
# File 'lib/head_music/instruments/instrument_configuration.rb', line 14

def instrument_key
  @instrument_key
end

#name_keyObject (readonly)

Returns the value of attribute name_key.



14
15
16
# File 'lib/head_music/instruments/instrument_configuration.rb', line 14

def name_key
  @name_key
end

#optionsObject (readonly)

Returns the value of attribute options.



14
15
16
# File 'lib/head_music/instruments/instrument_configuration.rb', line 14

def options
  @options
end

Class Method Details

.for_instrument(instrument_key) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/head_music/instruments/instrument_configuration.rb', line 19

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 Originally defined in module ValueEquality

#build_options(options_data) ⇒ Object (private)



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

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



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

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

#option(option_key) ⇒ Object



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

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

#to_sObject



47
48
49
# File 'lib/head_music/instruments/instrument_configuration.rb', line 47

def to_s
  name_key.to_s
end