From 6bddc5c36f7cdaa65175fc4580f2e0da58a32285 Mon Sep 17 00:00:00 2001 From: Crofty Date: Wed, 30 Nov 2011 14:34:34 +0000 Subject: [PATCH] Added a task list (TODOs) to Hubot hubot add task hubot list tasks hubot delete task --- src/scripts/tasks.coffee | 49 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/scripts/tasks.coffee diff --git a/src/scripts/tasks.coffee b/src/scripts/tasks.coffee new file mode 100644 index 000000000..9807a3b10 --- /dev/null +++ b/src/scripts/tasks.coffee @@ -0,0 +1,49 @@ +# Allows tasks (TODOs) to be added to Hubot +# +# task add - Add a task +# task list tasks - List the tasks +# task delete - 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}"