-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract related behavior to separate class
This creates a Tuner, which is responsible for tuning a guitar. The guitar still has a `tune` method on its public API, but the complexity of doing the tuning is delegated to this other class. This isn't an object that represents a real-life object, but still has value being separate. Now the class it was extracted from, the guitar, can focus on doing all of its guitar activities. Before it looked like the main purpose of a guitar was to tune itself with the percentage of code that was dedicated to tuning.
- Loading branch information
Showing
2 changed files
with
52 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# frozen_string_literal: true | ||
|
||
module Blues | ||
class Tuner | ||
VALID_TUNINGS = [ | ||
:all_fifths, | ||
:all_fourths, | ||
:down_half_step, | ||
:drop_d, | ||
:modal_c, | ||
:open_a, | ||
:standard, | ||
].freeze | ||
|
||
def initialize(guitar) | ||
@guitar = guitar | ||
end | ||
|
||
def tune(tuning = :standard) | ||
raise "unknown tuning" unless VALID_TUNINGS.include?(tuning) | ||
|
||
send("#{tuning}_tuning") | ||
end | ||
|
||
private | ||
|
||
def standard_tuning | ||
@guitar.strings[5].tune(note: :e, octave: 2) | ||
@guitar.strings[4].tune(note: :a, octave: 2) | ||
@guitar.strings[3].tune(note: :d, octave: 3) | ||
@guitar.strings[2].tune(note: :g, octave: 3) | ||
@guitar.strings[1].tune(note: :b, octave: 3) | ||
@guitar.strings[0].tune(note: :e, octave: 4) | ||
end | ||
|
||
def down_half_step_tuning | ||
@guitar.strings[5].tune(note: :e_flat, octave: 2) | ||
@guitar.strings[4].tune(note: :a_flat, octave: 2) | ||
@guitar.strings[3].tune(note: :d_flat, octave: 3) | ||
@guitar.strings[2].tune(note: :g_flat, octave: 3) | ||
@guitar.strings[1].tune(note: :b_flat, octave: 3) | ||
@guitar.strings[0].tune(note: :e_flat, octave: 4) | ||
end | ||
|
||
def drop_d_tuning = nil | ||
def open_a_tuning = nil | ||
def modal_c_tuning = nil | ||
def all_fourths_tuning = nil | ||
def all_fifths_tuning = nil | ||
end | ||
end |