Class: Chordproko::Chord

Inherits:
Object
  • Object
show all
Defined in:
lib/chordproko/chord.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content) ⇒ Chord

Returns a new instance of Chord.



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/chordproko/chord.rb', line 5

def initialize content
  @content = content  
  @chord_list = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"]
  @chord_indexes = {
    "C" => 0,
    "D" => 2,
    "E" => 4,
    "F" => 5,
    "G" => 7,
    "A" => 9,
    "B" => 11,
  }
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



4
5
6
# File 'lib/chordproko/chord.rb', line 4

def content
  @content
end

#keyObject

Returns the value of attribute key.



4
5
6
# File 'lib/chordproko/chord.rb', line 4

def key
  @key
end

Instance Method Details

#each(&block) ⇒ Object



18
19
20
# File 'lib/chordproko/chord.rb', line 18

def each(&block)
  [self].each(&block)
end

#to_sObject



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

def to_s
  "#{transpose_key}"
end

#transpose_keyObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/chordproko/chord.rb', line 24

def transpose_key
  return @content.to_s if @key == 0
  chord_value = @chord_indexes[@content.to_s[0]]
  @content.to_s[1..-1].each_char do |mod|
    case mod
    when "#"
      chord_value += 1
    when "b"
      chord_value -= 1
    end
  end
  chord_index = (chord_value + @key)% 12
  res = @chord_list[chord_index]

end