diff --git a/src/Ordinance.js b/src/Ordinance.js new file mode 100644 index 0000000..0d46082 --- /dev/null +++ b/src/Ordinance.js @@ -0,0 +1,146 @@ +module.exports = function(GedcomX){ + + /** + * LDS ordinance + * + * @class Ordinance + * @extends Reservation + * @param {Object} [json] + */ + var Ordinance = function(json){ + + // Protect against forgetting the new keyword when calling the constructor + if(!(this instanceof Ordinance)){ + return new Ordinance(json); + } + + // If the given object is already an instance then just return it. DON'T copy it. + if(Ordinance.isInstance(json)){ + return json; + } + + this.init(json); + }; + + Ordinance.prototype = Object.create(GedcomX.Reservation.prototype); + + Ordinance._gedxClass = Ordinance.prototype._gedxClass = 'GedcomX.Ordinance'; + + Ordinance.jsonProps = [ + 'living', + 'date', + 'templeCode' + ]; + + /** + * Check whether the given object is an instance of this class. + * + * @memberof Ordinance + * @static + * @param {Object} obj + * @returns {Boolean} + */ + Ordinance.isInstance = function(obj){ + return GedcomX.utils.isInstance(obj, this._gedxClass); + }; + + /** + * Initialize from JSON + * + * @memberof Ordinance + * @param {Object} + * @return Ordinance this + */ + Ordinance.prototype.init = function(json){ + + GedcomX.Reservation.prototype.init.call(this, json); + + if(json){ + this.setLiving(json.living); + this.setDate(json.date); + this.setTempleCode(json.templeCode); + } + return this; + }; + + /** + * Get the living flag + * + * @memberof Ordinance + * @returns {Boolean} living + */ + Ordinance.prototype.getLiving = function(){ + return this.living; + }; + + /** + * Set the living + * + * @memberof Ordinance + * @param {Boolean} living + * @returns {Ordinance} this + */ + Ordinance.prototype.setLiving = function(living){ + this.living = living; + return this; + }; + + /** + * Get the date + * + * @memberof Ordinance + * @returns {Date} date + */ + Ordinance.prototype.getDate = function(){ + return this.date; + }; + + /** + * Set the date + * + * @memberof Ordinance + * @param {Date} date + * @returns {Ordinance} this + */ + Ordinance.prototype.setDate = function(date){ + if(date){ + this.date = GedcomX.Date(date); + } + return this; + }; + + /** + * Get the templeCode + * + * @memberof Ordinance + * @returns {String} templeCode + */ + Ordinance.prototype.getTempleCode = function(){ + return this.templeCode; + }; + + /** + * Set the templeCode + * + * @memberof Ordinance + * @param {String} templeCode + * @returns {Ordinance} this + */ + Ordinance.prototype.setTempleCode = function(templeCode){ + this.templeCode = templeCode; + return this; + }; + + /** + * Export the object as JSON + * + * @memberof Ordinance + * @return {Object} JSON object + */ + Ordinance.prototype.toJSON = function(){ + return this._toJSON(GedcomX.Reservation, Ordinance.jsonProps); + }; + + GedcomX.Ordinance = Ordinance; + +}; \ No newline at end of file diff --git a/src/Person.js b/src/Person.js index 5c761c3..77399ca 100644 --- a/src/Person.js +++ b/src/Person.js @@ -4,7 +4,7 @@ module.exports = function(GedcomX){ // Extend serialization properties - GedcomX.Person.jsonProps.push('discussion-references'); + GedcomX.Person.jsonProps.push('discussion-references', 'ordinances'); // Override init() so that we can deserialize normalized values var oldInit = GedcomX.Person.prototype.init; @@ -12,6 +12,7 @@ module.exports = function(GedcomX){ oldInit.call(this, json); if(json){ this.setDiscussionReferences(json['discussion-references']); + this.setOrdinances(json.ordinances); } }; @@ -53,4 +54,42 @@ module.exports = function(GedcomX){ return this['discussion-references'] || []; }; + /** + * Set the ordinances + * + * @function setOrdinances + * @instance + * @memberof Person + * @param {Ordinance[]} ordinances + * @return {Person} this + */ + GedcomX.Person.prototype.setOrdinances = function(ordinances){ + return this._setArray(ordinances, 'ordinances', 'addOrdinance'); + }; + + /** + * Add an ordinance + * + * @function addOrdinance + * @instance + * @memberof Person + * @param {Ordinance} ordinance + * @return {Person} this + */ + GedcomX.Person.prototype.addOrdinance = function(ordinance){ + return this._arrayPush(ordinance, 'ordinances', GedcomX.Ordinance); + }; + + /** + * Get the ordinances + * + * @function getOrdinances + * @instance + * @memberof Person + * @return {Ordinance[]} ordinances + */ + GedcomX.Person.prototype.getOrdinances = function(){ + return this.ordinances || []; + }; + }; \ No newline at end of file diff --git a/src/Reservation.js b/src/Reservation.js new file mode 100644 index 0000000..0166137 --- /dev/null +++ b/src/Reservation.js @@ -0,0 +1,224 @@ +module.exports = function(GedcomX){ + + /** + * LDS ordinance reservation + * + * @class Reservation + * @extends Conclusion + * @param {Object} [json] + */ + var Reservation = function(json){ + + // Protect against forgetting the new keyword when calling the constructor + if(!(this instanceof Reservation)){ + return new Reservation(json); + } + + // If the given object is already an instance then just return it. DON'T copy it. + if(Reservation.isInstance(json)){ + return json; + } + + this.init(json); + }; + + Reservation.prototype = Object.create(GedcomX.Conclusion.prototype); + + Reservation._gedxClass = Reservation.prototype._gedxClass = 'GedcomX.Reservation'; + + Reservation.jsonProps = [ + 'type', + 'status', + 'spouse', + 'father', + 'mother', + 'assignee' + ]; + + /** + * Check whether the given object is an instance of this class. + * + * @memberof Reservation + * @static + * @param {Object} obj + * @returns {Boolean} + */ + Reservation.isInstance = function(obj){ + return GedcomX.utils.isInstance(obj, this._gedxClass); + }; + + /** + * Initialize from JSON + * + * @memberof Reservation + * @param {Object} + * @return Reservation this + */ + Reservation.prototype.init = function(json){ + + GedcomX.Conclusion.prototype.init.call(this, json); + + if(json){ + this.setType(json.type); + this.setStatus(json.status); + this.setSpouse(json.spouse); + this.setFather(json.father); + this.setMother(json.mother); + this.setAssignee(json.assignee); + } + return this; + }; + + /** + * Get the type + * + * @memberof Reservation + * @returns {String} type + */ + Reservation.prototype.getType = function(){ + return this.type; + }; + + /** + * Set the type + * + * @memberof Reservation + * @param {String} type + * @returns {Reservation} this + */ + Reservation.prototype.setType = function(type){ + this.type = type; + return this; + }; + + /** + * Get the status + * + * @memberof Reservation + * @returns {String} status + */ + Reservation.prototype.getStatus = function(){ + return this.status; + }; + + /** + * Set the status + * + * @memberof Reservation + * @param {String} status + * @returns {Reservation} this + */ + Reservation.prototype.setStatus = function(status){ + this.status = status; + return this; + }; + + /** + * Get the spouse + * + * @memberof Reservation + * @returns {ResourceReference} spouse + */ + Reservation.prototype.getSpouse = function(){ + return this.spouse; + }; + + /** + * Set the spouse + * + * @memberof Reservation + * @param {ResourceReference} spouse + * @returns {Reservation} this + */ + Reservation.prototype.setSpouse = function(spouse){ + if(spouse){ + this.spouse = GedcomX.ResourceReference(spouse); + } + return this; + }; + + /** + * Get the father + * + * @memberof Reservation + * @returns {ResourceReference} father + */ + Reservation.prototype.getFather = function(){ + return this.father; + }; + + /** + * Set the father + * + * @memberof Reservation + * @param {ResourceReference} father + * @returns {Reservation} this + */ + Reservation.prototype.setFather = function(father){ + if(father){ + this.father = GedcomX.ResourceReference(father); + } + return this; + }; + + /** + * Get the mother + * + * @memberof Reservation + * @returns {ResourceReference} mother + */ + Reservation.prototype.getMother = function(){ + return this.mother; + }; + + /** + * Set the mother + * + * @memberof Reservation + * @param {ResourceReference} mother + * @returns {Reservation} this + */ + Reservation.prototype.setMother = function(mother){ + if(mother){ + this.mother = GedcomX.ResourceReference(mother); + } + return this; + }; + + /** + * Get the assignee + * + * @memberof Reservation + * @returns {ResourceReference} assignee + */ + Reservation.prototype.getAssignee = function(){ + return this.assignee; + }; + + /** + * Set the assignee + * + * @memberof Reservation + * @param {ResourceReference} assignee + * @returns {Reservation} this + */ + Reservation.prototype.setAssignee = function(assignee){ + if(assignee){ + this.assignee = GedcomX.ResourceReference(assignee); + } + return this; + }; + + /** + * Export the object as JSON + * + * @memberof Reservation + * @return {Object} JSON object + */ + Reservation.prototype.toJSON = function(){ + return this._toJSON(GedcomX.Conclusion, Reservation.jsonProps); + }; + + GedcomX.Reservation = Reservation; + +}; \ No newline at end of file diff --git a/src/index.js b/src/index.js index 7868902..e58c049 100644 --- a/src/index.js +++ b/src/index.js @@ -18,6 +18,8 @@ module.exports = function(GedcomX){ require('./MergeAnalysis')(GedcomX); require('./MergeConflict')(GedcomX); require('./NameFormInfo')(GedcomX); + require('./Reservation')(GedcomX); + require('./Ordinance')(GedcomX); require('./SearchInfo')(GedcomX); require('./SourceReference')(GedcomX); require('./Tag')(GedcomX); diff --git a/test/Ordinance.js b/test/Ordinance.js new file mode 100644 index 0000000..78c0df4 --- /dev/null +++ b/test/Ordinance.js @@ -0,0 +1,73 @@ +var assert = require('chai').assert, + GedcomX = require('gedcomx-js'); + +var json = { + "living": false, + "type" : "http://lds.org/Baptism", + "status" : "http://familysearch.org/v1/Completed", + "date" : { + "formal" : "+2011-12-21" + }, + "templeCode" : "NZEAL", + "spouse" : { + "resource" : "https://familysearch.org/platform/tree/persons/LCXV-DST", + }, + "father" : { + "resource" : "https://familysearch.org/platform/tree/persons/LDJP-635", + }, + "mother" : { + "resource" : "https://familysearch.org/platform/tree/persons/LDJP-6WB", + }, + "assignee": { + "resource": "http://lds.org" + } +}; + +describe('Ordinance', function(){ + + it('Create plain', function(){ + assert.instanceOf(new GedcomX.Ordinance(), GedcomX.Ordinance, 'An instance of Ordinance is not returned when calling the constructor with new.'); + assert.instanceOf(GedcomX.Ordinance(), GedcomX.Ordinance, 'An instance of Ordinance is not returned when calling the constructor without new.'); + }); + + it('Create with JSON', function(){ + test(GedcomX.Ordinance(json)); + }); + + it('Build', function(){ + test(GedcomX.Ordinance() + .setLiving(json.living) + .setType(json.type) + .setStatus(json.status) + .setDate(json.date) + .setTempleCode(json.templeCode) + .setSpouse(json.spouse) + .setFather(json.father) + .setMother(json.mother) + .setAssignee(json.assignee) + ); + }); + + it('toJSON', function(){ + assert.deepEqual(GedcomX.Ordinance(json).toJSON(), json); + }); + + it('constructor does not copy instances', function(){ + var obj1 = GedcomX.Ordinance(); + var obj2 = GedcomX.Ordinance(obj1); + assert.strictEqual(obj1, obj2); + }); + +}); + +function test(ordinance){ + assert.equal(ordinance.getLiving(), json.living); + assert.equal(ordinance.getType(), json.type); + assert.equal(ordinance.getStatus(), json.status); + assert.equal(ordinance.getDate().getFormal(), json.date.formal); + assert.equal(ordinance.getTempleCode(), json.templeCode); + assert.equal(ordinance.getSpouse().getResource(), json.spouse.resource); + assert.equal(ordinance.getFather().getResource(), json.father.resource); + assert.equal(ordinance.getMother().getResource(), json.mother.resource); + assert.equal(ordinance.getAssignee().getResource(), json.assignee.resource); +} \ No newline at end of file diff --git a/test/Person.js b/test/Person.js index 3dc6fe8..5bbd0db 100644 --- a/test/Person.js +++ b/test/Person.js @@ -41,4 +41,42 @@ describe('Person property extensions', function(){ }); + describe('ordinances', function(){ + + var json = { + "ordinances" : [ { + "type" : "http://lds.org/Confirmation", + "status" : "http://familysearch.org/v1/Completed", + "templeCode" : "NZEAL" + } ] + }; + + it('Create with JSON', function(){ + test(GedcomX.Person(json)); + }); + + it('Build', function(){ + test(GedcomX.Person() + .addOrdinance( + GedcomX.Ordinance() + .setType(json.ordinances[0].type) + .setStatus(json.ordinances[0].status) + .setTempleCode(json.ordinances[0].templeCode) + )); + }); + + it('toJSON', function(){ + assert.deepEqual(GedcomX.Person(json).toJSON(), json); + }); + + function test(person){ + assert.equal(person.getOrdinances().length, 1); + var ordinance = person.getOrdinances()[0]; + assert.equal(ordinance.getType(), json.ordinances[0].type); + assert.equal(ordinance.getStatus(), json.ordinances[0].status); + assert.equal(ordinance.getTempleCode(), json.ordinances[0].templeCode); + } + + }); + }); \ No newline at end of file