Skip to content

Instantly share code, notes, and snippets.

@nraychaudhuri
Created December 7, 2012 20:12
Show Gist options
  • Save nraychaudhuri/4236151 to your computer and use it in GitHub Desktop.
Save nraychaudhuri/4236151 to your computer and use it in GitHub Desktop.
import scala.xml._
object Entities {
trait RawAsset
trait VodAsset
trait Version
trait Title
trait Contribution
trait Genre
trait Rating
trait Season
trait Series
trait Person
}
import Entities._
sealed trait Provider
sealed trait DADC extends Provider
sealed trait TMS extends Provider
trait EntityGenerator[Provider, Source, Target] {
def generate(s: Source): Target
}
object EntityGenerator {
implicit val dadcRawAsset = new EntityGenerator[DADC, Elem, RawAsset] {
def generate(s: Elem): RawAsset = {
println("Hey dadc raw asset")
new RawAsset {}
}
}
implicit val tmsRawAsset = new EntityGenerator[TMS, Elem, RawAsset] {
def generate(s: Elem): RawAsset = {
println("Hey tms raw asset")
new RawAsset {}
}
}
}
object Example extends App {
trait Metadata
trait Factory[CP <: Provider] {
def rawAsset(xml: Elem)(implicit g: EntityGenerator[CP, Elem, RawAsset]): RawAsset = g.generate(xml)
def vodAsset(metadata: Metadata)(implicit g: EntityGenerator[CP, Metadata, VodAsset]): VodAsset = g.generate(metadata)
def version(metadata: Metadata)(implicit g: EntityGenerator[CP, Metadata, Version]): Version = g.generate(metadata)
def title(metadata: Metadata)(implicit g: EntityGenerator[CP, Metadata, Title]): Title = g.generate(metadata)
def contributors(metadata: Metadata)(implicit g: EntityGenerator[CP, Metadata, Contribution]): Contribution = g.generate(metadata)
def person(name: Option[String])(implicit g: EntityGenerator[CP, Option[String], Option[Person]]): Option[Person] = g.generate(name)
def genre(metadata: Metadata)(implicit g: EntityGenerator[CP, Metadata, Genre]): Genre = g.generate(metadata)
}
object DADCFactory extends Factory[DADC]
object TMSFactory extends Factory[TMS]
DADCFactory.rawAsset(<a/>)
TMSFactory.rawAsset(<a/>)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment