Class: PM::Cursor
- Inherits:
-
Object
- Object
- PM::Cursor
- Defined in:
- lib/patchmaster/cursor.rb
Overview
A PM::Cursor knows the current PM::SongList, PM::Song, and PM::Patch, how to move between songs and patches, and how to find them given name regexes.
Instance Attribute Summary collapse
-
#patch ⇒ Object
readonly
Returns the value of attribute patch.
-
#song ⇒ Object
readonly
Returns the value of attribute song.
-
#song_list ⇒ Object
readonly
Returns the value of attribute song_list.
Instance Method Summary collapse
-
#clear ⇒ Object
Set @song_list, @song, and @patch to
nil
. -
#dameraulevenshtein(seq1, seq2) ⇒ Object
gist.github.com/182759 (git://gist.github.com/182759.git) Referenced from en.wikipedia.org/wiki/Damerau%E2%80%93Levenshtein_distance.
-
#find_nearest_match(list, str) ⇒ Object
List must contain objects that respond to #name.
- #goto_song(name_regex) ⇒ Object
- #goto_song_list(name_regex) ⇒ Object
-
#init ⇒ Object
Set @song_list to All Songs, @song to first song, and.
-
#initialize(pm) ⇒ Cursor
constructor
A new instance of Cursor.
-
#mark ⇒ Object
Remembers the names of the current song list, song, and patch.
- #next_patch ⇒ Object
- #next_song ⇒ Object
- #prev_patch ⇒ Object
- #prev_song ⇒ Object
-
#restore ⇒ Object
Using the names saved by #save, try to find them now.
Constructor Details
Instance Attribute Details
#patch ⇒ Object (readonly)
Returns the value of attribute patch.
8 9 10 |
# File 'lib/patchmaster/cursor.rb', line 8 def patch @patch end |
#song ⇒ Object (readonly)
Returns the value of attribute song.
8 9 10 |
# File 'lib/patchmaster/cursor.rb', line 8 def song @song end |
#song_list ⇒ Object (readonly)
Returns the value of attribute song_list.
8 9 10 |
# File 'lib/patchmaster/cursor.rb', line 8 def song_list @song_list end |
Instance Method Details
#clear ⇒ Object
Set @song_list, @song, and @patch to nil
.
16 17 18 19 |
# File 'lib/patchmaster/cursor.rb', line 16 def clear @song_list = @song = @patch = nil # Do not erase names saved by #mark. end |
#dameraulevenshtein(seq1, seq2) ⇒ Object
gist.github.com/182759 (git://gist.github.com/182759.git) Referenced from en.wikipedia.org/wiki/Damerau%E2%80%93Levenshtein_distance
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/patchmaster/cursor.rb', line 154 def dameraulevenshtein(seq1, seq2) oneago = nil thisrow = (1..seq2.size).to_a + [0] seq1.size.times do |x| twoago, oneago, thisrow = oneago, thisrow, [0] * seq2.size + [x + 1] seq2.size.times do |y| delcost = oneago[y] + 1 addcost = thisrow[y - 1] + 1 subcost = oneago[y - 1] + ((seq1[x] != seq2[y]) ? 1 : 0) thisrow[y] = [delcost, addcost, subcost].min if (x > 0 and y > 0 and seq1[x] == seq2[y-1] and seq1[x-1] == seq2[y] and seq1[x] != seq2[y]) thisrow[y] = [thisrow[y], twoago[y-2] + 1].min end end end return thisrow[seq2.size - 1] end |
#find_nearest_match(list, str) ⇒ Object
List must contain objects that respond to #name. If str
is nil or list
is nil
or empty then nil
is returned.
144 145 146 147 148 149 150 |
# File 'lib/patchmaster/cursor.rb', line 144 def find_nearest_match(list, str) return nil unless str && list && !list.empty? str = str.downcase distances = list.collect { |item| dameraulevenshtein(str, item.name.downcase) } list[distances.index(distances.min)] end |
#goto_song(name_regex) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/patchmaster/cursor.rb', line 75 def goto_song(name_regex) new_song_list = new_song = new_patch = nil new_song = @song_list.find(name_regex) if @song_list new_song = @@pm.all_songs.find(name_regex) unless new_song new_patch = new_song ? new_song.patches.first : nil if (new_song && new_song != @song) || # moved to new song (new_song == @song && @patch != new_patch) # same song but not at same first patch @patch.stop if @patch if @song_list.songs.include?(new_song) new_song_list = @song_list else # Not found in current song list. Switch to @pm.all_songs list. new_song_list = @@pm.all_songs end @song_list = new_song_list @song = new_song @patch = new_patch @patch.start end end |
#goto_song_list(name_regex) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/patchmaster/cursor.rb', line 100 def goto_song_list(name_regex) name_regex = Regexp.new(name_regex.to_s, true) # make case-insensitive new_song_list = @pm.song_lists.detect { |song_list| song_list.name =~ name_regex } return unless new_song_list @song_list = new_song_list new_song = @song_list.songs.first new_patch = new_song ? new_song.patches.first : nil if new_patch != @patch @patch.stop if @patch new_patch.start if new_patch end @song = new_song @patch = new_patch end |
#init ⇒ Object
Set @song_list to All Songs, @song to first song, and
23 24 25 26 27 28 29 30 31 |
# File 'lib/patchmaster/cursor.rb', line 23 def init @song_list = @pm.song_lists.first @song = @song_list.songs.first if @song @patch = @song.patches.first else @patch = nil end end |
#mark ⇒ Object
Remembers the names of the current song list, song, and patch. Used by #restore.
120 121 122 123 124 |
# File 'lib/patchmaster/cursor.rb', line 120 def mark @song_list_name = @song_list ? @song_list.name : nil @song_name = @song ? @song.name : nil @patch_name = @patch ? @patch.name : nil end |
#next_patch ⇒ Object
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/patchmaster/cursor.rb', line 53 def next_patch return unless @song if @song.patches.last == @patch next_song elsif @patch @patch.stop @patch = @song.patches[@song.patches.index(@patch) + 1] @patch.start end end |
#next_song ⇒ Object
33 34 35 36 37 38 39 40 41 |
# File 'lib/patchmaster/cursor.rb', line 33 def next_song return unless @song_list return if @song_list.songs.last == @song @patch.stop if @patch @song = @song_list.songs[@song_list.songs.index(@song) + 1] @patch = @song.patches.first @patch.start end |
#prev_patch ⇒ Object
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/patchmaster/cursor.rb', line 64 def prev_patch return unless @song if @song.patches.first == @patch prev_song elsif @patch @patch.stop @patch = @song.patches[@song.patches.index(@patch) - 1] @patch.start end end |
#prev_song ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/patchmaster/cursor.rb', line 43 def prev_song return unless @song_list return if @song_list.songs.first == @song @patch.stop if @patch @song = @song_list.songs[@song_list.songs.index(@song) - 1] @patch = @song.patches.first @patch.start end |
#restore ⇒ Object
Using the names saved by #save, try to find them now.
Since names can change we use Damerau-Levenshtein distance on lowercase versions of all strings.
130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/patchmaster/cursor.rb', line 130 def restore return unless @song_list_name # will be nil on initial load @song_list = find_nearest_match(@pm.song_lists, @song_list_name) || @pm.all_songs @song = find_nearest_match(@song_list.songs, @song_name) || @song_list.songs.first if @song @patch = find_nearest_match(@song.patches, @patch_name) || @song.patches.first else @patch = nil end end |