-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Major refactoring. Creates a Descriptor class that wraps everything i…
…n a much more elegant way
- Loading branch information
Showing
7 changed files
with
86 additions
and
70 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
"use strict"; | ||
|
||
var _ = require( "lodash" ); | ||
var Attempt = require( "attempt-js" ); | ||
|
||
function assignToObject( data ) { | ||
/* jshint validthis:true */ | ||
_.assign( this.instance, data ); | ||
} | ||
|
||
var descriptorPrototypeFields = { | ||
request: function( definition ) { | ||
return function( args ) { | ||
var self = this; | ||
return Attempt.join( definition.request.apply( definition, args ) ).chain( function( data ) { | ||
self.import( data ); | ||
return self.instance; | ||
} ); | ||
}; | ||
}, | ||
export: function( definition ) { | ||
return function() { | ||
return _.pick( this.instance, definition.export ); | ||
}; | ||
}, | ||
update: function() { | ||
return assignToObject; | ||
}, | ||
import: function() { | ||
return assignToObject; | ||
} | ||
}; | ||
|
||
module.exports = function( Serializable, getDefinition ) { | ||
return function( name ) { | ||
function Instance() {} | ||
Instance.prototype = _.create( Serializable.prototype, { | ||
constructor: Instance | ||
} ); | ||
function Descriptor( instance ) { | ||
this.instance = instance || new Instance(); | ||
} | ||
Descriptor.prototype.name = Instance[ Serializable.marker ] = name; | ||
Descriptor.pending = Attempt.join( getDefinition( name ) ).success( function( definition ) { | ||
delete Descriptor.pending; | ||
_.assign( Instance.prototype, definition.proto ); | ||
_.transform( descriptorPrototypeFields, function( proto, handler, methodName ) { | ||
proto[ methodName ] = handler( definition ); | ||
}, Descriptor.prototype ); | ||
} ); | ||
return Descriptor; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters