Refactored selectMatches to break out the error handling into a re-useable function.

This commit is contained in:
Jonathan Bernard 2016-03-05 21:30:59 -06:00
parent 7cad273a0b
commit 13b0fa5bc4
2 changed files with 55 additions and 43 deletions

View File

@ -5,6 +5,7 @@ select (current)
album <id | name>
playlist <id | name>
file <id | name>
bookmark <id | name>
tag(s) <tag-names>
* List
@ -23,6 +24,7 @@ add selection to playlist <id | name>
add album <id | name> to playlist <id | name>
add artist <id | name> to playlist <id | name>
add file <id | name> to playlist <id | name>
create playlist <name>
* Tagging
@ -58,6 +60,8 @@ clear
selected playlist
selection
set bookmark
* Transport operations
play
@ -75,6 +79,7 @@ top-level commands:
select
list
add
create
enque
tag
play

View File

@ -307,17 +307,17 @@ Configuration:
option = line.poll()
switch (option) {
case 'album':
return selectMatches(Album, library.getAlbumsWhere({
mediaFileId: curMediaFile.id}))
return select(getExactlyOne(Album, library.getAlbumsWhere({
mediaFileId: curMediaFile.id})))
case 'artist':
return selectMatches(Artist, library.getArtistsWhere({
return selectMatches(library.getArtistsWhere({
mediaFileId: curMediaFile.id}))
case 'playlist':
return selectMatches(Playlist, playQueue)
return selectMatches(playQueue)
case 'file':
return selectMatches(MediaFile, curMediaFile)
return selectMatches(curMediaFile)
case 'tags':
return selectMatches(Tag, library.getTagsWhere({
return selectMatches(library.getTagsWhere({
mediaFileId: curMediaFile.id}))
default:
setErr("Unrecognized option to ${promptStyle}select " +
@ -327,21 +327,16 @@ Configuration:
}
switch (option) {
case 'album':
return selectMatches(Album,
library.getByIdOrName(Album, line.join(' ')))
case 'artist':
return selectMatches(Artist,
library.getByIdOrName(Artist, line.join(' ')))
case 'playlist':
return selectMatches(Playlist,
library.getByIdOrName(Playlist, line.join(' ')))
case 'file':
return selectMatches(MediaFile,
library.getByIdOrName(MediaFile, line.join(' ')))
case 'tags':
return selectMatches(Tag,
library.getByIdOrName(Tag, line.join(' ')))
case 'album': return selectMatches(
library.getByIdOrName(Album, line.join(' ')))
case 'artist': return selectMatches(
library.getByIdOrName(Artist, line.join(' ')))
case 'playlist': return selectMatches(
library.getByIdOrName(Playlist, line.join(' ')))
case 'file': return selectMatches(
library.getByIdOrName(MediaFile, line.join(' ')))
case 'tags': return selectMatches(
library.getByIdOrName(Tag, line.join(' ')))
default:
setErr("Unrecognized option to ${promptStyle}select${errorStyle}")
@ -349,24 +344,29 @@ Configuration:
}
}
private def processNew(LinkedList line) {
def option = line.poll()
switch(option) {
case 'playlist':
case 'bookmark':
default:
printLongMessage("Unrecognized option to the ${promptStyle}" +
"new${normalStyle} command. Use ${promptStyle}help new" +
"${normalStyle} to see a list of valid options.")
return null
}
}
private def processAdd(LinkedList line) {
def options = line.poll()
def option = line.poll()
def thingToAddId, targetPlaylistId
def errMsg = "Invalid options to the ${promptStyle}add${normalStyle}" +
" command. Use ${promptStyle}help add${normalStyle} to see a " +
"list of valid options.";
if (option != "selection" && !(thingToAddId = line.poll())) {
printLongMessage(errMsg); return null }
if (line.poll() != 'to' && line.poll() != 'playlist') {
printLongMessage(errMsg); return null }
if (!(targetPlaylistId = line.poll())) {
printLongMessage(errMsg); return null }
switch(option) {
case 'playlist':
case 'bookmark':
case 'selection':
case 'album':
case 'artist':
case 'file':
// TODO
default:
printLongMessage("Unrecognized option to the ${promptStyle}" +
"add${normalStyle} command. Use ${promptStyle}help add" +
@ -516,20 +516,27 @@ Configuration:
String key = uncapitalize(modelClass.simpleName)
this.selection[key] = null }
public def selectMatches(Class modelClass, def matches) {
String englishName = toEnglish(modelClass.simpleName)
public def ensureExactlyOne(def matches) {
if (!matches) {
setErr("No $englishName matches.");
setErr("Nothing matches.");
return null }
else if (matches.size() > 1) {
String englishName = toEnglish(modelClass.simpleName)
if (matches.size() > 1) {
setErr("Multiple ${englishName}s match: " +
matches.collect { "${it.id}: ${it.name}" }.join(', '))
return null }
selection[uncapitalize(modelClass.simpleName)] = matches[0]
resetStatus()
return matches[0] }
public def getExactlyOne(Class modelClass, def criteria) {
return ensureExactlyOne(library.getByIdOrName(modelClass, criteria)) }
public def selectMatches(def matches) {
def match = ensureExactlyOne(matches)
if (match) selection[uncapitalize(match.class.simpleName)] = match
return match }
private void drawLeader(afterOutput = false) {
String leader = beforeLeader + getLeader() +