Completed
Push — master ( c7ea8a...8a62cc )
by Justin
01:44
created

src/core/ResourceReference.js   A

Complexity

Total Complexity 12
Complexity/F 1.71

Size

Lines of Code 103
Function Count 7

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
c 1
b 0
f 0
nc 2
dl 0
loc 103
rs 10
wmc 12
mnd 1
bc 11
fnc 7
bpm 1.5713
cpm 1.7142
noi 3

7 Functions

Rating   Name   Duplication   Size   Complexity  
A ResourceReference.js ➔ ResourceReference 0 14 3
A ResourceReference.getResource 0 3 1
A ResourceReference.setResource 0 4 1
A ResourceReference.init 0 9 2
A ResourceReference.isInstance 0 3 1
A ResourceReference.toJSON 0 3 1
A ResourceReference.matches 0 7 3
1
var utils = require('../utils'),
2
    Base = require('../Base');
3
4
/**
5
 * A generic reference to a resource.
6
 * 
7
 * @see {@link https://github.com/FamilySearch/gedcomx/blob/master/specifications/json-format-specification.md#resource-reference|GEDCOM X JSON Spec}
8
 * 
9
 * @class
10
 * @extends Base
11
 * @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...
12
 */
13
var ResourceReference = function(json){
14
  
15
  // Protect against forgetting the new keyword when calling the constructor
16
  if(!(this instanceof ResourceReference)){
17
    return new ResourceReference(json);
18
  }
19
  
20
  // If the given object is already an instance then just return it. DON'T copy it.
21
  if(ResourceReference.isInstance(json)){
22
    return json;
23
  }
24
  
25
  this.init(json);
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...
26
};
27
28
ResourceReference.prototype = Object.create(Base.prototype);
29
30
ResourceReference._gedxClass = ResourceReference.prototype._gedxClass = 'GedcomX.ResourceReference';
31
32
ResourceReference.jsonProps = ['resource'];
33
34
/**
35
 * Check whether the given object is an instance of this class.
36
 * 
37
 * @param {Object} obj
38
 * @returns {Boolean}
39
 */
40
ResourceReference.isInstance = function(obj){
41
  return utils.isInstance(obj, this._gedxClass);
42
};
43
44
/**
45
 * Initialize from JSON
46
 * 
47
 * @param {Object}
0 ignored issues
show
Documentation introduced by
The parameter * does not exist. Did you maybe forget to remove this comment?
Loading history...
48
 * @return {ResourceReference} this
49
 */
50
ResourceReference.prototype.init = function(json){
51
  
52
  Base.prototype.init.call(this, json);
53
  
54
  if(json){
55
    this.setResource(json.resource);
56
  }
57
  return this;
58
};
59
60
/**
61
 * Get the resource URI
62
 * 
63
 * @returns {String} Resource
64
 */
65
ResourceReference.prototype.getResource = function(){
66
  return this.resource;
67
};
68
69
/**
70
 * Set the resource URI
71
 * 
72
 * @param {String} uri
73
 * @returns {ResourceReference} this object
74
 */
75
ResourceReference.prototype.setResource = function(uri){
76
  this.resource = uri;
77
  return this;
78
};
79
80
/**
81
 * Check whether this reference matches the given resource.
82
 * 
83
 * @param {Base} resource Resource or ID
84
 * @return {Boolean}
85
 */
86
ResourceReference.prototype.matches = function(resource){
87
  if(resource === undefined){
88
    return false;
89
  }
90
  var id = '#' + (typeof resource === 'string' ? resource : resource.getId());
91
  return this.resource === id;
92
};
93
94
/**
95
 * Export the object as JSON
96
 * 
97
 * @return {Object} JSON object
98
 */
99
ResourceReference.prototype.toJSON = function(){
100
  return this._toJSON(Base, ResourceReference.jsonProps);
101
};
102
103
module.exports = ResourceReference;