Created
May 23, 2011 18:17
-
-
Save oayandosu/987207 to your computer and use it in GitHub Desktop.
Hive file.js in CoffeeScript
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
model = require "./model" | |
backbone = require "backbone" | |
fs = require "fs" | |
util = require "util" | |
fpath = require "path" | |
exports = module.exports = model.extend | |
initialize: (attributes) -> | |
path = @get('path') | |
if path | |
name = fpath.basename path | |
@set name: name if name | |
# if name | |
# @set name: name | |
if attributes.watch | |
fs.watchFile path, -> | |
@sync 'read', @ | |
@change() | |
@ | |
ext: -> | |
fpath.extname @get 'name' | |
absolute: -> | |
fpath.resolve @get 'path' | |
dir: -> | |
fpath.dirname @absolute() | |
sync: (method, model) -> | |
model.syncing = true | |
path = model.absolut() | |
switch method | |
when "create", "update" | |
hive.log "#{method}-ing file @ #{path}" | |
fs.writeFile path, model.get('data'), model.get('encoding'), (err) -> | |
hive.log "#{method}d file @ #{path}" | |
if err | |
model.error err | |
else | |
model.sucess data: model.get 'data', true | |
when "read" | |
hive.log "#{method}-ing file @ #{path}" | |
fs.readFile path, (err, data) -> | |
hive.log "#{method}d file @ #{path}" | |
if err | |
model.error err | |
else | |
model.sucess data: data | |
when "delete" | |
hive.log "#{method}-ing file @ #{path}" | |
fs.unlink path, (err) -> | |
hive.log "#{method}d file @ #{path}" | |
if err | |
model.error err | |
else | |
model.sucess data: null | |
paste: (destination) -> | |
@ready -> | |
name = @get 'name' | |
path = "#{destination.get 'path'} / #{name}" | |
if !path | |
@error 'Could not paste file to hive.Dir without a path' | |
i = fs.createReadStream @get 'path' | |
o = fs.createWriteStream path | |
util.pump i, o, -> | |
hive.log "wrote file @ #{ path }" | |
@ |
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
var backbone, exports, fpath, fs, model, util; | |
model = require("./model"); | |
backbone = require("backbone"); | |
fs = require("fs"); | |
util = require("util"); | |
fpath = require("path"); | |
exports = module.exports = model.extend({ | |
initialize: function(attributes) { | |
var name, path; | |
path = this.get('path'); | |
if (path) { | |
name = fpath.basename(path); | |
this.set({ | |
name: name ? name : void 0 | |
}); | |
if (attributes.watch) { | |
fs.watchFile(path, function() { | |
this.sync('read', this); | |
return this.change(); | |
}); | |
} | |
} | |
return this; | |
}, | |
ext: function() { | |
return fpath.extname(this.get('name')); | |
}, | |
absolute: function() { | |
return fpath.resolve(this.get('path')); | |
}, | |
dir: function() { | |
return fpath.dirname(this.absolute()); | |
}, | |
sync: function(method, model) { | |
var path; | |
model.syncing = true; | |
path = model.absolut(); | |
switch (method) { | |
case "create": | |
case "update": | |
hive.log("" + method + "-ing file @ " + path); | |
return fs.writeFile(path, model.get('data'), model.get('encoding'), function(err) { | |
hive.log("" + method + "d file @ " + path); | |
if (err) { | |
return model.error(err); | |
} else { | |
return model.sucess({ | |
data: model.get('data') | |
}, true); | |
} | |
}); | |
case "read": | |
hive.log("" + method + "-ing file @ " + path); | |
return fs.readFile(path, function(err, data) { | |
hive.log("" + method + "d file @ " + path); | |
if (err) { | |
return model.error(err); | |
} else { | |
return model.sucess({ | |
data: data | |
}); | |
} | |
}); | |
case "delete": | |
hive.log("" + method + "-ing file @ " + path); | |
return fs.unlink(path, function(err) { | |
hive.log("" + method + "d file @ " + path); | |
if (err) { | |
return model.error(err); | |
} else { | |
return model.sucess({ | |
data: null | |
}); | |
} | |
}); | |
} | |
}, | |
paste: function(destination) { | |
return this.ready(function() { | |
var i, name, o, path; | |
name = this.get('name'); | |
path = "" + (destination.get('path')) + " / " + name; | |
if (!path) { | |
this.error('Could not paste file to hive.Dir without a path'); | |
} | |
i = fs.createReadStream(this.get('path')); | |
o = fs.createWriteStream(path); | |
util.pump(i, o, function() { | |
return hive.log("wrote file @ " + path); | |
}); | |
return this; | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment