diff --git a/.gitignore b/.gitignore index b4d8c05..59f5eb1 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ config/database.yml tmp/* doc/api doc/app +*.rvmrc *.swp *.swo *.un~ diff --git a/README.mkd b/README.mkd index ccdcc35..46682aa 100644 --- a/README.mkd +++ b/README.mkd @@ -55,6 +55,12 @@ this.userObject = BackboneFactory.create('user'); This will create objects using the [defaults](http://documentcloud.github.com/backbone/#Model-defaults) you have in your class definitions. +Creating lists of objects, + +```javscript +this.postObjects = BackboneFactory.create_list('post', 3); // Creates an array of 3 posts +this.userObjects = BackboneFactory.create_list('user', 7, options); // Creates an array of 7 users with given options +``` ### Defining Sequences diff --git a/public/javascripts/backbone-factory.js b/public/javascripts/backbone-factory.js index dec1479..26ead80 100644 --- a/public/javascripts/backbone-factory.js +++ b/public/javascripts/backbone-factory.js @@ -33,13 +33,21 @@ if(this.factories[factory_name] === undefined){ throw "Factory with name " + factory_name + " does not exist"; } - return this.factories[factory_name].apply(null, [options]); + return this.factories[factory_name].apply(null, [options]); + }, + + create_list: function(factory_name, count, options){ + var list = []; + for(var i = 0; i < count; i++){ + list.push(this.create(factory_name, options)); + } + return list; }, define_sequence: function(sequence_name, callback){ this.sequences[sequence_name] = {} this.sequences[sequence_name]['counter'] = 0; - this.sequences[sequence_name]['callback'] = callback; + this.sequences[sequence_name]['callback'] = callback; }, next: function(sequence_name){ @@ -47,7 +55,7 @@ throw "Sequence with name " + sequence_name + " does not exist"; } this.sequences[sequence_name]['counter'] += 1; - return this.sequences[sequence_name]['callback'].apply(null, [this.sequences[sequence_name]['counter']]); //= callback; + return this.sequences[sequence_name]['callback'].apply(null, [this.sequences[sequence_name]['counter']]); //= callback; } } })(); diff --git a/spec/javascripts/BackboneFactorySpec.js b/spec/javascripts/BackboneFactorySpec.js index 0356d18..7b17cda 100644 --- a/spec/javascripts/BackboneFactorySpec.js +++ b/spec/javascripts/BackboneFactorySpec.js @@ -4,7 +4,7 @@ describe("Backbone Factory", function() { beforeEach(function() { var emailSequence = BackboneFactory.define_sequence('email', function(n){ - return "person"+n+"@example.com"; + return "person"+n+"@example.com"; }); }); @@ -19,7 +19,7 @@ describe("Backbone Factory", function() { beforeEach(function() { var emailSequence = BackboneFactory.define_sequence('person_email', function(n){ - return "person"+n+"@example.com"; + return "person"+n+"@example.com"; }); var postFactory = BackboneFactory.define('post', Post, function(){ return { @@ -38,19 +38,19 @@ describe("Backbone Factory", function() { this.postObject = BackboneFactory.create('post'); this.userObject = BackboneFactory.create('user'); }); - + it("return an instance of the Backbone Object requested", function() { expect(this.postObject instanceof Post).toBeTruthy(); expect(this.userObject instanceof User).toBeTruthy(); }); - + // Not sure if this test is needed. But what the hell! it("should preserve the defaults if not overriden", function() { expect(this.postObject.get('title')).toBe('Default Title'); }); - + it("should use the defaults supplied when creating objects", function() { expect(this.userObject.get('name')).toBe('Backbone User'); @@ -63,7 +63,7 @@ describe("Backbone Factory", function() { }); it("should work if other factories are passed", function(){ - expect(this.postObject.get('author') instanceof User).toBeTruthy(); + expect(this.postObject.get('author') instanceof User).toBeTruthy(); }) it("should override defaults if arguments are passed on creation", function(){ @@ -84,7 +84,32 @@ describe("Backbone Factory", function() { var secondID = BackboneFactory.create('user').id; expect(secondID).toBe(firstID + 1); }); - + + describe("Create List", function() { + + it("should create multiple models for create_list", function(){ + var count = 3; + var models = BackboneFactory.create_list('user', count); + expect(models.length).toEqual(count); + _.each(models, function(model) { + expect(model.id).toBeDefined(); + }); + }); + + it("should create multiple models for create_list with options", function(){ + var count = 3; + var favoriteColor = 'red'; + var options = function(){ return {favorite_color: favoriteColor}; }; + var models = BackboneFactory.create_list('user', count, options); + expect(models.length).toEqual(count); + _.each(models, function(model) { + expect(model.id).toBeDefined(); + expect(model.get('favorite_color')).toEqual(favoriteColor); + }); + }); + + }); + describe("Error Messages", function() { it("should throw an error if factory_name is not proper", function() { @@ -98,10 +123,10 @@ describe("Backbone Factory", function() { it("should throw an error if you try to use an undefined sequence", function() { expect(function(){BackboneFactory.next('undefined_sequence')}).toThrow("Sequence with name undefined_sequence does not exist"); }); - - }); - - }); - -}); + + }); + + }); + +}); diff --git a/spec/javascripts/support/jasmine.yml b/spec/javascripts/support/jasmine.yml index 3109481..ca92460 100644 --- a/spec/javascripts/support/jasmine.yml +++ b/spec/javascripts/support/jasmine.yml @@ -53,7 +53,7 @@ helpers: # - **/*[sS]pec.js # spec_files: - - **/*[sS]pec.js + - '**/*[sS]pec.js' # src_dir #