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

View File

@ -6,10 +6,15 @@ describe "basic tests", ->
testWork = -> testWork = ->
console.log "hi" console.log "hi"
r = new Robot(name: "irobot", work: testWork) robot = new Robot(name: "irobot", work: testWork)
it "robot should have a name", -> it "robot should have a name, if given", ->
r.name.should.be.equal 'irobot' 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", -> it "robot should have work", ->
r.work.should.be.equal testWork robot.work.should.be.equal testWork