summaryrefslogtreecommitdiff
path: root/test/pending/run/reify_addressbook.scala
blob: d53a0f7bc00026e31bd71429fcb70bd77fe67609 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import scala.reflect.runtime.universe._
import scala.tools.reflect.Eval

object Test extends App {
  reify {
    case class Person(name: String, age: Int)

    /** An AddressBook takes a variable number of arguments
     *  which are accessed as a Sequence
     */
    class AddressBook(a: Person*) {
      private val people: List[Person] = a.toList

      /** Serialize to XHTML. Scala supports XML literals
       *  which may contain Scala expressions between braces,
       *  which are replaced by their evaluation
       */
      def toXHTML =
        <table cellpadding="2" cellspacing="0">
          <tr>
            <th>Name</th>
            <th>Age</th>
          </tr>
          { for (p <- people) yield
              <tr>
                <td> { p.name } </td>
                <td> { p.age.toString() } </td>
              </tr>
          }
        </table>;
    }

    /** We introduce CSS using raw strings (between triple
     *  quotes). Raw strings may contain newlines and special
     *  characters (like \) are not interpreted.
     */
    val header =
      <head>
        <title>
          { "My Address Book" }
        </title>
        <style type="text/css"> {
       """table { border-right: 1px solid #cccccc; }
          th { background-color: #cccccc; }
          td { border-left: 1px solid #acacac; }
          td { border-bottom: 1px solid #acacac;"""}
        </style>
      </head>;

    val people = new AddressBook(
      Person("Tom", 20),
      Person("Bob", 22),
      Person("James", 19));

    val page =
      <html>
        { header }
        <body>
         { people.toXHTML }
        </body>
      </html>;

    println(page)
  }.eval
}