Created
October 27, 2018 18:04
-
-
Save ponylang-gist/6538dc2a66ad7bcb52542005f6df0d4f to your computer and use it in GitHub Desktop.
Shared via Pony Playground (https://playground.ponylang.io/?gist=6538dc2a66ad7bcb52542005f6df0d4f)
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
class Movie is Comparable[Movie ref] | |
""" | |
What we know about a movie in the library. | |
""" | |
let id: U16 | |
let title: String | |
let location: U16 | |
let actors: String | |
let genre: U8 | |
new create( ident: U16, loc: U16, ttl: String, act: String, gen: U8 ) => | |
id = ident | |
title = ttl | |
location = loc | |
actors = act | |
genre = gen | |
new from_db( data: Array[U8] val ) => | |
""" | |
Create the object from a database record in packed form. | |
""" | |
var r: BinRecReader = BinRecReader | |
r.append( data ) | |
id = data(U8(MovieField.idTAG())) | |
title = data(U8(MovieField.ttlTAG())) | |
location = data(U8(MovieField.locTAG())) | |
genre = data(U8(MovieField.genTAG())) | |
actors = data(U8(MovieField.actTAG())) | |
fun pack(): Array[U8] => | |
""" | |
Pack all the fields into the format stored in the database. | |
""" | |
var w: BinRecWriter = BinRecWriter | |
w.add_pair( U8(MovieField.idTAG()), id ) | |
w.add_pair( U8(MovieField.genTAG()), genre ) | |
w.add_pair( U8(MovieField.ttlTAG()), title ) | |
w.add_pair( U8(MovieField.locTAG()), location ) | |
if actors.size() > 0 then | |
w.add_pair( U8(MovieField.actTAG()), actors ) | |
end | |
w.done() | |
fun val lt( other: Movie ): Bool => | |
""" | |
Determine whether this movie should sort before another movie. | |
""" | |
if genre < other.genre then | |
return true | |
end | |
if title < other.title then | |
return true | |
end | |
return false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment