Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

support both EnumField (id) and EnumNameField (string)

  • Loading branch information...
commit 307e4bc90d5fd2db507d42acede5c7201078e31a 1 parent b104c82
@jliszka jliszka authored
View
10 src/main/scala/com/foursquare/rogue/QueryField.scala
@@ -8,8 +8,9 @@ import java.util.regex.Pattern
import net.liftweb.common.{Box, Full, Empty}
import net.liftweb.json.JsonAST.{JInt, JValue, JArray}
import net.liftweb.mongodb.record.{BsonRecord, MongoId, MongoRecord}
-import net.liftweb.record.{Field, MandatoryTypedField, OptionalTypedField}
import net.liftweb.mongodb.record.field.{BsonRecordField, BsonRecordListField, MongoCaseClassField, MongoCaseClassListField}
+import net.liftweb.record.{Field, MandatoryTypedField, OptionalTypedField}
+import net.liftweb.record.field.EnumField
import org.bson.types.ObjectId
import org.joda.time.DateTime
import scala.util.matching.Regex
@@ -98,11 +99,16 @@ class CalendarQueryField[M <: BsonRecord[M]](val field: Field[java.util.Calendar
def between(range: (DateTime, DateTime)) = new StrictBetweenQueryClause(field.name, range._1.toDate, range._2.toDate)
}
-class EnumerationQueryField[M <: BsonRecord[M], E <: Enumeration#Value](field: Field[E, M])
+class EnumNameQueryField[M <: BsonRecord[M], E <: Enumeration#Value](field: Field[E, M])
extends AbstractQueryField[E, String, M](field) {
override def valueToDB(e: E) = e.toString
}
+class EnumIdQueryField[M <: BsonRecord[M], E <: Enumeration](field: EnumField[M, E])
+ extends AbstractQueryField[E#Value, Int, M](field) {
+ override def valueToDB(e: E#Value) = e.id
+}
+
class GeoQueryField[M <: BsonRecord[M]](field: Field[LatLong, M])
extends AbstractQueryField[LatLong, java.util.List[Double], M](field) {
override def valueToDB(ll: LatLong) =
View
13 src/main/scala/com/foursquare/rogue/Rogue.scala
@@ -6,9 +6,10 @@ import com.foursquare.rogue.MongoHelpers.{AndCondition, MongoModify}
import java.util.Calendar
import net.liftweb.common.Box
import net.liftweb.mongodb.record.{BsonRecord, MongoId, MongoRecord, MongoMetaRecord}
-import net.liftweb.record.{Field, MandatoryTypedField, OptionalTypedField}
import net.liftweb.mongodb.record.field.{
BsonRecordField, BsonRecordListField, MongoCaseClassField, MongoCaseClassListField}
+import net.liftweb.record.{Field, MandatoryTypedField, OptionalTypedField}
+import net.liftweb.record.field.EnumField
import org.bson.types.ObjectId
/**
@@ -126,9 +127,13 @@ trait Rogue {
(f: Field[Double, M]): NumericQueryField[Double, M] =
new NumericQueryField(f)
- implicit def enumerationFieldToEnumerationQueryField[M <: BsonRecord[M], F <: Enumeration#Value]
- (f: Field[F, M]): EnumerationQueryField[M, F] =
- new EnumerationQueryField(f)
+ implicit def enumNameFieldToEnumNameQueryField[M <: BsonRecord[M], F <: Enumeration#Value]
+ (f: Field[F, M]): EnumNameQueryField[M, F] =
+ new EnumNameQueryField(f)
+
+ implicit def enumFieldToEnumQueryField[M <: BsonRecord[M], F <: Enumeration]
+ (f: EnumField[M, F]): EnumIdQueryField[M, F] =
+ new EnumIdQueryField(f)
implicit def enumerationListFieldToEnumerationListQueryField[M <: BsonRecord[M], F <: Enumeration#Value]
(f: Field[List[F], M]): EnumerationListQueryField[F, M] =
View
3  src/test/scala/com/foursquare/rogue/QueryTest.scala
@@ -31,6 +31,9 @@ class QueryTest extends SpecsMatchers {
Venue where (_.closed eqs true) toString() must_== """db.venues.find({ "closed" : true})"""
Venue where (_._id eqs oid) toString() must_== ("""db.venues.find({ "_id" : { "$oid" : "%s"}})""" format oid.toString)
VenueClaim where (_.status eqs ClaimStatus.approved) toString() must_== """db.venueclaims.find({ "status" : "Approved"})"""
+ VenueClaim where (_.reason eqs RejectReason.tooManyClaims) toString() must_== """db.venueclaims.find({ "reason" : 0})"""
+ VenueClaim where (_.reason eqs RejectReason.cheater) toString() must_== """db.venueclaims.find({ "reason" : 1})"""
+ VenueClaim where (_.reason eqs RejectReason.wrongCode) toString() must_== """db.venueclaims.find({ "reason" : 2})"""
VenueClaim where (_.venueid eqs oid) toString() must_== ("""db.venueclaims.find({ "vid" : { "$oid" : "%s"}})""" format oid.toString)
VenueClaim where (_.venueid eqs ven1.id) toString() must_== ("""db.venueclaims.find({ "vid" : { "$oid" : "%s"}})""" format oid1.toString)
View
9 src/test/scala/com/foursquare/rogue/TestModels.scala
@@ -81,13 +81,20 @@ object ClaimStatus extends Enumeration {
val approved = Value("Approved")
}
+object RejectReason extends Enumeration {
+ val tooManyClaims = Value("too many claims")
+ val cheater = Value("cheater")
+ val wrongCode = Value("wrong code")
+}
+
class VenueClaim extends MongoRecord[VenueClaim] with MongoId[VenueClaim] with Venue.FK[VenueClaim] {
def meta = VenueClaim
object userid extends LongField(this) { override def name = "uid" }
object status extends EnumNameField(this, ClaimStatus)
+ object reason extends EnumField(this, RejectReason)
}
object VenueClaim extends VenueClaim with MongoMetaRecord[VenueClaim] {
- override def fieldOrder = List(status, _id, userid, venueid)
+ override def fieldOrder = List(status, _id, userid, venueid, reason)
override def collectionName = "venueclaims"
override def mongoIdentifier = RogueTestMongo
}
Please sign in to comment.
Something went wrong with that request. Please try again.