-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Timestamp to provide a better type-safe reference to a date
- Loading branch information
1 parent
b241beb
commit dd315f8
Showing
7 changed files
with
50 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package lightdb | ||
|
||
import fabric.define.DefType | ||
import fabric.rw._ | ||
|
||
case class Timestamp(value: Long = System.currentTimeMillis()) extends AnyVal | ||
|
||
object Timestamp { | ||
implicit val rw: RW[Timestamp] = RW.from( | ||
r = _.value.json, | ||
w = j => Timestamp(j.asLong), | ||
d = DefType.Int | ||
) | ||
implicit val numeric: Numeric[Timestamp] = Numeric[Long].map(Timestamp.apply)(_.value) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
package lightdb.doc | ||
|
||
import lightdb.Timestamp | ||
|
||
trait RecordDocument[Doc <: RecordDocument[Doc]] extends Document[Doc] { | ||
def created: Long | ||
def modified: Long | ||
def created: Timestamp | ||
def modified: Timestamp | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
package lightdb.doc | ||
|
||
import fabric.rw._ | ||
import lightdb.field.Field | ||
import lightdb.Timestamp | ||
|
||
trait RecordDocumentModel[Doc <: RecordDocument[Doc]] extends DocumentModel[Doc] { | ||
val created: I[Long] = field.index("created", (doc: Doc) => doc.created) | ||
val modified: I[Long] = field.index("modified", (doc: Doc) => doc.modified) | ||
val created: I[Timestamp] = field.index("created", (doc: Doc) => doc.created) | ||
val modified: I[Timestamp] = field.index("modified", (doc: Doc) => doc.modified) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package object lightdb { | ||
implicit class NumericOps[A](numeric: Numeric[A]) { | ||
def map[B](to: A => B)(from: B => A): Numeric[B] = new Numeric[B] { | ||
override def plus(x: B, y: B): B = to(numeric.plus(from(x), from(y))) | ||
override def minus(x: B, y: B): B = to(numeric.minus(from(x), from(y))) | ||
override def times(x: B, y: B): B = to(numeric.times(from(x), from(y))) | ||
override def negate(x: B): B = to(numeric.negate(from(x))) | ||
override def fromInt(x: Int): B = to(numeric.fromInt(x)) | ||
override def toInt(x: B): Int = numeric.toInt(from(x)) | ||
override def toLong(x: B): Long = numeric.toLong(from(x)) | ||
override def toFloat(x: B): Float = numeric.toFloat(from(x)) | ||
override def toDouble(x: B): Double = numeric.toDouble(from(x)) | ||
override def compare(x: B, y: B): Int = numeric.compare(from(x), from(y)) | ||
override def parseString(str: String): Option[B] = numeric.parseString(str).map(to) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters