Skip to content

Commit 045caca

Browse files
committed
private _adapter, ES6 setters and getters
1 parent 23e55e9 commit 045caca

File tree

2 files changed

+56
-17
lines changed

2 files changed

+56
-17
lines changed

spec/AdaptableController.spec.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ describe("AdaptableController", ()=>{
1515
var adapter = new FilesAdapter();
1616
var controller = new FilesController(adapter);
1717
expect(controller.adapter).toBe(adapter);
18+
// make sure _adapter is private
19+
expect(controller._adapter).toBe(undefined);
20+
// Override _adapter is not doing anything
21+
controller._adapter = "Hello";
22+
expect(controller.adapter).toBe(adapter);
1823
done();
1924
});
2025

@@ -26,6 +31,17 @@ describe("AdaptableController", ()=>{
2631
done();
2732
});
2833

34+
it("should fail setting the wrong adapter to the controller", (done) => {
35+
function WrongAdapter() {};
36+
var adapter = new FilesAdapter();
37+
var controller = new FilesController(adapter);
38+
var otherAdapter = new WrongAdapter();
39+
expect(() => {
40+
controller.adapter = otherAdapter;
41+
}).toThrow();
42+
done();
43+
});
44+
2945
it("should fail to instantiate a controller with wrong adapter", (done) => {
3046
function WrongAdapter() {};
3147
var adapter = new WrongAdapter();
@@ -41,4 +57,31 @@ describe("AdaptableController", ()=>{
4157
}).toThrow();
4258
done();
4359
});
60+
61+
it("should accept an object adapter", (done) => {
62+
var adapter = {
63+
createFile: function(config, filename, data) { },
64+
deleteFile: function(config, filename) { },
65+
getFileData: function(config, filename) { },
66+
getFileLocation: function(config, filename) { },
67+
}
68+
expect(() => {
69+
new FilesController(adapter);
70+
}).not.toThrow();
71+
done();
72+
});
73+
74+
it("should accept an object adapter", (done) => {
75+
function AGoodAdapter() {};
76+
AGoodAdapter.prototype.createFile = function(config, filename, data) { };
77+
AGoodAdapter.prototype.deleteFile = function(config, filename) { };
78+
AGoodAdapter.prototype.getFileData = function(config, filename) { };
79+
AGoodAdapter.prototype.getFileLocation = function(config, filename) { };
80+
81+
var adapter = new AGoodAdapter();
82+
expect(() => {
83+
new FilesController(adapter);
84+
}).not.toThrow();
85+
done();
86+
});
4487
});

src/Controllers/AdaptableController.js

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,22 @@ based on the parameters passed
88
99
*/
1010

11+
// _adapter is private, use Symbol
12+
var _adapter = Symbol();
13+
1114
export class AdaptableController {
12-
/**
13-
* Check whether the api call has master key or not.
14-
* @param {options} the adapter options
15-
* @param {defaultAdapter} the default adapter class or object to use
16-
* @discussion
17-
* Supported options types:
18-
* - string: the options will be loaded with required, when loaded, if default
19-
* is set on the returned object, we'll use that one to support modules
20-
* - object: a plain javascript object (options.constructor === Object), if options.adapter is set, we'll try to load it with the same mechanics.
21-
* - function: we'll create a new instance from that function, and pass the options object
22-
*/
23-
constructor(adapter, options) {
24-
this.setAdapter(adapter, options);
15+
16+
constructor(adapter) {
17+
this.adapter = adapter;
2518
}
26-
27-
setAdapter(adapter, options) {
19+
20+
set adapter(adapter) {
2821
this.validateAdapter(adapter);
29-
this.adapter = adapter;
30-
this.options = options;
22+
this[_adapter] = adapter;
23+
}
24+
25+
get adapter() {
26+
return this[_adapter];
3127
}
3228

3329
expectedAdapterType() {

0 commit comments

Comments
 (0)