Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Add an .after method to add callbacks to Factory builds #11

Merged
merged 1 commit into from about 1 year ago

2 participants

Mark Shirley Brandon Keepers
Mark Shirley

This pull request adds a simple callback so that one can have a little more control over the build process by having direct access to the object after it has been built. This has the benefit of giving you access to any sequence attributes after they have been created (for example primary key ids).

Mark Shirley maspwr referenced this pull request February 03, 2013
Closed

Provide .after callback #9

Brandon Keepers bkeepers merged commit a6dd012 into from February 04, 2013
Brandon Keepers bkeepers closed this February 04, 2013
Brandon Keepers
Owner

:+1: Looks great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Feb 03, 2013
Mark Shirley Add an .after method to add callbacks to Factory builds ddffb2f
This page is out of date. Refresh to see the latest.
10  README.md
Source Rendered
@@ -39,14 +39,20 @@ Which returns an object that looks roughly like:
39 39
       random_seed:  0.8999513240996748,
40 40
       players: [
41 41
                     {id: 1, name:'Player 1'},
42  
-                    {id: 1, name:'Player 2'}
  42
+                    {id: 2, name:'Player 2'}
43 43
       ]
44 44
     }
45 45
 
46 46
 For a factory with a constructor, if you want just the attributes:
47 47
 
48 48
     Factory.attributes('game') // return just the attributes
  49
+    
  50
+You can also define a callback function to be run after building an object:
  51
+
  52
+    Factory.define('coach').after(function(coach, options) { if (options.buildPlayer) { Factory.build('player', {coach_id: coach.id}; } })
  53
+    
  54
+    Factory.build('coach', {}, {buildPlayer: true});
49 55
 
50 56
 ## Credits
51 57
 
52  
-Thanks to [Daniel Morrison](http://twitter.com/danielmorrison/status/58883772040486912) for the name and [Jon Hoyt](http://twitter.com/jonmagic) for inspiration and brainstorming the idea.
  58
+Thanks to [Daniel Morrison](http://twitter.com/danielmorrison/status/58883772040486912) for the name and [Jon Hoyt](http://twitter.com/jonmagic) for inspiration and brainstorming the idea.
20  spec/javascripts/rosie.spec.js
@@ -12,7 +12,9 @@ describe('Factory', function() {
12 12
       };
13 13
 
14 14
       beforeEach(function() {
15  
-        Factory.define('thing', Thing).attr('name', 'Thing 1');
  15
+        Factory.define('thing', Thing).attr('name', 'Thing 1').after(function(obj) {
  16
+          obj.afterCalled = true;
  17
+        });
16 18
       });
17 19
 
18 20
       it('should return a new instance of that constructor', function() {
@@ -21,7 +23,11 @@ describe('Factory', function() {
21 23
       });
22 24
 
23 25
       it('should set attributes', function() {
24  
-        expect(Factory.build('thing')).toEqual({name: 'Thing 1'});
  26
+        expect(Factory.build('thing')).toEqual({name: 'Thing 1', afterCalled: true});
  27
+      });
  28
+
  29
+      it('should run callbacks', function() {
  30
+          expect(Factory.build('thing').afterCalled).toBe(true);
25 31
       });
26 32
     });
27 33
 
@@ -42,12 +48,18 @@ describe('Factory', function() {
42 48
 
43 49
   describe('extend', function() {
44 50
     beforeEach(function() {
45  
-      Factory.define('thing').attr('name', 'Thing 1');
  51
+      Factory.define('thing').attr('name', 'Thing 1').after(function(obj) {
  52
+        obj.afterCalled = true;
  53
+      });
46 54
       Factory.define('anotherThing').extend('thing').attr('title', 'Title 1');
47 55
     });
48 56
 
49 57
     it('should extend attributes', function() {
50  
-      expect(Factory.build('anotherThing')).toEqual({name:'Thing 1', title:'Title 1'});
  58
+      expect(Factory.build('anotherThing')).toEqual({name:'Thing 1', title:'Title 1', afterCalled: true});
  59
+    });
  60
+
  61
+    it('should extend callbacks', function() {
  62
+      expect(Factory.build('anotherThing').afterCalled).toBe(true);
51 63
     });
52 64
   });
53 65
 
18  src/rosie.js
@@ -2,6 +2,7 @@ var Factory = function(constructor) {
2 2
   this.construct = constructor;
3 3
   this.attrs = {};
4 4
   this.sequences = {};
  5
+  this.callbacks = [];
5 6
 };
6 7
 
7 8
 Factory.prototype = {
@@ -21,6 +22,11 @@ Factory.prototype = {
21 22
     return this;
22 23
   },
23 24
 
  25
+  after: function(callback) {
  26
+    this.callbacks.push(callback);
  27
+    return this;
  28
+  },
  29
+
24 30
   attributes: function(attrs) {
25 31
     attrs = attrs || {};
26 32
     for(var attr in this.attrs) {
@@ -43,6 +49,10 @@ Factory.prototype = {
43 49
         this.attrs[attr] = factory.attrs[attr];
44 50
       }
45 51
     }
  52
+    // Copy the parent's callbacks
  53
+    for(var i = 0; i < factory.callbacks.length; i++) {
  54
+        this.callbacks.push(factory.callbacks[i]);
  55
+    }
46 56
     return this;
47 57
   }
48 58
 };
@@ -55,8 +65,12 @@ Factory.define = function(name, constructor) {
55 65
   return factory;
56 66
 };
57 67
 
58  
-Factory.build = function(name, attrs) {
59  
-  return this.factories[name].build(attrs);
  68
+Factory.build = function(name, attrs, options) {
  69
+  var obj = this.factories[name].build(attrs);
  70
+  for(var i = 0; i < this.factories[name].callbacks.length; i++) {
  71
+      this.factories[name].callbacks[i](obj, options);
  72
+  }
  73
+  return obj;
60 74
 };
61 75
 
62 76
 Factory.attributes = function(name, attrs) {
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.