Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small Fixes/Additions #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions frameworks/foundation/debug/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ Fictum = {

registerUrl: function(url, response, options) {
this._ensureServerIsSetup();
this.server.registerUrl(url, response, options);
this.server.registerUrl(url, response, options || {});
},

responseFor: function(url, options) {
this._ensureServerIsSetup();
options.url = url;
return this.server.responseFor(url, options);
},

Expand All @@ -37,6 +38,11 @@ Fictum = {
return this.server.addResource(type, attributes);
},

removeResource: function(type, key, value) {
this._ensureServerIsSetup();
return this.server.removeResource(type, key, value);
},

startInterceptingRequests: function() {
if(Fictum.originalSendFunction != undefined)
throw new Error('ERROR: Already intercepting requests');
Expand All @@ -45,11 +51,13 @@ Fictum = {
SC.Request.reopen({
send: function(original, context) {
if(Fictum.isARegisteredUrl(this.get('address'))) {
var response = Fictum.responseFor(this.get('address'), {json: this.get('isJSON')});
var response = Fictum.responseFor(this.get('address'), {json: this.get('isJSON'), type: this.get('type'), body: context});
response.set('request', this);
setTimeout(function() {
response.set('status', 200);
response.notify();
SC.run(function() {
response.set('status', 200);
response.notify();
});
}, 1);
return response;
} else {
Expand Down
8 changes: 8 additions & 0 deletions frameworks/foundation/debug/resource_store.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ Fictum.ResourceStore = SC.Object.extend({
return resourceType.addResource(attributes);
},

removeResource: function(type, key, value) {
var resourceType = this._resourceTypeFor(type);
if (resourceType === null) {
throw new Error('ERROR: You requested to remove a resource of type "' + type + '", but that type has not been registered in the resource store');
}
return resourceType.removeResource(key, value);
},

allOfType: function(type) {
var resourceType = this._resourceTypeFor(type);
if(! resourceType)
Expand Down
26 changes: 23 additions & 3 deletions frameworks/foundation/debug/resource_type.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,31 @@ Fictum.ResourceType = SC.Object.extend({
return newResource;
},

removeResource: function(key, value) {
var resourceIndex = this._resourceIndexFor(key, value);
if (resourceIndex > -1) { this.get('resources').removeAt(resourceIndex); }
return resourceIndex > -1;
},

_attributeHashFor: function(attributes) {
var defaultAttributes = this.get('defaultAttributes');
for( var key in attributes) {
defaultAttributes[key] = attributes[key];
for( var key in defaultAttributes) {
if (attributes[key] === undefined) { attributes[key] = defaultAttributes[key]; }
}
return defaultAttributes;
return attributes;
},

_resourceIndexFor: function(key, value) {
var resources = this.get('resources'),
len = resources.get('length') || 0,
idx;

for (idx = 0; idx < len; idx++) {
if (resources[idx] && resources[idx][key] === value) {
return idx;
}
}

return -1;
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ sc_require('debug/base');
sc_require('debug/utils/json');

Fictum.DynamicResponse = SC.Object.extend({
value: function(store) {
return JSON.stringify(this.get('response')(store));
value: function(store, options) {
return JSON.stringify(this.get('response')(store, options));
}
});
4 changes: 4 additions & 0 deletions frameworks/foundation/debug/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,9 @@ Fictum.Server = SC.Object.extend({

addResourceType: function(type, defaultAttributes) {
this.get('resourceStore').addResourceType(type, defaultAttributes);
},

removeResource: function(type, key, value) {
return this.get('resourceStore').removeResource(type, attributes);
}
});
2 changes: 1 addition & 1 deletion frameworks/foundation/debug/url_stub.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Fictum.UrlStub = SC.Object.extend({
},

getResponse: function(store, options) {
var response = this.get('response').value(store);
var response = this.get('response').value(store, options);
if(options && options.json) response = jQuery.parseJSON(response);
return SC.Response.create({body: response, status: this.get('status')});
},
Expand Down
1 change: 1 addition & 0 deletions frameworks/foundation/debug/url_stub_collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Fictum.UrlStubCollection = SC.Object.extend({

responseFor: function(url, resourceStore, options) {
var urlStub = this._findUrlStubByUrl(url);
options.matches = urlStub.matchesUrl(url);
return urlStub === null ? undefined : urlStub.getResponse(resourceStore, options);
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ sc_require('debug/url_types/base');

Fictum.RegularExpressionUrl = Fictum.Url.extend({
matches: function(url) {
return url.match(this.get('url')) !== null;
var match = url.match(this.get('url'));
return match || false;
}
});