Tagged: Scala RSS

  • Tyler 11:04 am on July 30, 2008 Permalink
    Tags: Scala   

    Scala: Silly things I’ve done. 

    I had a list of objects, say like this:

    val l = List("aaa", "bbb", "ccc")

    and I wanted to produce something like this:

    <td id=IDaaa>aaa</td>
    <td id=IDbbb>bbb</td>
    <td id=IDccc>ccc</td>

    I first did this:

    l.map(u => <td id="ID" + {u}>{u}</td>)

    and that failed, unsurprisingly. That’s not even close to valid xml.

    The real way is this:

    l.map(u => <td id={"ID" + u}>{u}</td>)

    This post brought to you by “Expose your Ignorance.”

     
  • Tyler 7:46 pm on April 22, 2008 Permalink
    Tags: , , Scala   

    Getting my nerd on. 

    I will be attending ScalaLiftOff on Saturday May 10th, 2008 from 9am to 5pm.

    What’s ScalaLiftOff?

    The Scala lift off is a great place for members of the Scala and lift communities to get together, learn about Scala, learn about lift and get to know each other face to face. There will be Scala tracks going all day long and lift tracks to suits everyone’s interests.

     
  • Tyler 3:03 pm on March 11, 2008 Permalink
    Tags: gravatars, , , Scala   

    Gravatars and Liftweb. 

    Liftweb and Gravatars together at last.

    Here’s the snippet:
    package net.liftweb.example.snippet

    import net.liftweb.example.model._
    import scala.xml.{NodeSeq, Text, Group, Node, Elem}
    import net.liftweb.http._
    import net.liftweb.http.S
    import net.liftweb.mapper._
    import net.liftweb.http.S._
    import net.liftweb.util.Helpers._
    import net.liftweb.util._
    import java.util.Locale
    import net.liftweb.sitemap._
    import net.liftweb.sitemap.Loc._
    import java.security._

    // gravatar_id - MD5 sum of your email address
    // size - image size
    // rating - rating of the image, let's start with "G" which is also the default

    class Gravatar {
    val theUser: User = User.currentUser.openOr(new User)

    private def getMD5(s: String): String = {
    val m = MessageDigest.getInstance("MD5")
    m.update(s.getBytes(),0,s.length())
    BigInt(1,m.digest()).toString(16)
    }

    def getGravatar(xhtml: NodeSeq): NodeSeq = {
    var src = "http://www.gravatar.com/avatar.php?gravatar_id=" + getMD5(S.attr("e").openOr(theUser.email))
    src = src + "&size=" + S.attr("s").openOr("42")
    src = src + "&rating=" + S.attr("r").openOr("G")

    <img src={src}/>
    }

    And here’s how to use it in your templates:

    <lift :Gravatar.getGravatar e="email address" s="80" r="G" />

     
  • Tyler 2:05 pm on November 24, 2007 Permalink
    Tags: , , mysql, Scala   

    Using MySql with Lift 

    I need to use MySQL with my Lift webapp and here’s how I did it. In Boot.scala I replaced the Derby DBVendor object with a MySQL object. Hope that helps.

    Add this to your pom.xml:

            <dependency>
              <groupid>mysql</groupid>
              <artifactid>mysql-connector-java</artifactid>
              <version>5.0.41</version>
            </dependency>
    

    object DBVendor extends ConnectionManager {
    def newConnection(name: ConnectionIdentifier): Can[Connection] = {
    try
    {
    Class.forName("com.mysql.jdbc.Driver")
    val dm = DriverManager.getConnection("jdbc:mysql://localhost:3306/database_name", "user", "password")
    Full(dm)
    }
    catch
    {
    case e : Exception => e.printStackTrace; Empty
    }
    }
    def releaseConnection(conn: Connection)
    {
    conn.close
    }
    }

    Next up is learning how to set up my domain objects properly.

     
c
compose new post
j
next post/next comment
k
previous post/previous comment
r
reply
e
edit
o
show/hide comments
t
go to top
l
go to login
h
show/hide help
shift + esc
cancel