All robots have a name

This commit is contained in:
deadprogram 2013-10-20 13:29:41 -07:00
parent d050835620
commit 2019cd5b6c
2 changed files with 20 additions and 10 deletions

View File

@ -9,16 +9,23 @@
'use strict';
Connection = source("connection")
Device = source("device")
module.exports = class Robot
constructor: (opts) ->
opts ?= {}
@_connections = {}
@_devices = {}
@name = opts.name
@name = opts.name or @constructor.randomName()
@connections = initConnections(opts.connection or opts.connections or {})
@devices = initDevices(opts.device or opts.devices or {})
@work = opts.work or -> (console.log "No work yet")
start: ->
startConnections()
startDevices()
(@work)
initConnections = (connections) ->
console.log "Initializing connections..."
initConnection connection for connection in connections
@ -35,13 +42,11 @@ module.exports = class Robot
console.log "Initializing device '#{ device.name }'..."
@_devices[device.name] = new Device(device)
start: ->
startConnections()
startDevices()
(@work)
startConnections = ->
console.log "Starting connections..."
startDevices = ->
console.log "Starting devices..."
@randomName: ->
"Robot #{ Math.floor(Math.random() * 100000) }"

View File

@ -6,10 +6,15 @@ describe "basic tests", ->
testWork = ->
console.log "hi"
r = new Robot(name: "irobot", work: testWork)
robot = new Robot(name: "irobot", work: testWork)
it "robot should have a name", ->
r.name.should.be.equal 'irobot'
it "robot should have a name, if given", ->
robot.name.should.be.equal 'irobot'
it "robot should have a random name, if not given", ->
sinon.stub(Robot, 'randomName').returns('Electra')
r = new Robot
r.name.should.be.equal 'Electra'
it "robot should have work", ->
r.work.should.be.equal testWork
robot.work.should.be.equal testWork