Skip to content

Instantly share code, notes, and snippets.

@alistair
Created March 5, 2015 22:36
Show Gist options
  • Save alistair/57b90d298d444ad8640c to your computer and use it in GitHub Desktop.
Save alistair/57b90d298d444ad8640c to your computer and use it in GitHub Desktop.
case class State(emailAddress: String, loginCount: Int, failedLoginCount: Int, locked: Boolean) {
/* Login to go here */
}
abstract class Event() {
def apply(s: State): State
}
case class AccountCreated(emailAddress: String) extends Event {
def apply(s: State) = State(emailAddress, 0, 0, false)
}
case class AccountLoggedIn() extends Event {
def apply(s: State) = State(s.emailAddress, s.loginCount + 1, 0, false)
}
case class AccountLoginFailed() extends Event {
def apply(s: State) = State(s.emailAddress, s.loginCount, s.failedLoginCount + 1, false)
}
case class AccountLocked() extends Event {
def apply(s: State) = State(s.emailAddress, s.loginCount, s.failedLoginCount, true)
}
case class AccountUnLocked() extends Event {
def apply(s: State) = State(s.emailAddress, s.loginCount, 0, false)
}
var stream = AccountUnLocked() :: AccountLocked() :: AccountLoginFailed() :: AccountLoginFailed() :: AccountLoggedIn() :: AccountLoggedIn() :: AccountLoggedIn() :: AccountCreated("[email protected]") :: Nil
(State("", 0, 0, false) /: stream.reverse)( (state, event) => event(state))
/* example login implemetation? don't really like this
def login(events: List[Event], password: String): List[Event] = {
if (password == "hello" && !locked)
AccountLoggedIn() :: events
else
events
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment