Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Feature: Frabjous.Parser.handle now returns the first object that was…

… created
  • Loading branch information...
commit 97ed2899040cf940717cd72ea0fd69e2b6f1e576 1 parent a5e3a0a
Theo Cushion authored
View
53 spec/javascripts/ParsedItemsSpec.js
@@ -0,0 +1,53 @@
+describe("ParsedItems", function(){
+ var items;
+
+ beforeEach(function(){
+ items = new Frabjous.ParsedItems();
+ });
+
+ describe("add", function(){
+ it("should add a new item to the list and return item", function(){
+ var returned = items.add({id: "23", foo: "bar"});
+ expect(returned).toEqual({id: "23", foo: "bar"});
+ expect(items.all()).toEqual([{id: "23", foo: "bar"}]);
+ });
+ it("should not add an item if it is null", function(){
+ var returned = items.add(null);
+ expect(returned).toEqual(undefined);
+ expect(items.all()).toEqual([]);
+ });
+ it("should not add an item if it is undefined", function(){
+ var returned = items.add(undefined);
+ expect(returned).toEqual(undefined);
+ expect(items.all()).toEqual([]);
+ });
+ it("should not add an item if it does not have an id", function(){
+ var returned = items.add({foo: "bar"});
+ expect(returned).toEqual(undefined);
+ expect(items.all()).toEqual([]);
+ });
+ it("should update an item in the list if already there and return merged item", function(){
+ items.add({id: "23", foo: "1", bar: "2" });
+
+ var returned = items.add({id: "23", foo: "one", baz: "three"});
+ expect(returned).toEqual({id: "23", foo: "one", bar: "2", baz: "three"});
+ expect(items.all()).toEqual([{id: "23", foo: "one", bar: "2", baz: "three"}]);
+ });
+ it("should deep merge objects", function(){
+ items.add({id: "23", nested: {foo: "bar"}});
+
+ var returned = items.add({id: "23", nested: {baz: "b"}});
+ expect(returned).toEqual({id: "23", nested: {foo: "bar", baz: "b"}});
+ expect(items.all()).toEqual([{id: "23", nested: {foo: "bar", baz: "b"}}]);
+ });
+ });
+
+ describe("all", function(){
+ it("should return array of unique items", function(){
+ items.add({id: "a", foo: "a"});
+ items.add({id: "b", foo: "b"});
+
+ expect(items.all()).toEqual([{id: "a", foo: "a"}, {id: "b", foo: "b"}]);
+ });
+ });
+});
View
55 spec/javascripts/ParserSpec.js
@@ -1,53 +1,18 @@
-describe("ParsedItems", function(){
- var items;
+describe("Parser", function(){
- beforeEach(function(){
- items = new Frabjous.ParsedItems();
- });
+ var subject = Frabjous.Parser;
- describe("add", function(){
- it("should add a new item to the list and return item", function(){
- var returned = items.add({id: "23", foo: "bar"});
- expect(returned).toEqual({id: "23", foo: "bar"});
- expect(items.all()).toEqual([{id: "23", foo: "bar"}]);
- });
- it("should not add an item if it is null", function(){
- var returned = items.add(null);
- expect(returned).toEqual(undefined);
- expect(items.all()).toEqual([]);
- });
- it("should not add an item if it is undefined", function(){
- var returned = items.add(undefined);
- expect(returned).toEqual(undefined);
- expect(items.all()).toEqual([]);
- });
- it("should not add an item if it does not have an id", function(){
- var returned = items.add({foo: "bar"});
- expect(returned).toEqual(undefined);
- expect(items.all()).toEqual([]);
- });
- it("should update an item in the list if already there and return merged item", function(){
- items.add({id: "23", foo: "1", bar: "2" });
+ describe("handle", function(){
+
+ it("should return the first created object", function(){
+ var s = createStanza("<message type='chat' to='bob@bar.com' from='alice@bar.com'><body>hello there</body></message>");
- var returned = items.add({id: "23", foo: "one", baz: "three"});
- expect(returned).toEqual({id: "23", foo: "one", bar: "2", baz: "three"});
- expect(items.all()).toEqual([{id: "23", foo: "one", bar: "2", baz: "three"}]);
- });
- it("should deep merge objects", function(){
- items.add({id: "23", nested: {foo: "bar"}});
+ var r = subject.handle(s);
- var returned = items.add({id: "23", nested: {baz: "b"}});
- expect(returned).toEqual({id: "23", nested: {foo: "bar", baz: "b"}});
- expect(items.all()).toEqual([{id: "23", nested: {foo: "bar", baz: "b"}}]);
+ var e = Frabjous.Store.find(Frabjous.Message, s.id());
+ expect(r).toBe(r);
});
+
});
- describe("all", function(){
- it("should return array of unique items", function(){
- items.add({id: "a", foo: "a"});
- items.add({id: "b", foo: "b"});
-
- expect(items.all()).toEqual([{id: "a", foo: "a"}, {id: "b", foo: "b"}]);
- });
- });
});
View
6 src/parser/parser.js
@@ -19,16 +19,18 @@ Frabjous.Parser = function(){
}
var items = itemsList.all();
+ var first_result;
for(var i in items){
if(items.hasOwnProperty(i)){
var item = items[i];
var frabjous_type = item.frabjous_type;
delete item.frabjous_type;
Frabjous.log.debug("Parsed "+frabjous_type+":",item);
- Frabjous.Store.load_and_find(frabjous_type,item);
+ var result = Frabjous.Store.load_and_find(frabjous_type,item);
+ if(Ember.none(first_result)){ first_result = result; }
}
}
-
+ return first_result;
},
handlers: function(){
return _handlers;
View
2  src/storage/store.js
@@ -5,6 +5,6 @@ Frabjous.Store = DS.Store.create({
revision: 3,
load_and_find: function(type,hash){
this.load(type,hash);
- this.find(type,hash.id);
+ return this.find(type,hash.id);
}
});
Please sign in to comment.
Something went wrong with that request. Please try again.