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

Adding create_list method for creating lists/array of objects #3

Open
wants to merge 2 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ config/database.yml
tmp/*
doc/api
doc/app
*.rvmrc
*.swp
*.swo
*.un~
Expand Down
6 changes: 6 additions & 0 deletions README.mkd
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 11 additions & 3 deletions public/javascripts/backbone-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,29 @@
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){
if(this.sequences[sequence_name] === undefined){
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;
}
}
})();
51 changes: 38 additions & 13 deletions spec/javascripts/BackboneFactorySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
});
});

Expand All @@ -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 {
Expand All @@ -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');
Expand All @@ -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(){
Expand All @@ -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() {
Expand All @@ -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");
});
});
});
});

});

});

});

2 changes: 1 addition & 1 deletion spec/javascripts/support/jasmine.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ helpers:
# - **/*[sS]pec.js
#
spec_files:
- **/*[sS]pec.js
- '**/*[sS]pec.js'

# src_dir
#
Expand Down