Class: Coltrane::Commands::Chords

Inherits:
Command
  • Object
show all
Defined in:
lib/coltrane/commands/chords.rb

Constant Summary

Constants inherited from Command

Coltrane::Commands::Command::COMMON_OPTIONS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Command

add_shared_option, #custom_guitar, inherited, #render, subclasses

Constructor Details

#initialize(chord = nil, flavor: :notes, on: :text, notes: nil, preface: nil, voicings: 4, **options) ⇒ Chords

Returns a new instance of Chords.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/coltrane/commands/chords.rb', line 6

def initialize(chord = nil,
               flavor: :notes,
               on: :text,
               notes: nil,
               preface: nil,
               voicings: 4,
               **options)
  if chord
    @chord = chord.is_a?(Theory::Chord) ? chord : Theory::Chord.new(name: chord)
  elsif notes
    @chord = Theory::Chord.new(notes: notes&.split('-'))
  else
    raise 'Provide chord names or notes. Ex: coltrane chords Cm7-Db7, ' \
          'or coltrane chords --notes C-E-G'
  end

  @flavor   = flavor.to_sym
  @preface  = preface || @chord.name
  @on       = on.to_sym
  @voicings = voicings.to_i
end

Instance Attribute Details

#chordObject (readonly)

Returns the value of attribute chord.



4
5
6
# File 'lib/coltrane/commands/chords.rb', line 4

def chord
  @chord
end

#flavorObject (readonly)

Returns the value of attribute flavor.



4
5
6
# File 'lib/coltrane/commands/chords.rb', line 4

def flavor
  @flavor
end

#onObject (readonly)

Returns the value of attribute on.



4
5
6
# File 'lib/coltrane/commands/chords.rb', line 4

def on
  @on
end

#prefaceObject (readonly)

Returns the value of attribute preface.



4
5
6
# File 'lib/coltrane/commands/chords.rb', line 4

def preface
  @preface
end

#voicingsObject (readonly)

Returns the value of attribute voicings.



4
5
6
# File 'lib/coltrane/commands/chords.rb', line 4

def voicings
  @voicings
end

Class Method Details

.mercenary_init(program) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/coltrane/commands/chords.rb', line 73

def self.mercenary_init(program)
  program.command(:chords) do |c|
    c.alias(:chord)
    c.syntax 'chords [<chord-name>] [--on <instrument>]'
    c.description 'Shows the given chord. Ex: coltrane chord Cmaj7 --on piano'
    c.option :notes, '--notes C-D-E', 'finds chords with those notes, ' \
                     'provided they are separated by dashes'

    add_shared_option(:flavor, c)
    add_shared_option(:on, c)
    add_shared_option(:voicings, c)
    c.action { |(chords), **options|
      (chords&.split('-') || [nil]).each { |chord|
        new(chord, **options).render
      }
    }
  end
end

Instance Method Details

#custom_guitar_chordsObject



51
52
53
# File 'lib/coltrane/commands/chords.rb', line 51

def custom_guitar_chords
  Representation::Guitar::Chord.find(chord, guitar: custom_guitar)
end

#custom_guitar_notesObject



47
48
49
# File 'lib/coltrane/commands/chords.rb', line 47

def custom_guitar_notes
  Representation::Guitar::NoteSet.new(chord.notes, guitar: custom_guitar)
end

#layout_horizontal?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/coltrane/commands/chords.rb', line 55

def layout_horizontal?
  on.to_s =~ /guitar|ukulele|ukelele|bass|piano/
end

#on_modelObject



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/coltrane/commands/chords.rb', line 33

def on_model
  case on
  when :text                  then chord
  when :guitar                then Representation::Guitar.find_chords(chord).first(voicings)
  when :ukulele, :ukelele     then Representation::Ukulele.find_chords(chord).first(voicings)
  when :bass                  then Representation::Bass.find_notes(chord.notes)
  when :piano                 then Representation::Piano.find_notes(chord.notes)
  when :'guitar-frets'        then Representation::Guitar.find_notes(chord.notes)
  when :'ukulele-frets',      :'ukelele-frets' then Representation::Ukulele.find_notes(chord.notes)
  when /custom_guitar_frets/  then custom_guitar_notes
  when /custom_guitar/        then custom_guitar_chords.first(voicings)
  end
end

#renderer_optionsObject



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/coltrane/commands/chords.rb', line 59

def renderer_options
  [
    { flavor: flavor },
    (
      {
        layout: :horizontal,
        per_row: 4,
      } if layout_horizontal?
    )
  ]
  .compact
  .reduce({}, :merge)
end

#representationObject



28
29
30
31
# File 'lib/coltrane/commands/chords.rb', line 28

def representation
  return on_model if on == :text
  { preface => on_model }
end