Skip to content

Commit

Permalink
Extract related behavior to separate class
Browse files Browse the repository at this point in the history
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
kevin-j-m committed Sep 7, 2022
1 parent ecd5390 commit ca99264
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 42 deletions.
43 changes: 1 addition & 42 deletions lib/blues/guitar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,9 @@

module Blues
class Guitar
VALID_TUNINGS = [
:all_fifths,
:all_fourths,
:down_half_step,
:drop_d,
:modal_c,
:open_a,
:standard,
].freeze

attr_reader :strings

def initialize(amplifier: nil)
@tuning = nil
@strings = Array.new(6) { |i| GuitarString.new(number: i + 1) }
@amplifier = amplifier
end
Expand All @@ -34,11 +23,7 @@ def pick(string:, fret:)
end

def tune(tuning = :standard)
raise "unknown tuning" unless VALID_TUNINGS.include?(tuning)

send("#{tuning}_tuning")

@tuning = tuning
Tuner.new(self).tune(tuning)
end

def restring(gauge_set:)
Expand All @@ -49,31 +34,5 @@ def restring(gauge_set:)
)
end
end

private

def standard_tuning
@strings[5].tune(note: :e, octave: 2)
@strings[4].tune(note: :a, octave: 2)
@strings[3].tune(note: :d, octave: 3)
@strings[2].tune(note: :g, octave: 3)
@strings[1].tune(note: :b, octave: 3)
@strings[0].tune(note: :e, octave: 4)
end

def down_half_step_tuning
@strings[5].tune(note: :e_flat, octave: 2)
@strings[4].tune(note: :a_flat, octave: 2)
@strings[3].tune(note: :d_flat, octave: 3)
@strings[2].tune(note: :g_flat, octave: 3)
@strings[1].tune(note: :b_flat, octave: 3)
@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
51 changes: 51 additions & 0 deletions lib/blues/tuner.rb
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

0 comments on commit ca99264

Please sign in to comment.