Completed
Push — master ( 2281e8...8b163e )
by Justin
01:35
created

src/Attribution.js   A

Complexity

Total Complexity 25
Complexity/F 1.92

Size

Lines of Code 188
Function Count 13

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
c 1
b 0
f 0
nc 1
dl 0
loc 188
rs 10
wmc 25
mnd 1
bc 25
fnc 13
bpm 1.923
cpm 1.923
noi 3

13 Functions

Rating   Name   Duplication   Size   Complexity  
A Attribution.setChangeMessage 0 4 1
B Attribution.toJSON 0 25 6
A Attribution.getChangeMessage 0 3 1
A Attribution.setCreated 0 6 2
A Attribution.getModified 0 3 1
A Attribution.setModified 0 6 2
B Attribution.js ➔ Attribution 0 22 4
A Attribution.getCreated 0 3 1
A Attribution.getContributor 0 3 1
A Attribution.getCreator 0 3 1
A Attribution.isInstance 0 3 1
A Attribution.setCreator 0 6 2
A Attribution.setContributor 0 6 2
1
var ExtensibleData = require('./ExtensibleData'),
2
    ResourceReference = require('./ResourceReference'),
3
    utils = require('./utils');
4
5
/**
6
 * Define who is contributing information, when they contributed it, 
7
 * and why they are making the contribution.
8
 * 
9
 * @constructor
10
 * @param {Object} [json]
0 ignored issues
show
Documentation introduced by
The parameter [json] does not exist. Did you maybe forget to remove this comment?
Loading history...
11
 */
12
var Attribution = function(json){
13
  
14
  // Protect against forgetting the new keyword when calling the constructor
15
  if(!(this instanceof Attribution)){
16
    return new Attribution(json);
17
  }
18
  
19
  // If the given object is already an instance then just return it. DON'T copy it.
20
  if(Attribution.isInstance(json)){
21
    return json;
22
  }
23
  
24
  ExtensibleData.call(this, json);
25
  
26
  if(json){
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if json is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
27
    this.setChangeMessage(json.changeMessage);
28
    this.setContributor(json.contributor);
29
    this.setCreated(json.created);
30
    this.setCreator(json.creator);
31
    this.setModified(json.modified);
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
32
  }
33
};
34
35
Attribution.prototype = Object.create(ExtensibleData.prototype);
36
37
Attribution._gedxClass = Attribution.prototype._gedxClass = 'GedcomX.Attribution';
38
39
/**
40
 * Check whether the given object is an instance of this class.
41
 * 
42
 * @param {Object} obj
43
 * @returns {Boolean}
44
 */
45
Attribution.isInstance = function(obj){
46
  return utils.isInstance(obj, this._gedxClass);
47
};
48
49
/**
50
 * Get the change message.
51
 * 
52
 * @returns {String} Change message
53
 */
54
Attribution.prototype.getChangeMessage = function(){
55
  return this.changeMessage;
56
};
57
58
/**
59
 * Set the change message.
60
 * 
61
 * @param {String} changeMessage
62
 * @returns {Attribution} This object
63
 */
64
Attribution.prototype.setChangeMessage = function(changeMessage){
65
  this.changeMessage = changeMessage;
66
  return this;
67
};
68
69
/**
70
 * Get the contributor.
71
 * 
72
 * @returns {ResourceReference} contributor
73
 */
74
Attribution.prototype.getContributor = function(){
75
  return this.contributor;
76
};
77
78
/**
79
 * Set the contributor
80
 * 
81
 * @param {Object|ResourceReference} contributor ResourceReference representing the contributor.
82
 * @returns {Attribution} This instance
83
 */
84
Attribution.prototype.setContributor = function(contributor){
85
  if(contributor){
86
    this.contributor = ResourceReference(contributor);
87
  }
88
  return this;
89
};
90
91
/**
92
 * Get the created timestamp
93
 * 
94
 * @returns {Date} created
95
 */
96
Attribution.prototype.getCreated = function(){
97
  return this.created;
98
};
99
100
/**
101
 * Set the created timestamp
102
 * 
103
 * @param {Date|Number} date Integer timestamp (milliseconds since epoch) or JavaScript Date instance
104
 * @returns {Attribution} This instance
105
 */
106
Attribution.prototype.setCreated = function(date){
107
  if(date){
108
    this.created = new Date(date);
109
  }
110
  return this;
111
};
112
113
/**
114
 * Get the creator
115
 * 
116
 * @returns {ResourceReference} Creator
117
 */
118
Attribution.prototype.getCreator = function(){
119
  return this.creator;
120
};
121
122
/**
123
 * Set the creator
124
 * 
125
 * @param {Object|ResourceReference} creator ResourceReference representing the creator.
126
 * @returns {Attribution} This instance
127
 */
128
Attribution.prototype.setCreator = function(creator){
129
  if(creator){
130
    this.creator = new ResourceReference(creator);
131
  }
132
  return this;
133
};
134
135
/**
136
 * Get the modified timestamp
137
 * 
138
 * @returns {Date} modified
139
 */
140
Attribution.prototype.getModified = function(){
141
  return this.modified;
142
};
143
144
/**
145
 * Set the modified timestamp
146
 * 
147
 * @param {Date|Number} date Integer timestamp (milliseconds since epoch) or JavaScript Date instance
148
 * @returns {Attribution} This instance
149
 */
150
Attribution.prototype.setModified = function(date){
151
  if(date){
152
    this.modified = new Date(date);
153
  }
154
  return this;
155
};
156
157
/**
158
 * Export the object as JSON
159
 * 
160
 * @return {Object} JSON object
161
 */
162
Attribution.prototype.toJSON = function(){
163
  var json = ExtensibleData.prototype.toJSON.call(this);
164
  
165
  if(this.changeMessage){
166
    json.changeMessage = this.changeMessage;
167
  }
168
  
169
  if(this.contributor){
170
    json.contributor = this.contributor.toJSON();
171
  }
172
  
173
  if(this.created){
174
    json.created = this.created.getTime();
175
  }
176
  
177
  if(this.creator){
178
    json.creator = this.creator.toJSON();
179
  }
180
  
181
  if(this.modified){
182
    json.modified = this.modified.getTime();
183
  }
184
  
185
  return json;
186
};
187
188
module.exports = Attribution;