Skip to content

Commit

Permalink
Added a task list (TODOs) to Hubot
Browse files Browse the repository at this point in the history
hubot add task
hubot list tasks
hubot delete task <task number>
  • Loading branch information
crofty committed Nov 30, 2011
1 parent 62e96d5 commit 6bddc5c
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/scripts/tasks.coffee
@@ -0,0 +1,49 @@
# Allows tasks (TODOs) to be added to Hubot
#
# task add <task> - Add a task
# task list tasks - List the tasks
# task delete <task number> - Delete a task
#

class Tasks
constructor: (@robot) ->
@cache = []
@robot.brain.on 'loaded', =>
if @robot.brain.data.tasks
@cache = @robot.brain.data.tasks
nextTaskNum: ->
maxTaskNum = if @cache.length then Math.max.apply(Math,@cache.map (n) -> n.num) else 0
maxTaskNum++
maxTaskNum
add: (taskString) ->
task = {num: @nextTaskNum(), task: taskString}
@cache.push task
@robot.brain.data.tasks = @cache
task
all: -> @cache
deleteByNumber: (num) ->
index = @cache.map((n) -> n.num).indexOf(parseInt(num))
task = @cache.splice(index, 1)[0]
@robot.brain.data.tasks = @cache
task

module.exports = (robot) ->
tasks = new Tasks robot

robot.respond /(task add|add task) (.+?)$/i, (msg) ->
task = tasks.add msg.match[2]
msg.send "Task added: ##{task.num} - #{task.task}"

robot.respond /(task list|list tasks)/i, (msg) ->
if tasks.all().length > 0
response = ""
for task, num in tasks.all()
response += "##{task.num} - #{task.task}\n"
msg.send response
else
msg.send "There are no tasks"

robot.respond /(task delete|delete task) #?(\d+)/i, (msg) ->
taskNum = msg.match[2]
task = tasks.deleteByNumber taskNum
msg.send "Task deleted: ##{task.num} - #{task.task}"

0 comments on commit 6bddc5c

Please sign in to comment.