anonymous / gist:7897557
Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Attempt for DSL in Scala on event that happened in Brno on 12/10/2013.

View gist:7897557
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 66
 
object define {
def alert(alertBuilder: AlertBuilder): Alert = {
alertBuilder.finish.getOrElse(
throw new IllegalStateException("can't build incomplete Alert")
)
}
}
 
 
abstract class AlertBuilder {
implicit def stringToAlertBuilder(string: String) = name
 
val props = scala.collection.mutable.Map.empty[String, Any]
 
def finish: Option[Alert] = {
Some(new Alert)
}
 
}
 
object having extends AlertBuilder {
def apply(alertBuilder: AlertBuilder): AlertBuilder = this
}
 
object name extends AlertBuilder {
def apply(alertBuilder: AlertBuilder): AlertBuilder = alertBuilder
 
def apply(string: String) = {
props += ("name" -> string)
this
}
}
 
object conditions extends AlertBuilder {
 
def apply(conditions: LogicOperation): AlertBuilder = {
props += ("conditions" -> conditions)
this
}
 
def any(list: List[Boolean]) = this
}
 
abstract class LogicOperation {
var conditions: List[Boolean] = _
 
def evaluate = conditions.reduceLeft(combine)
 
def combine(leftOp: Boolean, rightOp: Boolean): Boolean
 
def apply(conditions: List[Boolean]) = {
this.conditions = conditions
this
}
}
object any extends LogicOperation {
override def combine(l: Boolean, r: Boolean) = l || r
}
object all extends LogicOperation {
override def combine(l: Boolean, r: Boolean) = l && r
}
 
class Alert {
 
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.