Class: WTFChord::Chord

Inherits:
Object
  • Object
show all
Defined in:
lib/wtf_chord/chord.rb,
lib/wtf_chord/fingerings_generator.rb

Direct Known Subclasses

FingeringsGenerator

Constant Summary collapse

BASS_MATCH =
/(?<=[\\\/])[A-H][b#]?(?=$)/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(note, name) ⇒ Chord

Returns a new instance of Chord.



10
11
12
13
14
15
16
17
18
19
# File 'lib/wtf_chord/chord.rb', line 10

def initialize(note, name)
  @pitch = note.is_a?(Pitch) ? note : WTFChord.note(note)
  @name  = "#{@pitch.key}#{name}".freeze
  @steps = Array(WTFChord.rules[name])
  @notes = @steps.map { |dist| (@pitch + dist).note }.tap(&:uniq!)

  BASS_MATCH.match(@name) do |m|
    @bass = WTFChord.note(m[0]).note
  end
end

Instance Attribute Details

#bassObject (readonly)

Returns the value of attribute bass.



8
9
10
# File 'lib/wtf_chord/chord.rb', line 8

def bass
  @bass
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/wtf_chord/chord.rb', line 8

def name
  @name
end

#notesObject (readonly)

Returns the value of attribute notes.



8
9
10
# File 'lib/wtf_chord/chord.rb', line 8

def notes
  @notes
end

#pitchObject (readonly)

Returns the value of attribute pitch.



8
9
10
# File 'lib/wtf_chord/chord.rb', line 8

def pitch
  @pitch
end

#stepsObject (readonly)

Returns the value of attribute steps.



8
9
10
# File 'lib/wtf_chord/chord.rb', line 8

def steps
  @steps
end

Instance Method Details

#bass?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/wtf_chord/chord.rb', line 29

def bass?
  !!@bass
end

#fingerings(limit = nil) ⇒ Object



21
22
23
# File 'lib/wtf_chord/chord.rb', line 21

def fingerings(limit = nil)
  FingeringsGenerator.new(self).call[0, limit || 5]
end

#inspectObject



37
38
39
# File 'lib/wtf_chord/chord.rb', line 37

def inspect
  "#{name} (#{@notes.map(&:key) * ' - '})"
end

#original_bassObject



33
34
35
# File 'lib/wtf_chord/chord.rb', line 33

def original_bass
  @notes[0]
end

#third_toneObject



25
26
27
# File 'lib/wtf_chord/chord.rb', line 25

def third_tone
  @third_tone ||= @notes[@steps.size > 3 ? 2 : -1]
end