DRY dispatch

116 views
Skip to first unread message

Etam

unread,
Jan 20, 2011, 1:50:56 PM1/20/11
to Lift
Hi,

I have an Image class:

class Image extends LongKeyedMapper[Image] with IdPK with Logger {

which overrides toHtml method:

override def toHtml =
<img src={"/gallery/image/%s/%s/%s/%s" format (slug.is, "fit",
100, 100)} />

and it works beacause of this:

def dispatch = {
LiftRules.dispatch.append {
case Req("gallery" :: "image" :: slug :: method ::
width :: height :: Nil, _, _) => {
() => Image.stream(slug, method, width, height)
}
}
}

As you can see this is **not DRY** approach, since you have to define
the URL (/gallery/image) twice.

Is it possible to make it DRY? Can you get the path from LiftRules or
something?

Thanks in advance,
Etam.

David Pollak

unread,
Jan 20, 2011, 2:03:05 PM1/20/11
to lif...@googlegroups.com
object ImageGallery {
  val path = "gallery" :: "image" :: Nil

  val pathLen = path.length

  def prefix = path.mkString("/", "/", "/")

  def unapply(in: List[String]): Option[List[String]] = Some(in.drop(pathLen)).filter(ignore => in.startsWith(path))
}


<img src={ImageGallery.prefix+"%s/%s"...}>

case Req(ImageGallery(slug :: method :: width :: height :: _, _, _) =>

If you get particularly adventurous, you can add an apply method to ImageGallery that takes the parameters and returns a String.  You can also return Some[(String, String, Int, Int)] (or whatever the input parameters to apply are) from unapply, so you'd get:

<img src={ImageGallery(p1, p2, p3, p4)}>

and

case Req(ImageGallery(p1, p2, p3, p4), _, _) =>


--
You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to lif...@googlegroups.com.
To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.




--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Blog: http://goodstuff.im
Surf the harmonics

David Pollak

unread,
Jan 20, 2011, 2:02:33 PM1/20/11
to lif...@googlegroups.com
object ImageGallery {
  val path = "gallery" :: "image" :: Nil

  val pathLen = path.length

  def prefix = path.mkString("/", "/", "/")

  def unapply(in: List[String]): Option[List[String]] = Some(in.drop(pathLen)).filter(ignore => in.startsWith(path))
}


<img src={ImageGallery.prefix+"%s/%s"...}>

case Req(ImageGallery(slug :: method :: width :: height :: _, _, _) =>

If you get particularly adventurous, you can add an apply method to ImageGallery that takes the parameters and returns a String.  You can also return Some[(String, String, Int, Int)] (or whatever the input parameters to apply are) from unapply, so you'd get:

<img src={ImageGallery(p1, p2, p3, p4)}>

and

case Req(ImageGallery(p1, p2, p3, p4), _, _) =>

On Thu, Jan 20, 2011 at 10:50 AM, Etam <odwr...@gmail.com> wrote:
--
You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to lif...@googlegroups.com.
To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.

Etam

unread,
Jan 23, 2011, 9:22:27 PM1/23/11
to Lift
Thanks!

How do you do this with:

object Profile {

val URL_SLUG_PARAM = "slug"
val URL_PATH = "profile" :: Nil
val URL_PREFIX = URL_PATH.mkString("/", "/", "/")

def rewrite = {
LiftRules.statelessRewrite.append {
case RewriteRequest(ParsePath(List("profile", slug),_,_,_),_,_)
=>
RewriteResponse(Profile.URL_PATH, Map("slug" -> slug))
}
}
}

?

Thanks in advance,
Etam.

Etam

unread,
Feb 11, 2011, 8:10:12 PM2/11/11
to Lift
This is the answer:

object SluggedMetaMapper {
def unapply(parsePath: ParsePath) = {
Some((parsePath.partPath.init, parsePath.partPath.last,
parsePath.suffix, parsePath.absolute, parsePath.endSlash))
}
}

---

val URL_PATH: List[String]
val URL_SLUG_PARAM = "slug"
lazy val URL_PREFIX = URL_PATH.mkString("/", "/", "/")
lazy val URL_LINK = Loc(URL_PATH.mkString("-"), URL_PATH,
URL_PATH.last, Loc.Hidden)

def rewrite = {
debug("Add rewrite %s<slug> -> %s" format (URL_PREFIX,
URL_PATH.mkString("/", "/", ".html")))
LiftRules.statelessRewrite.append {
case RewriteRequest(SluggedMetaMapper(URL_PATH, slug,_,_,_),_,_) =>
RewriteResponse(URL_PATH, Map(URL_SLUG_PARAM -> slug))
Reply all
Reply to author
Forward
0 new messages