Last active
August 29, 2015 14:13
-
-
Save mkantor/fbcc3a0e8b7b6b7a7fc6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
sealed abstract class LoyaltyDomain(val id: String) { | |
def name = { | |
this match { | |
case LoyaltyDomain.ANDROID => "Android" | |
case LoyaltyDomain.IOS => "iOS" | |
case LoyaltyDomain.AMAZON => "Amazon" | |
case LoyaltyDomain.WINPHONE => "Windows Phone" | |
case LoyaltyDomain.WINSTORE => "Windows Store" | |
// ??? | |
case LoyaltyDomain.UNDETERMINED => "Undetermined" | |
case LoyaltyDomain.ALL => "All" | |
} | |
} | |
override def toString = this.id | |
} | |
object LoyaltyDomain { | |
case object ANDROID extends LoyaltyDomain("com.google.play") | |
case object IOS extends LoyaltyDomain("com.apple.itunes") | |
case object AMAZON extends LoyaltyDomain("com.amazon.apps") | |
case object WINPHONE extends LoyaltyDomain("com.windowsphone") | |
case object WINSTORE extends LoyaltyDomain("com.windowsstore") | |
// This exists so we can iterate over the possible values (e.g. for the admin UI). I can't think of a | |
// non-reflective way to automate this, so I think we'll have to maintain this list along with the objects above. | |
val list: List[LoyaltyDomain] = List( | |
ANDROID, | |
IOS, | |
AMAZON, | |
WINPHONE, | |
WINSTORE | |
// TODO: undetermined/all? | |
) | |
// ??? | |
case object UNDETERMINED extends LoyaltyDomain("undetermined") | |
case object ALL extends LoyaltyDomain("all") | |
} | |
// ---------------------------------------------------------------------------- | |
// Normal usage will look like this: | |
def handleDomain(domain: LoyaltyDomain) = { | |
domain match { | |
case LoyaltyDomain.IOS => | |
// ... | |
case LoyaltyDomain.ANDROID => | |
// ... | |
case _ => | |
// We'll get warnings about non-exhaustive matches if we go with a sealed class/case objects. | |
} | |
} | |
handleDomain(LoyaltyDomain.ANDROID) | |
// For admin UI, etc: | |
val niceNames = LoyaltyDomain.list.map { domain => | |
domain.name -> domain.id | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment