Skip to content

Instantly share code, notes, and snippets.

@chrhicks
Created March 21, 2013 01:28
Show Gist options
  • Save chrhicks/5209998 to your computer and use it in GitHub Desktop.
Save chrhicks/5209998 to your computer and use it in GitHub Desktop.
A navigation menu model
object menu extends App {
val admin = NavItem("Admin", Some("http://www.gilt.com/admin"))
.addChild("Sale", None)
.addChild(
NavItem("Product", Some("https://admin.gilt.com/product/"))
.addChild("Edit Product", Some("https://admin.gilt.com/proudct/edit/2123"))
)
println(admin)
}
trait NavMenuElement {
val label: String
val url: Option[String]
val children: List[NavMenuElement]
}
object NavItem {
def create(label: String, url: Option[String]) = NavItem(label, url)
}
case class NavItem(
override val label: String,
override val url: Option[String],
override val children: List[NavItem] = List()) extends NavMenuElement {
def addChild(childLabel: String, childUrl: Option[String]) = copy(children = children :+ NavItem(childLabel, childUrl))
def addChild(childItem: NavItem) = copy(children = children :+ childItem)
}
case class NavMenu(items: List[NavItem])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment