Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

An even neater arrow notation for creating relationships, using only …

…--> for outgoing relationships and <-- for incoming relationships

A neat new notation for reading/writing properties on a node

Updated the NeoConvertersSpec to reflect the changes mentioned above
  • Loading branch information...
commit 7a09bb9004b28e69d567f819ed47d349b321be0b 1 parent fda9750
@jawher jawher authored
View
17 src/main/scala/com/eptcomputing/neo4j/NeoConverters.scala
@@ -19,13 +19,10 @@ import org.neo4j.api.core._
trait NeoConverters {
class NodeRelationshipMethods(node: Node) {
- // Create outgoing relationship
- def --|(relType: String) = new OutgoingRelationshipBuilder(node, DynamicRelationshipType.withName(relType))
- def --|(relType: RelationshipType) = new OutgoingRelationshipBuilder(node, relType)
+ def -->(relType: RelationshipType) = new OutgoingRelationshipBuilder(node, relType)
// Create incoming relationship
- def <--(relType: String) = new IncomingRelationshipBuilder(node, DynamicRelationshipType.withName(relType))
def <--(relType: RelationshipType) = new IncomingRelationshipBuilder(node, relType)
}
@@ -40,11 +37,21 @@ trait NeoConverters {
// Half-way through building an incoming relationship
class IncomingRelationshipBuilder(toNode: Node, relType: RelationshipType) {
- def |--(fromNode: Node) = {
+ def <--(fromNode: Node) = {
fromNode.createRelationshipTo(toNode, relType)
new NodeRelationshipMethods(fromNode)
}
}
implicit def node2relationshipBuilder(node: Node) = new NodeRelationshipMethods(node)
+
+ implicit def stringToRelationshipType(relType: String) = DynamicRelationshipType.withName(relType)
+
+ class RichNode(node: Node) {
+ def apply(property: String) = node.getProperty(property)
+ def update(property: String, value: Any) : Unit = node.setProperty(property, value)
+ }
+
+ implicit def nodeToRichNode(node: Node) = new RichNode(node)
+
}
View
39 src/test/scala/com/eptcomputing/neo4j/NeoConvertersSpec.scala
@@ -10,43 +10,43 @@ import org.neo4j.api.core._
@RunWith(classOf[JUnit4Runner])
class NeoConvertersSpec extends Spec with ShouldMatchers with NeoConverters {
describe("NeoConverters") {
- it("should create a new relationship in --| relType --> notation") {
+ it("should create a new relationship in --> relType --> notation") {
NeoServer.exec { neo =>
val start = neo.createNode
val end = neo.createNode
val relType = DynamicRelationshipType.withName("foo")
- start --| relType --> end
+ start --> relType --> end
start.getSingleRelationship(relType, Direction.OUTGOING).
getOtherNode(start) should equal(end)
}
}
- it("should create a new relationship in --| \"relName\" --> notation") {
+ it("should create a new relationship in --> \"relName\" --> notation") {
NeoServer.exec { neo =>
val start = neo.createNode
val end = neo.createNode
- start --| "foo" --> end
+ start --> "foo" --> end
start.getSingleRelationship(DynamicRelationshipType.withName("foo"), Direction.OUTGOING).
getOtherNode(start) should equal(end)
}
}
- it("should create a new relationship in <-- relType |-- notation") {
+ it("should create a new relationship in <-- relType <-- notation") {
NeoServer.exec { neo =>
val start = neo.createNode
val end = neo.createNode
val relType = DynamicRelationshipType.withName("foo")
- end <-- relType |-- start
+ end <-- relType <-- start
start.getSingleRelationship(relType, Direction.OUTGOING).
getOtherNode(start) should equal(end)
}
}
- it("should create a new relationship in <-- \"relName\" |-- notation") {
+ it("should create a new relationship in <-- \"relName\" <-- notation") {
NeoServer.exec { neo =>
val start = neo.createNode
val end = neo.createNode
- end <-- "foo" |-- start
+ end <-- "foo" <-- start
start.getSingleRelationship(DynamicRelationshipType.withName("foo"), Direction.OUTGOING).
getOtherNode(start) should equal(end)
}
@@ -57,7 +57,7 @@ class NeoConvertersSpec extends Spec with ShouldMatchers with NeoConverters {
val start = neo.createNode
val middle = neo.createNode
val end = neo.createNode
- start --| "foo" --> middle --| "bar" --> end
+ start --> "foo" --> middle --> "bar" --> end
start.getSingleRelationship(DynamicRelationshipType.withName("foo"), Direction.OUTGOING).
getOtherNode(start) should equal(middle)
middle.getSingleRelationship(DynamicRelationshipType.withName("bar"), Direction.OUTGOING).
@@ -70,7 +70,7 @@ class NeoConvertersSpec extends Spec with ShouldMatchers with NeoConverters {
val left = neo.createNode
val middle = neo.createNode
val right = neo.createNode
- left --| "foo" --> middle <-- "bar" |-- right
+ left --> "foo" --> middle <-- "bar" <-- right
left.getSingleRelationship(DynamicRelationshipType.withName("foo"), Direction.OUTGOING).
getOtherNode(left) should equal(middle)
right.getSingleRelationship(DynamicRelationshipType.withName("bar"), Direction.OUTGOING).
@@ -81,9 +81,26 @@ class NeoConvertersSpec extends Spec with ShouldMatchers with NeoConverters {
it("should ignore a relationshipBuilder with no end node") {
NeoServer.exec { neo =>
val start = neo.createNode
- start --| "foo"
+ start --> "foo"
start.getRelationships.iterator.hasNext should equal(false)
}
}
+
+ it("should read a property in a node('property') notation") {
+ NeoServer.exec { neo =>
+ val start = neo.createNode
+ start.setProperty("foo", "bar")
+ start("foo") should equal("bar")
+ }
+ }
+
+ it("should create a property in a node('property')=value notation") {
+ NeoServer.exec { neo =>
+ val start = neo.createNode
+ start("foo") = "bar"
+ start.getProperty("foo") should equal("bar")
+ }
+ }
+
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.