class ChordPosition

Attributes

chord[R]
frets[R]

Public Class Methods

create_list(frets_list, chord) click to toggle source
# File lib/chord_finder/chord_position.rb, line 2
def self.create_list(frets_list, chord)
  frets_list.map { |frets| ChordPosition.new(frets, chord) }
end
new(frets, chord) click to toggle source
# File lib/chord_finder/chord_position.rb, line 8
def initialize(frets, chord)
  @frets = frets
  @chord = chord
end

Public Instance Methods

bass_note() click to toggle source
# File lib/chord_finder/chord_position.rb, line 57
def bass_note
  frets.find { |fret| fret.note.to_s != "x" }.note
end
bass_note_string() click to toggle source
# File lib/chord_finder/chord_position.rb, line 61
def bass_note_string
  frets.find_index { |fret| fret.note.to_s != "x" }
end
closed_fret_nums() click to toggle source
# File lib/chord_finder/chord_position.rb, line 41
def closed_fret_nums
  @closed_fret_nums ||= fret_nums.select { |num| num > 0 }
end
fret_nums() click to toggle source
# File lib/chord_finder/chord_position.rb, line 29
def fret_nums
  @fret_nums ||= frets.map(&:number)
end
highest_fret_num() click to toggle source
# File lib/chord_finder/chord_position.rb, line 49
def highest_fret_num
  closed_fret_nums.max || 0
end
lowest_fret_num() click to toggle source
# File lib/chord_finder/chord_position.rb, line 45
def lowest_fret_num
  closed_fret_nums.min || 0
end
notes() click to toggle source
# File lib/chord_finder/chord_position.rb, line 53
def notes
  @notes ||= frets.map(&:note).select { |note| note.to_s != "x" }
end
num_muted_strings() click to toggle source
# File lib/chord_finder/chord_position.rb, line 37
def num_muted_strings
  fret_nums.count(-1)
end
num_sounding_strings() click to toggle source
# File lib/chord_finder/chord_position.rb, line 33
def num_sounding_strings
  6 - num_muted_strings
end
same_form?(other_position) click to toggle source
# File lib/chord_finder/chord_position.rb, line 17
def same_form?(other_position)
  same_bass?(other_position) &&
  same_on_sounding_strings?(other_position)
end
to_s() click to toggle source
# File lib/chord_finder/chord_position.rb, line 13
def to_s
  fret_nums.join.gsub("-1", "x")
end
valid?() click to toggle source
# File lib/chord_finder/chord_position.rb, line 22
def valid?
  chord?                  &&
  frettable?              &&
  includes_bass_string?   &&
  first_or_fifth_in_bass?
end

Private Instance Methods

chord?() click to toggle source
# File lib/chord_finder/chord_position.rb, line 66
def chord?
  notes.uniq.sort_by(&:position) == chord.notes.sort_by(&:position)
end
first_or_fifth_in_bass?() click to toggle source
# File lib/chord_finder/chord_position.rb, line 78
def first_or_fifth_in_bass?
  bass_note == chord.first || bass_note == chord.fifth
end
frettable?() click to toggle source
# File lib/chord_finder/chord_position.rb, line 70
def frettable?
  closed_fret_nums.empty? || (highest_fret_num - lowest_fret_num) <= 3
end
includes_bass_string?() click to toggle source
# File lib/chord_finder/chord_position.rb, line 74
def includes_bass_string?
  (0..2).include? bass_note_string
end
same_bass?(other_position) click to toggle source
# File lib/chord_finder/chord_position.rb, line 82
def same_bass?(other_position)
  s1, s2 = bass_note_string, other_position.bass_note_string
  s1 == s2 && frets[s1] == frets[s2]
end
same_on_sounding_strings?(other_position) click to toggle source
# File lib/chord_finder/chord_position.rb, line 87
def same_on_sounding_strings?(other_position)
  fret_nums.zip(other_position.fret_nums).all? { |num1, num2|
    (num1 >= 0 && num2 >= 0) ? num1 == num2 : true }
end