Module: JazzModel::ChordCollection
- Defined in:
- lib/jazz_model/chord_collection.rb
Overview
Intended to be used as a dynamic object extension of an Array representing a collection of chords for some added convenience.
For example, even though the base class is an Array, we can write:
-
scale.chords.symbols
- For an array of symbols. -
scale.chords.names
- For an array of full names.
Instance Method Summary collapse
-
#names ⇒ Object
Returns array of chord names only.
-
#resolve(symbol) ⇒ Object
(also: #[])
WARNING: This is basically duplicated from Chord Try to make this DRY!.
-
#symbols ⇒ Object
Returns array of chord symbols only.
-
#to_s(format = :symbols) ⇒ Object
Formats chord collection.
Instance Method Details
#names ⇒ Object
Returns array of chord names only
26 27 28 |
# File 'lib/jazz_model/chord_collection.rb', line 26 def names self.map {|c| "#{c.key.name if c.key} #{c.name}".strip } end |
#resolve(symbol) ⇒ Object Also known as: []
WARNING: This is basically duplicated from Chord Try to make this DRY!
Resolves a chord symbol into a chord. Implementation is somewhat flakey due to the potential ambiguities arising from specifying key and symbols together.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/jazz_model/chord_collection.rb', line 37 def resolve(symbol) in_key = nil return nil if symbol.nil? Key.all.each do |k| if symbol.starts_with?(k.name) in_key = k symbol.sub!(k.name, '').strip break end end chord_symbol = self.map {|c| c.symbols.to_a}.flatten.detect {|cs| cs.name == symbol} # Perhaps the matched key was really part of the name of the chord, try that: if chord_symbol.nil? && !in_key.nil? symbol = in_key.name + symbol chord_symbol = self.symbols[symbol] end # If still not found, must be invalid: return nil if chord_symbol.nil? chord = chord_symbol.chord chord.key = in_key unless in_key.nil? chord end |
#symbols ⇒ Object
Returns array of chord symbols only
21 22 23 |
# File 'lib/jazz_model/chord_collection.rb', line 21 def symbols self.map {|c| "#{c.key.name if c.key}#{c.primary_symbol.name}" } end |
#to_s(format = :symbols) ⇒ Object
Formats chord collection
13 14 15 16 17 18 |
# File 'lib/jazz_model/chord_collection.rb', line 13 def to_s(format = :symbols) case format when :symbols then symbols.join(', ') when :names then names.join(', ') end end |