Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/main/scala/com/escalatesoft/subcut/inject/BindingId.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,11 @@ abstract class BindingId {
if (shortName.last == '$') shortName.init else shortName
}
}

abstract class TypedBindingId[T] {
lazy val bindingName = {
val shortName = this.getClass.getSimpleName
// strip the trailing $ for objects
if (shortName.last == '$') shortName.init else shortName
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,7 @@ trait MutableBindingModule extends BindingModule { outer =>
* @param name the string name to identify the binding when used in combination with the type parameter.
*/
def idBy(name: String) = identifiedBy(name)
def idBy(id: TypedBindingId[T]) = identifiedBy(id.bindingName)
}

// and a parameterized bind method to kick it all off
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ class BindingIdObjectsTest extends FunSuite with Matchers {
import module._
bind [String] idBy Mammal toSingle "Cheetah"
bind [String] idBy Role toSingle "Run"
bind [String] idBy Vehicle toSingle "Gallop"
// bind [String] idBy Vehicle toSingle "Gallop"
bind [String] idBy PoolSize toSingle "Gallop"
}

val mtd = new Speedway
Expand All @@ -55,11 +56,12 @@ class BindingIdObjectsTest extends FunSuite with Matchers {
object Vehicle extends BindingId
object Role extends BindingId
object Mammal extends BindingId
object PoolSize extends TypedBindingId[Int]

class Speedway(implicit val bindingModule: BindingModule) extends Injectable {
val mammal = inject[String](Mammal)
val role = inject[String]('Role)
val vehicle = inject[String]("Vehicle")

override def toString = "I am a " + mammal + " that likes to " + role + " using a " + vehicle
}
}