Class: Fretboard::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/fretboard/builder.rb

Constant Summary collapse

DEFAULT_NUMBER_OF_FRETS =

fretboard.build fretboard.data fretboard.draw

12

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tuning, number_of_frets = DEFAULT_NUMBER_OF_FRETS) ⇒ Builder

Returns a new instance of Builder.



98
99
100
101
102
103
# File 'lib/fretboard/builder.rb', line 98

def initialize(tuning, number_of_frets = DEFAULT_NUMBER_OF_FRETS)
  @tuning = tuning.upcase.to_sym
  @number_of_frets = number_of_frets

  @data = {}
end

Instance Attribute Details

#dataObject (readonly)

METHODS_NAMES = %i[

standart
drop_c
drop_d
double_drop_d
open_a
open_c
open_d
open_d_minor
open_g
open_g_minor
modal_d
modal_g

].freeze



42
43
44
# File 'lib/fretboard/builder.rb', line 42

def data
  @data
end

Class Method Details

.double_drop_d(number_of_frets = DEFAULT_NUMBER_OF_FRETS) ⇒ Object



62
63
64
# File 'lib/fretboard/builder.rb', line 62

def self.double_drop_d(number_of_frets = DEFAULT_NUMBER_OF_FRETS)
  new(:double_drop_d, number_of_frets)
end

.drop_c(number_of_frets = DEFAULT_NUMBER_OF_FRETS) ⇒ Object



54
55
56
# File 'lib/fretboard/builder.rb', line 54

def self.drop_c(number_of_frets = DEFAULT_NUMBER_OF_FRETS)
  new(:drop_c, number_of_frets)
end

.drop_d(number_of_frets = DEFAULT_NUMBER_OF_FRETS) ⇒ Object



58
59
60
# File 'lib/fretboard/builder.rb', line 58

def self.drop_d(number_of_frets = DEFAULT_NUMBER_OF_FRETS)
  new(:drop_d, number_of_frets)
end


90
91
92
# File 'lib/fretboard/builder.rb', line 90

def self.modal_d(number_of_frets = DEFAULT_NUMBER_OF_FRETS)
  new(:modal_d, number_of_frets)
end


94
95
96
# File 'lib/fretboard/builder.rb', line 94

def self.modal_g(number_of_frets = DEFAULT_NUMBER_OF_FRETS)
  new(:modal_g, number_of_frets)
end

.open_a(number_of_frets = DEFAULT_NUMBER_OF_FRETS) ⇒ Object



66
67
68
# File 'lib/fretboard/builder.rb', line 66

def self.open_a(number_of_frets = DEFAULT_NUMBER_OF_FRETS)
  new(:open_a, number_of_frets)
end

.open_c(number_of_frets = DEFAULT_NUMBER_OF_FRETS) ⇒ Object



70
71
72
# File 'lib/fretboard/builder.rb', line 70

def self.open_c(number_of_frets = DEFAULT_NUMBER_OF_FRETS)
  new(:open_c, number_of_frets)
end

.open_d(number_of_frets = DEFAULT_NUMBER_OF_FRETS) ⇒ Object



74
75
76
# File 'lib/fretboard/builder.rb', line 74

def self.open_d(number_of_frets = DEFAULT_NUMBER_OF_FRETS)
  new(:open_d, number_of_frets)
end

.open_d_minor(number_of_frets = DEFAULT_NUMBER_OF_FRETS) ⇒ Object



78
79
80
# File 'lib/fretboard/builder.rb', line 78

def self.open_d_minor(number_of_frets = DEFAULT_NUMBER_OF_FRETS)
  new(:open_d_minor, number_of_frets)
end

.open_g(number_of_frets = DEFAULT_NUMBER_OF_FRETS) ⇒ Object



82
83
84
# File 'lib/fretboard/builder.rb', line 82

def self.open_g(number_of_frets = DEFAULT_NUMBER_OF_FRETS)
  new(:open_g, number_of_frets)
end

.open_g_minor(number_of_frets = DEFAULT_NUMBER_OF_FRETS) ⇒ Object



86
87
88
# File 'lib/fretboard/builder.rb', line 86

def self.open_g_minor(number_of_frets = DEFAULT_NUMBER_OF_FRETS)
  new(:open_g_minor, number_of_frets)
end

.standart(number_of_frets = DEFAULT_NUMBER_OF_FRETS) ⇒ Object

METHODS_NAMES.each do |method_name|

define_method(method_name) do |number_of_frets = DEFAULT_NUMBER_OF_FRETS|
  new(method_name, number_of_frets)
end

end



50
51
52
# File 'lib/fretboard/builder.rb', line 50

def self.standart(number_of_frets = DEFAULT_NUMBER_OF_FRETS)
  new(:standart, number_of_frets)
end

Instance Method Details

#build(sharp_or_flat: :both) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/fretboard/builder.rb', line 105

def build(sharp_or_flat: :both)
  unless Fretboard::Tunings.exists?(@tuning)
    Fretboard::Console.danger('Unable to detect guitar tuning')
    return
  end

  tuning = Fretboard::Tunings.get(@tuning)
  strings = tuning[:STRINGS]
  number_of_strings = strings.size

  (1..number_of_strings).each do |string_number|
    puts
    puts "String: #{string_number}"
    puts

    current_note = strings[string_number][:NOTE]

    @data[string_number] = {}

    puts "Fret: 0 (#{current_note})"

    @data[string_number][0] = current_note

    (1..@number_of_frets).each do |fret|
      next_note = Fretboard::Note.next_for(
        current_note,
        sharp_or_flat: sharp_or_flat
      )

      puts "Fret: #{fret} (#{next_note})"

      @data[string_number][fret] = next_note

      current_note = next_note
    end
  end

  puts
  puts 'done'
end

#draw(sharp_or_flat: :both) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/fretboard/builder.rb', line 146

def draw(sharp_or_flat: :both)
  unless @data.any?
    Fretboard::Console.danger('Create the data')
    return
  end

  headings = []
  rows = []

  @data.each_pair do |string_number, string_notes|
    row = []

    headings << '№'
    row << string_number

    headings << 0

    open_note = string_notes[0]

    if open_note.is_a?(Array)
      open_note = if sharp_or_flat == :both
                    open_note.join('/')
                  elsif sharp_or_flat == :sharp
                    open_note.first
                  else
                    open_note.last
                  end
    end

    row << open_note

    string_notes.except(0).each_pair do |fret, note|
      headings << fret
      row << note
    end

    rows << row
  end

  Fretboard::Console.print_table(headings.uniq, rows)

  nil
end