Class: WTFChord::Chord

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

Direct Known Subclasses

WTFChord::Collectors::Generic

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.



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

def initialize(note, name)
  @pitch = note.is_a?(Pitch) ? note : WTFChord.note(note)
  @name  = "#{@pitch.key}#{name}"
  @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.



10
11
12
# File 'lib/wtf_chord/chord.rb', line 10

def bass
  @bass
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/wtf_chord/chord.rb', line 10

def name
  @name
end

#notesObject (readonly)

Returns the value of attribute notes.



10
11
12
# File 'lib/wtf_chord/chord.rb', line 10

def notes
  @notes
end

#pitchObject (readonly)

Returns the value of attribute pitch.



10
11
12
# File 'lib/wtf_chord/chord.rb', line 10

def pitch
  @pitch
end

#stepsObject (readonly)

Returns the value of attribute steps.



10
11
12
# File 'lib/wtf_chord/chord.rb', line 10

def steps
  @steps
end

Instance Method Details

#===(other) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/wtf_chord/chord.rb', line 47

def === other
  case other
  when InstrumentPitch
    self === other.note
  when Note
    notes.include?(other) || (bass? && bass == other)
  else
    super
  end
end

#bass?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/wtf_chord/chord.rb', line 35

def bass?
  !!@bass
end

#fingerings(limit = nil, collector: Collectors::Guitar) ⇒ Object



23
24
25
# File 'lib/wtf_chord/chord.rb', line 23

def fingerings(limit = nil, collector: Collectors::Guitar)
  collector.new(self).call[0, limit || 5]
end

#inspectObject



58
59
60
# File 'lib/wtf_chord/chord.rb', line 58

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

#original_bassObject



43
44
45
# File 'lib/wtf_chord/chord.rb', line 43

def original_bass
  @notes[0]
end

#sizeObject



39
40
41
# File 'lib/wtf_chord/chord.rb', line 39

def size
  bass? ? [*@notes, bass].uniq.size : @notes.size
end

#third_toneObject



31
32
33
# File 'lib/wtf_chord/chord.rb', line 31

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

#toneObject



27
28
29
# File 'lib/wtf_chord/chord.rb', line 27

def tone
  bass || original_bass
end