Skip to content

Commit

Permalink
move safeParent to Context class
Browse files Browse the repository at this point in the history
  • Loading branch information
timcharper committed Jun 3, 2014
1 parent 297ea90 commit 7c2a3a5
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions src/main/scala/com/gilt/handlebars/context/Context.scala
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,6 @@ object Context {
case /* UndefinedValue |*/ None | false | Nil | null | "" => false
case _ => true
}

/**
* Returns the parent of the provided context, but skips artificial levels in the hierarchy
* introduced by Iterable, Option, etc.
*/
def safeParent(ctx: Context[_]): Context[Any] = {
if (ctx.isRoot || ctx.isUndefined) {
ctx
} else {
ctx.parent.model match {
case map:Map[_,_] => ctx.parent
case list:Iterable[_] => safeParent(ctx.parent.parent)
case _ => ctx.parent
}
}
}
}

trait Context[+T] extends ContextFactory with Loggable {
Expand All @@ -100,6 +84,22 @@ trait Context[+T] extends ContextFactory with Loggable {

def truthValue: Boolean = Context.truthValue(model)

/**
* Returns the parent of the provided context, but skips artificial levels in the hierarchy
* introduced by Iterable, Option, etc.
*/
def safeParent: Context[Any] = {
if (this.isRoot || this.isUndefined) {
this
} else {
this.parent.model match {
case map:Map[_,_] => this.parent
case list:Iterable[_] => this.parent.parent.safeParent
case _ => this.parent
}
}
}

def lookup(path: List[String], args: List[Any]): Context[Any] = {
path.head match {
case p if isUndefined => this
Expand All @@ -111,9 +111,9 @@ trait Context[+T] extends ContextFactory with Loggable {
} else {
if (path.tail.isEmpty) {
// Just the parent, '..'. Path doesn't access any property on it.
Context.safeParent(this)
safeParent
} else {
Context.safeParent(this).lookup(path.tail, args)
safeParent.lookup(path.tail, args)
}
}

Expand Down

0 comments on commit 7c2a3a5

Please sign in to comment.