Skip to content

Instantly share code, notes, and snippets.

@chandu0101
Last active November 24, 2015 08:58
Show Gist options
  • Save chandu0101/de73572dd4533a9764e9 to your computer and use it in GitHub Desktop.
Save chandu0101/de73572dd4533a9764e9 to your computer and use it in GitHub Desktop.
import org.scalajs.dom
object ScalaJSExample extends js.JSApp{
case class FieldName(name : String , tpe : String)
def main(): Unit = {
val name = "SeedCategory"
val input = s"""
|val id: String
|val category: String
""".stripMargin
val fieldTypes = input.split("\n").filter(_.nonEmpty).map(s => {
val s1 = s.trim.replace("val","").replace(" ","")
val s2 = s1.split(":")
FieldName(s2.head,s2.last)
}).filter(ft => ft.name.nonEmpty && ft.tpe.nonEmpty)
val comanionObject = s"""
|object $name {
|
| def apply(${fieldTypes.map(ft => s"${ft.name}: ${ft.tpe}").mkString(", ")})= {
| ${fieldTypes.map(ft => s"val ${ft.name}_sh = ${ft.name} ").mkString("\n")}
| new $name {
| ${fieldTypes.map(ft => s"val ${ft.name} = ${ft.name}_sh").mkString("\n")}
| }
| }
|
|}
""".stripMargin
val copyMethod = s"""
| def copy(${fieldTypes.map(ft => s"${ft.name}: ${ft.tpe} = ${ft.name}").mkString(", ")})= {
| ${fieldTypes.map(ft => s"val ${ft.name}_sh = ${ft.name} ").mkString("\n")}
| new $name {
| ${fieldTypes.map(ft => s"val ${ft.name} = ${ft.name}_sh").mkString("\n")}
| }
| }
|
""".stripMargin
println(comanionObject)
dom.window.console.log(comanionObject)
println(copyMethod)
dom.window.console.log(copyMethod)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment