spur-common
Version:
A Node.JS library of common modules used as a base to most Node.JS applications.
49 lines (35 loc) • 1.27 kB
text/coffeescript
module.exports = (_, console, consoleColors)->
class BaseDelegate
supportsMethods:()->
for methodName in
do(methodName) =>
@[methodName] = (args...)=>
(methodName, args)
()
callDelegate:(methodName, args)->
_.each , (delegate)=>
if _.isFunction(delegate[methodName])
delegate[methodName].apply(delegate, args)
else if _.isFunction(delegate)
delegate.call(@, methodName, args)
useNoop:()->
= []
useConsole:()->
= []
consoleDelegate:(methodName, args)=>
prefix = (methodName)
console.log.apply(console, [prefix].concat(args))
getColoredLabel: (methodName)->
label = "#{@constructor.name}##{methodName}: "
if _.contains(["fatal", "error"], methodName)
color = "red"
else if _.contains(["warn"], methodName)
color = "yellow"
consoleColors[color or "cyan"](label)
useRecorder:()->
= []
recorderDelegate:(methodName, args)=>
?= {}
([methodName] ?= []).push(args)
use:(delegate)=>
= [delegate]