1 | View Code Duplication | var utils = require('../utils'), |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
2 | Base = require('../Base'); |
||
3 | |||
4 | /** |
||
5 | * Common attributes for all Atom entities |
||
6 | * |
||
7 | * @see {@link https://tools.ietf.org/html/rfc4287#page-7|RFC 4287} |
||
8 | * |
||
9 | * @class AtomCommon |
||
10 | * @extends Base |
||
11 | * @param {Object} [json] |
||
12 | */ |
||
13 | var AtomCommon = function(json){ |
||
14 | |||
15 | // Protect against forgetting the new keyword when calling the constructor |
||
16 | if(!(this instanceof AtomCommon)){ |
||
17 | return new AtomCommon(json); |
||
18 | } |
||
19 | |||
20 | // If the given object is already an instance then just return it. DON'T copy it. |
||
21 | if(AtomCommon.isInstance(json)){ |
||
22 | return json; |
||
23 | } |
||
24 | |||
25 | this.init(json); |
||
0 ignored issues
–
show
|
|||
26 | }; |
||
27 | |||
28 | AtomCommon.prototype = Object.create(Base.prototype); |
||
29 | |||
30 | AtomCommon._gedxClass = AtomCommon.prototype._gedxClass = 'GedcomX.AtomCommon'; |
||
31 | |||
32 | AtomCommon.jsonProps = [ |
||
33 | 'base', |
||
34 | 'lang' |
||
35 | ]; |
||
36 | |||
37 | /** |
||
38 | * Check whether the given object is an instance of this class. |
||
39 | * |
||
40 | * @param {Object} obj |
||
41 | * @returns {Boolean} |
||
42 | */ |
||
43 | AtomCommon.isInstance = function(obj){ |
||
44 | return utils.isInstance(obj, this._gedxClass); |
||
45 | }; |
||
46 | |||
47 | /** |
||
48 | * Initialize from JSON |
||
49 | * |
||
50 | * @param {Object} |
||
0 ignored issues
–
show
|
|||
51 | * @return {AtomCommon} this |
||
52 | */ |
||
53 | AtomCommon.prototype.init = function(json){ |
||
54 | |||
55 | Base.prototype.init.call(this, json); |
||
56 | |||
57 | if(json){ |
||
58 | this.setBase(json.base); |
||
59 | this.setLang(json.lang); |
||
60 | } |
||
61 | return this; |
||
62 | }; |
||
63 | |||
64 | /** |
||
65 | * Set the base attribute |
||
66 | * |
||
67 | * @param {String} base |
||
68 | * @return {AtomCommon} this |
||
69 | */ |
||
70 | AtomCommon.prototype.setBase = function(base){ |
||
71 | this.base = base; |
||
72 | return this; |
||
73 | }; |
||
74 | |||
75 | /** |
||
76 | * Get the base |
||
77 | * |
||
78 | * @return {String} base |
||
79 | */ |
||
80 | AtomCommon.prototype.getBase = function(base){ |
||
81 | return this.base; |
||
82 | }; |
||
83 | |||
84 | /** |
||
85 | * Set the lang |
||
86 | * |
||
87 | * @param {String} lang |
||
88 | * @return {AtomCommon} this |
||
89 | */ |
||
90 | AtomCommon.prototype.setLang = function(lang){ |
||
91 | this.lang = lang; |
||
92 | return this; |
||
93 | }; |
||
94 | |||
95 | /** |
||
96 | * Get the lang |
||
97 | * |
||
98 | * @return {String} lang |
||
99 | */ |
||
100 | AtomCommon.prototype.getLang = function(lang){ |
||
101 | return this.lang; |
||
102 | }; |
||
103 | |||
104 | /** |
||
105 | * Export the object as JSON |
||
106 | * |
||
107 | * @return {Object} JSON object |
||
108 | */ |
||
109 | AtomCommon.prototype.toJSON = function(){ |
||
110 | return this._toJSON(Base, AtomCommon.jsonProps); |
||
111 | }; |
||
112 | |||
113 | module.exports = AtomCommon; |
||
114 |