Permalink
Browse files

Merge branch 'master' of github.com:SpringSource/grails-data-mapping

  • Loading branch information...
graemerocher committed Mar 18, 2013
2 parents 3d63fc1 + 2291c81 commit 7d6327699a5d90c6738984bdf5dde6977e6e58ba
@@ -100,6 +100,27 @@ class Neo4jGormInstanceApi<D> extends GormInstanceApi<D> {
traverse(instance, order, stopEvaluator as StopEvaluator, returnableEvaluator as ReturnableEvaluator, args)
}
+ /**
+ * Allows accessing to dynamic properties with the dot operator
+ *
+ * @param instance The instance
+ * @param name The property name
+ * @return The property value
+ */
+ def propertyMissing(D instance, String name) {
+ getAt(instance, name)
+ }
+
+ /**
+ * Allows setting a dynamic property via the dot operator
+ * @param instance The instance
+ * @param name The property name
+ * @param val The value
+ */
+ def propertyMissing(D instance, String name, val) {
+ putAt(instance, name, val)
+ }
+
/**
* Allows subscript access to schemaless attributes.
*
@@ -160,6 +160,30 @@ class ApiExtensionsSpec extends GormDatastoreSpec {
person['someDoubleArray'] == [0.9, 1.0, 1.1]
}
+ def "test handling of non-declared properties using dot notation"() {
+ when:
+ def person = new Person(lastName:'person1').save(flush:true)
+ session.clear()
+ person = Person.load(person.id)
+ person.notDeclaredProperty = 'someValue' // n.b. the 'dot' notation is not valid for undeclared properties
+ person.emptyArray = []
+ person.someIntArray = [1,2,3]
+ person.someStringArray = ['a', 'b', 'c']
+ person.someDoubleArray= [0.9, 1.0, 1.1]
+ session.flush()
+ session.clear()
+ person = Person.get(person.id)
+
+ then:
+ person.notDeclaredProperty == 'someValue'
+ person.lastName == 'person1' // declared properties are also available via map semantics
+ person.someIntArray == [1,2,3]
+ person.someStringArray == ['a', 'b', 'c']
+ person.emptyArray == []
+ person.someDoubleArray == [0.9, 1.0, 1.1]
+ }
+
+
def "test handling of non-declared properties on transient instance"() {
when:
def person = new Person(lastName:'person1')
@@ -2,8 +2,8 @@ h1. schemaless attributes
h2. Purpose
-For domain classes mapped by Neo4j you can put and get arbitrary attributes on a instances by using map semantics on the
-domain instance.
+For domain classes mapped by Neo4j you can put and get arbitrary attributes on a instances by using the dot operator or
+map semantics on the domain instance.
{note}
Setting arbitrary attribute in only allowed when the domain instance is persisted (aka @save@ has been called)!
@@ -20,6 +20,7 @@ class Person implements Serializable {
}
{code}
+h3. using map semantics
{code}
when:
def person = new Person(lastName:'person1').save()
@@ -40,6 +41,30 @@ person['someStringArray'] == ['a', 'b', 'c']
person['someDoubleArray'] == [0.9, 1.0, 1.1]
{code}
+h3. using dot operator
+{code}
+when:
+def person = new Person(lastName:'person1').save(flush:true)
+session.clear()
+person = Person.load(person.id)
+person.notDeclaredProperty = 'someValue' // n.b. the 'dot' notation is not valid for undeclared properties
+person.emptyArray = []
+person.someIntArray = [1,2,3]
+person.someStringArray = ['a', 'b', 'c']
+person.someDoubleArray= [0.9, 1.0, 1.1]
+session.flush()
+session.clear()
+person = Person.get(person.id)
+
+then:
+person.notDeclaredProperty == 'someValue'
+person.lastName == 'person1' // declared properties are also available via map semantics
+person.someIntArray == [1,2,3]
+person.someStringArray == ['a', 'b', 'c']
+person.emptyArray == []
+person.someDoubleArray == [0.9, 1.0, 1.1]
+{code}
+
h2. Description
The non declared attribtes are stored a regular properties on the domain instance's node. The values of the schemaless

0 comments on commit 7d63276

Please sign in to comment.