Skip to content

Commit 545ceeb

Browse files
committed
Refactored blob.js
1 parent 138b755 commit 545ceeb

1 file changed

Lines changed: 56 additions & 24 deletions

File tree

lib/blob.js

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,65 @@
1-
var git = require( '../' );
1+
var git = require('../'),
2+
success = require('./utilities').success;
23

3-
var _Blob = function( obj ) {
4-
var self = {};
5-
6-
if( obj instanceof git.raw.Repo ) {
7-
self.repo = obj;
8-
self.blob = new git.raw.Blob( obj );
4+
/**
5+
* Blob convenience class constructor.
6+
*
7+
* @param {git.raw.Repo} rawRepo
8+
* @param {git.raw.Blob|null} rawBlob
9+
*/
10+
var Blob = function(rawRepo, rawBlob) {
11+
if(!(rawRepo instanceof git.raw.Repo)) {
12+
throw git.error('First parameter for Blob must be a raw repo');
913
}
10-
else if( obj instanceof git.raw.Blob ) {
11-
self.blob = obj;
14+
this.rawRepo = rawRepo;
15+
16+
if(rawBlob instanceof git.raw.Blob) {
17+
this.rawBlob = rawBlob;
18+
} else {
19+
this.rawBlob = new git.raw.Blob(this.rawRepo);
1220
}
21+
};
1322

14-
Object.defineProperty( self, 'raw', {
15-
get: function() {
16-
return self.blob.rawContent().toString();
17-
},
18-
enumerable: true
23+
/**
24+
* Retrieve the blob represented by the oid.
25+
*
26+
* @param {git.raw.Oid} oid
27+
* @param {Function} callback
28+
*/
29+
Blob.prototype.lookup = function(oid, callback) {
30+
var self = this;
31+
self.rawBlob.lookup(self.rawRepo, oid, function blobLookup(error, rawBlob) {
32+
if (success(error, callback)) {
33+
self.rawBlob = rawBlob;
34+
callback(null, self);
35+
}
1936
});
37+
};
2038

21-
self.lookup = function( oid ) {
22-
self.blob.lookup( self.repo, oid, function() {
23-
var args = Array.prototype.slice.call( arguments );
24-
args[0] = git.util().error( args[0] );
25-
26-
callback.apply( self, args.concat( self ) );
27-
});
28-
};
39+
/**
40+
* Retrieve the blob's raw content buffer.
41+
*
42+
* @param {Function} callback
43+
*/
44+
Blob.prototype.rawContent = function(callback) {
45+
this.rawBlob.rawContent(function(error, content) {
46+
if (success(error, callback)) {
47+
callback(null, content);
48+
}
49+
});
50+
};
2951

30-
return self;
52+
/**
53+
* Retrieve the blob's content.
54+
*
55+
* @param {Function} callback
56+
*/
57+
Blob.prototype.content = function(callback) {
58+
this.rawContent(function(error, content) {
59+
if (success(error, callback)) {
60+
callback(null, content.toString());
61+
}
62+
});
3163
};
3264

33-
exports.blob = _Blob;
65+
exports.blob = Blob;

0 commit comments

Comments
 (0)