From b94975186eb4d42158d3b062943ca313ffa7acae Mon Sep 17 00:00:00 2001 From: Costin-Giorgian PAPUC Date: Sat, 31 Oct 2015 16:55:15 +0200 Subject: [PATCH] Adding code/todo_list.rb --- code/todo_list.rb | 48 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 code/todo_list.rb diff --git a/code/todo_list.rb b/code/todo_list.rb new file mode 100644 index 0000000..5209ec9 --- /dev/null +++ b/code/todo_list.rb @@ -0,0 +1,48 @@ +class TodoItem + attr_accessor :description + def initialize(description, done=false) + @description = description + @done = done + end + def done? + @done + end + def done! + @done = true + end +end + +class TodoList + attr_reader :items, :color, :name + def initialize(name, opts = {}) + @name = name + @color = opts[:color] + @items = [] + end + def add(item) + if item.is_a? String + @items << TodoItem.new(item) + else + @items << item + end + end + + def items_pending + items.select {|e| not e.done?} + end + def items_done + items.select {|e| e.done?} + end + def find_by_description(description) + items.find {|e| e.description == description} + end + def set_as_done(description) + item = self.find_by_description(description) + if item + item.done! + else + false + end + end + +end \ No newline at end of file