Module ChordCollection
In: app/models/chord_collection.rb

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:

Methods

[]   names   resolve   symbols   to_s  

Public Instance methods

[](symbol)

Alias for resolve

Returns array of chord names only

[Source]

    # File app/models/chord_collection.rb, line 25
25:         def names
26:                 self.map {|c| "#{c.key.name if c.key} #{c.name}".strip }
27:         end

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.

[Source]

    # File app/models/chord_collection.rb, line 36
36:         def resolve(symbol)
37:                 in_key = nil
38:           
39:                 return nil if symbol.nil?
40:                 
41:                 Key.cache.each do |k|
42:                         if symbol.starts_with?(k.name)
43:                                 in_key = k
44:                                 symbol.sub!(k.name, '').strip
45:                                 break
46:                         end
47:                 end
48:                 
49:                 chord_symbol = self.map {|c| c.symbols.to_a}.flatten.detect {|cs| cs.name == symbol}
50:                 
51:                 # Perhaps the matched key was really part of the name of the chord, try that:
52:                 if chord_symbol.nil? && !in_key.nil?
53:                         symbol = in_key.name + symbol
54:                         chord_symbol = self.symbols[symbol]
55:                 end
56:                 
57:                 # If still not found, must be invalid:
58:                 return nil if chord_symbol.nil?
59:                 
60:                 chord = chord_symbol.chord
61:                 chord.key = in_key unless in_key.nil?
62:                 chord
63:         end

Returns array of chord symbols only

[Source]

    # File app/models/chord_collection.rb, line 20
20:         def symbols
21:                 self.map {|c| "#{c.key.name if c.key}#{c.primary_symbol.name}" }
22:         end

Formats chord collection

[Source]

    # File app/models/chord_collection.rb, line 12
12:         def to_s(format = :symbols)
13:                 case format
14:                 when :symbols then symbols.join(', ')
15:                 when :names then names.join(', ')
16:                 end
17:         end

[Validate]