This module is mixed in wherever a collection of tones is being returned and handles some of the fundamental logic involved in key and mode calculations and note values.
TODO: Have to use this until we figure out how to override find/default collection result
[Source]
# File app/models/tone_sequence.rb, line 46 46: def all 47: in_key_context! if key 48: in_mode_context! if mode 49: self.sort_by(&:position) 50: end
Shifts indexes to simulat a key change
# File app/models/tone_sequence.rb, line 19 19: def in_key_context! 20: self.each do |tone| 21: tone.tone = (tone.tone + key.index) % 12 22: tone.letter_index = (tone.letter_index + key.letter_index) % 7 23: end 24: true 25: end
Manually specifies the key context for this tone sequence only.
# File app/models/tone_sequence.rb, line 13 13: def in_key_of(in_key = nil) 14: @key = in_key.instance_of?(String) ? Key[in_key] : in_key 15: self 16: end
Manually specifies the mode context for this tone sequence only.
# File app/models/tone_sequence.rb, line 34 34: def in_mode(mode) 35: @mode = mode 36: self 37: end
Shifts mode positions to place tone sequence in mode context
# File app/models/tone_sequence.rb, line 40 40: def in_mode_context! 41: self.each {|tone| tone.position = (tone.position - mode) % self.count + 1} 42: true 43: end
Takes manually specified key context for this collection or delegates to th association owner.
# File app/models/tone_sequence.rb, line 8 8: def key 9: @key || (proxy_owner.key if proxy_owner.respond_to?(:key)) 10: end
Takes manually specified mode context for this collection or delegates to the association owner.
# File app/models/tone_sequence.rb, line 29 29: def mode 30: @mode || (proxy_owner.mode if proxy_owner.respond_to?(:mode)) 31: end
Does the magic in determining the actual note from the tones with tone and letter indexes.
# File app/models/tone_sequence.rb, line 54 54: def notes 55: all.map do |tone| 56: Key.from_index(tone.tone, tone.letter_index).name 57: end.extend(NoteSequence) 58: end
[Validate]