Class: OutroRails::GuitarVoicingGenerator

Inherits:
Object
  • Object
show all
Includes:
VoicingSeedWriter
Defined in:
lib/outro_rails/generators/guitar_voicing_generator.rb

Overview

Generates complete guitar voicing seed entries for a chord entry (a hash loaded from db/seeds/chords/*.yml), using the shape library in Instruments::Guitar::ChordShapes.

Voicings are ordered from the top of the neck down:

1. the open chord, when one exists for this root
2. its bass extensions - slash voicings that walk the shape down
 toward string 6
3. every movable shape, sorted by starting fret

Chords with no shapes defined (or with alterations/added tones) are reported as unsupported so callers can fall back to stubs.

Defined Under Namespace

Classes: Voicing

Constant Summary collapse

Shapes =

Shorthand for the guitar shape library this generator draws grips from

Instruments::Guitar::ChordShapes
FRET_WINDOW =

A five-fret display window, like the hand position it represents.

4
OPEN_RANK =

Sort ranks within a starting fret: open grips first, then their slash extensions, then barred shapes.

0
EXTENSION_RANK =
1
MOVABLE_RANK =
2

Instance Attribute Summary collapse

Attributes included from VoicingSeedWriter

#chord

Instance Method Summary collapse

Methods included from VoicingSeedWriter

#root, #symbol

Constructor Details

#initialize(chord, guitar: Instruments::Guitar.new) ⇒ GuitarVoicingGenerator

Takes a chord seed hash and the instrument to voice it on



45
46
47
48
# File 'lib/outro_rails/generators/guitar_voicing_generator.rb', line 45

def initialize(chord, guitar: Instruments::Guitar.new)
  @chord = chord
  @guitar = guitar
end

Instance Attribute Details

#guitarObject (readonly)

The instrument supplying string count and tuning



42
43
44
# File 'lib/outro_rails/generators/guitar_voicing_generator.rb', line 42

def guitar
  @guitar
end

Instance Method Details

#supported?Boolean

True when shapes exist for this chord. Altered and added-tone chords are out of scope for the shape library.

Returns:

  • (Boolean)


52
53
54
55
56
# File 'lib/outro_rails/generators/guitar_voicing_generator.rb', line 52

def supported?
  Array(chord["alterations"]).empty? &&
    Array(chord["added_tones"]).empty? &&
    !shapes.nil?
end

#to_yamlObject

Renders every voicing as a YAML fixture fragment, numbering positions from 1. Raises unless the chord is supported.

Raises:

  • (ArgumentError)


60
61
62
63
64
65
66
67
68
# File 'lib/outro_rails/generators/guitar_voicing_generator.rb', line 60

def to_yaml
  raise ArgumentError,
        "no guitar shapes defined for #{symbol}" unless supported?

  voicings
    .each_with_index
    .map { |voicing, index| entry_yaml(voicing, index + 1) }
    .join("\n")
end