From 09db48682db3d5ac181c99fc5ba57a65aa53120c Mon Sep 17 00:00:00 2001 From: Mitar Date: Tue, 8 Apr 2014 03:23:21 -0700 Subject: [PATCH] Made objectify idempotent. --- lib.coffee | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib.coffee b/lib.coffee index 31a5d3f..9656aa5 100644 --- a/lib.coffee +++ b/lib.coffee @@ -107,11 +107,16 @@ class globals.Document if field.isArray throw new Error "Document does not match schema, not an array" unless _.isArray obj[name] - obj[name] = _.map obj[name], (o) => new field.targetDocument o + obj[name] = _.map obj[name], (o) => + if o instanceof field.targetDocument + o + else + new field.targetDocument o else throw new Error "Document does not match schema, ancestorArray does not match: #{ field.ancestorArray } vs. #{ ancestorArray }" if field.ancestorArray isnt ancestorArray throw new Error "Document does not match schema, not a plain object" unless isPlainObject obj[name] - obj[name] = new field.targetDocument obj[name] + if obj[name] not instanceof field.targetDocument + obj[name] = new field.targetDocument obj[name] else if isPlainObject field if _.isArray obj[name]