Class: PM::Cursor

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(pm) ⇒ Cursor

Returns a new instance of Cursor.



10
11
12
13
# File 'lib/patchmaster/cursor.rb', line 10

def initialize(pm)
  @pm = pm
  clear
end

Instance Attribute Details

#patchObject (readonly)

Returns the value of attribute patch.



8
9
10
# File 'lib/patchmaster/cursor.rb', line 8

def patch
  @patch
end

#songObject (readonly)

Returns the value of attribute song.



8
9
10
# File 'lib/patchmaster/cursor.rb', line 8

def song
  @song
end

#song_listObject (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

#clearObject

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

#initObject

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

#markObject

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_patchObject



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_songObject



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_patchObject



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_songObject



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

#restoreObject

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