Comment new methods in Basestar

This commit is contained in:
Andrew Stewart 2013-11-03 00:29:47 -07:00
parent 616bbbeee3
commit 1056b91c60
1 changed files with 43 additions and 11 deletions

View File

@ -8,23 +8,44 @@
'use strict'; 'use strict';
namespace = require 'node-namespace'
EventEmitter = require('events').EventEmitter
require './utils' require './utils'
# Basestar is the class used when writing external Cylon adaptors/drivers. namespace = require 'node-namespace'
EventEmitter = require('events').EventEmitter
# Basestar is a base class to be used when writing external Cylon adaptors and
# drivers. It provides some useful base methods and functionality
# #
# It provides some useful methods and behaviour. # It also extends EventEmitter, so child classes are capable of emitting events
# for other parts of the system to handle.
namespace 'Cylon', -> namespace 'Cylon', ->
class @Basestar extends EventEmitter class @Basestar extends EventEmitter
constructor: (opts) -> constructor: (opts) ->
@self = this @self = this
# proxies calls from all methods in klass to target # Public: Proxies calls from all methods in the class to a target class
#
# methods - array of methods to proxy
# target - class/object to proxy methods to
# klass - class to proxy methods from
# force - whether or not to overwrite existing method definitions
#
# Returns the klass where the methods have been proxied
proxyMethods: (methods, target, klass, force = false) -> proxyMethods: (methods, target, klass, force = false) ->
proxyFunctionsToObject(methods, target, klass, force) proxyFunctionsToObject(methods, target, klass, force)
# creates an event handler that proxies events from source object to target # Public: Defines an event handler that proxies events from a source object
# to a target object
#
# opts - object containing options:
# - targetEventName or eventName - event that should be emitted from the
# target
# - target - object to proxy event to
# - source - object to proxy event from
# - update - whether or not to send an 'update' event
#
# Returns the source
defineEvent: (opts) -> defineEvent: (opts) ->
targetEventName = opts.targetEventName or opts.eventName targetEventName = opts.targetEventName or opts.eventName
sendUpdate = opts.sendUpdate or false sendUpdate = opts.sendUpdate or false
@ -32,17 +53,28 @@ namespace 'Cylon', ->
opts.target.emit(targetEventName, args) opts.target.emit(targetEventName, args)
opts.target.emit('update', targetEventName, args) if sendUpdate opts.target.emit('update', targetEventName, args) if sendUpdate
# creates an event handler that proxies events from an adaptor object's 'connector' source
# (object reference to whatever module is actually talking to the hardware)
# to the adaptor's associated connection # Public: Creates an event handler that proxies events from an adaptor's
# 'connector' (reference to whatever module is actually talking to the hw)
# to the adaptor's associated connection.
#
# opts - hash of opts to be passed to defineEvent()
#
# Returns @connector
defineAdaptorEvent: (opts) -> defineAdaptorEvent: (opts) ->
opts['source'] = @connector opts['source'] = @connector
opts['target'] = @connection opts['target'] = @connection
opts['sendUpdate'] ?= false opts['sendUpdate'] ?= false
@defineEvent(opts) @defineEvent(opts)
# creates an event handler that proxies events from a driver object's 'connection' # Public: Creates an event handler that proxies events from an device's
# to the driver's associated device # 'connector' (reference to whatever module is actually talking to the hw)
# to the device's associated connection.
#
# opts - hash of opts to be passed to defineEvent()
#
# Returns @connection
defineDriverEvent: (opts) -> defineDriverEvent: (opts) ->
opts['source'] = @connection opts['source'] = @connection
opts['target'] = @device opts['target'] = @device