私は、これらのようないくつかの機能があります:ReactiveMongo共通機能
import reactivemongo.play.json.collection.JSONCollection
def quotesFuture: Future[JSONCollection] = database.map(_.collection[JSONCollection]("quotes"))
1: def getByAuthor(author: String) = Action.async {
2: quotesFuture.flatMap{ quotes =>
3: quotes
4: .find(obj("author" -> author))
5: .cursor[JsObject](ReadPreference.primary)
6: .collect[List](maxQuotes)
7: .map(q => Ok(Json.toJson(q)))
8: }
9: }
10:
11: def search(term: String) = Action.async {
12: quotesFuture.flatMap{ quotes =>
13: quotes
14: .find(obj("$text" -> obj("$search" -> term)), textScore)
15: .sort(textScore)
16: .cursor[JsObject](ReadPreference.primary)
17: .collect[List](maxQuotes)
18: .map(q => Ok(Json.toJson(q)))
19: }
20: }
のが、繰り返しがたくさんあります。
100: def getByAuthor(author: String) = getList { quotes =>
101: quotes
102: .find(obj("author" -> author))
103: }
104:
105: def search(term: String) = getList { quotes =>
106: quotes
107: .find(obj("$text" -> obj("$search" -> term)), textScore)
108: .sort(textScore)
109: }
110:
111: def getList(query: (JSONCollection) => ???) = Action.async {
112: quotesFuture.flatMap{ quotes =>
113: query(quotes)
114: .cursor[JsObject](ReadPreference.primary)
115: .collect[List](maxQuotes)
116: .map(q => Ok(Json.toJson(q)))
117: }
118: }
質問はライン111で???
は何をする必要があります:次のように検索し、並べ替えている変更だけの事は、私はリファクタリングしたいのですが?線14-15からメソッドを抽出するためのIntelliJを求める場合
、それは以下
def tempdef(term: String, quotes: JSONCollection): GenericQueryBuilder[quotes.pack.type]#Self = {
quotes
.find(obj("$text" -> obj("$search" -> term)), textScore)
.sort(textScore)
}
を作成するのIntelliJによって提案された結果の型はかなり怖いです。したがって、行111の???
はGenericQueryBuilder[quotes.pack.type]#Self
である必要がありますが、変数quotes
に依存します。この作業を行うには、???
を何に置き換える必要がありますか? IntelliJのを使用
、私はquotes.pack
が参照する参照してください。私はJSONSerializationPack.type
によってライン111に???
を置き換える試み、それがコンパイルされ、動作
case class JSONCollection(...) {...
val pack = JSONSerializationPack
}
。
しかし、JSONCollection
の実装の詳細を見ると、不正行為になり、JSONCollectionの実装が変更された場合、これが機能しなくなる可能性があります。
したがって、ライン111の???
はどうなるでしょうか?
この例では、コードの重複を削除する簡単な方法がありますか?
、 'DEFリスト= {GETLIST _.find(セレクタ= OBJ())。カーソル[JSObjectを(ReadPreference.primary)}'。私は 'Cursor [JsObject](ReadPreference.primary)'のたびに複製する必要がありますが、すでに改善されています。ありがとうございます。 –
私は "世話をする"文を理解していません。この例はコンパイルして動作します。あなたはそれを言い換えることができますか? –