1
|
|
|
module.exports = function(GedcomX){ |
2
|
|
|
|
3
|
|
|
var utils = require('../utils'); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Metadata about the structure and layout of a record, as well as the expected |
7
|
|
|
* data to be extracted from a record. |
8
|
|
|
* |
9
|
|
|
* @see {@link https://github.com/FamilySearch/gedcomx-record/blob/master/specifications/record-specification.md#record-descriptor|GEDCOM X Records Spec} |
10
|
|
|
* |
11
|
|
|
* @class RecordDescriptor |
12
|
|
|
* @extends ExtensibleData |
13
|
|
|
* @param {Object} [json] |
14
|
|
|
*/ |
15
|
|
|
var RecordDescriptor = function(json){ |
16
|
|
|
|
17
|
|
|
// Protect against forgetting the new keyword when calling the constructor |
18
|
|
|
if(!(this instanceof RecordDescriptor)){ |
19
|
|
|
return new RecordDescriptor(json); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
// If the given object is already an instance then just return it. DON'T copy it. |
23
|
|
|
if(RecordDescriptor.isInstance(json)){ |
24
|
|
|
return json; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
this.init(json); |
|
|
|
|
28
|
|
|
}; |
29
|
|
|
|
30
|
|
|
RecordDescriptor.prototype = Object.create(GedcomX.ExtensibleData.prototype); |
31
|
|
|
|
32
|
|
|
RecordDescriptor._gedxClass = RecordDescriptor.prototype._gedxClass = 'GedcomX.RecordDescriptor'; |
33
|
|
|
|
34
|
|
|
RecordDescriptor.jsonProps = [ |
35
|
|
|
'lang', |
36
|
|
|
'fields' |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Check whether the given object is an instance of this class. |
41
|
|
|
* |
42
|
|
|
* @param {Object} obj |
43
|
|
|
* @returns {Boolean} |
44
|
|
|
*/ |
45
|
|
|
RecordDescriptor.isInstance = function(obj){ |
46
|
|
|
return utils.isInstance(obj, this._gedxClass); |
47
|
|
|
}; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Initialize from JSON |
51
|
|
|
* |
52
|
|
|
* @param {Object} |
|
|
|
|
53
|
|
|
* @return {RecordDescriptor} this |
54
|
|
|
*/ |
55
|
|
|
RecordDescriptor.prototype.init = function(json){ |
56
|
|
|
|
57
|
|
|
GedcomX.ExtensibleData.prototype.init.call(this, json); |
58
|
|
|
|
59
|
|
|
if(json){ |
60
|
|
|
this.setLang(json.lang); |
61
|
|
|
this.setFields(json.fields); |
62
|
|
|
} |
63
|
|
|
return this; |
64
|
|
|
}; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Set the lang |
68
|
|
|
* |
69
|
|
|
* @param {String} lang |
70
|
|
|
* @return {RecordDescriptor} this |
71
|
|
|
*/ |
72
|
|
|
RecordDescriptor.prototype.setLang = function(lang){ |
73
|
|
|
this.lang = lang; |
74
|
|
|
return this; |
75
|
|
|
}; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get the lang |
79
|
|
|
* |
80
|
|
|
* @return {String} lang |
81
|
|
|
*/ |
82
|
|
|
RecordDescriptor.prototype.getLang = function(){ |
83
|
|
|
return this.lang; |
84
|
|
|
}; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Set the fields |
88
|
|
|
* |
89
|
|
|
* @param {FieldDescriptor[]} fields |
90
|
|
|
* @return {RecordDescriptor} this |
91
|
|
|
*/ |
92
|
|
|
RecordDescriptor.prototype.setFields = function(fields){ |
93
|
|
|
return this._setArray(fields, 'fields', 'addField'); |
94
|
|
|
}; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Add a field |
98
|
|
|
* |
99
|
|
|
* @param {FieldDescriptor} field |
100
|
|
|
* @return {RecordDescriptor} this |
101
|
|
|
*/ |
102
|
|
|
RecordDescriptor.prototype.addField = function(field){ |
103
|
|
|
return this._arrayPush(field, 'fields', GedcomX.FieldDescriptor); |
104
|
|
|
}; |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get the fields |
108
|
|
|
* |
109
|
|
|
* @return {FieldDescriptor[]} |
110
|
|
|
*/ |
111
|
|
|
RecordDescriptor.prototype.getFields = function(){ |
112
|
|
|
return this.fields || []; |
113
|
|
|
}; |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Export the object as JSON |
117
|
|
|
* |
118
|
|
|
* @return {Object} JSON object |
119
|
|
|
*/ |
120
|
|
|
RecordDescriptor.prototype.toJSON = function(){ |
121
|
|
|
return this._toJSON(GedcomX.ExtensibleData, RecordDescriptor.jsonProps); |
122
|
|
|
}; |
123
|
|
|
|
124
|
|
|
GedcomX.RecordDescriptor = RecordDescriptor; |
125
|
|
|
|
126
|
|
|
}; |