Цани обнови решението на 06.11.2013 00:28 (преди около 11 години)
+class TodoListItem
+ attr_accessor :status, :description, :priority, :tags
+ def initialize(status, description, priority, tags = "")
+ @status = status.downcase.to_sym
+ @description = description
+ @priority = priority.downcase.to_sym
+ @tags = tags.split(",").map(&:strip).to_a
+ end
+end
+
+class TodoList
+ include Enumerable
+
+ attr_accessor :listitems
+
+ def initialize(listitems)
+ @listitems = listitems
+ end
+
+ def self.parse text
+ new text.lines.map{|line| TodoListItem.new *line.split("|").map(&:strip)}
+ end
+
+ def filter filter
+ TodoList.new @listitems.select { |item| filter.accepts? item }
+ end
+
+ def tasks_todo
+ #self.filter(Criteria.status :todo).count - new TodoList every time? no 10x
+ @listitems.select { |item| (Criteria.status :todo).accepts? item }.count
+ end
+
+ def tasks_in_progress
+ @listitems.select { |item| (Criteria.status :current).accepts? item }.count
+ end
+
+ def tasks_completed
+ @listitems.select { |item| (Criteria.status :done).accepts? item }.count
+ end
+
+ def each
+ listitems.each {|item| yield item}
+ end
+
+ def adjoin otherlist
+ TodoList.new @listitems | otherlist.listitems
+ end
+
+ def completed?
+ self.tasks_completed == @listitems.count
+ end
+end
+
+class Filter
+ attr_accessor :callback
+
+ def initialize(&callback)
+ @callback = callback
+ end
+
+ def accepts? item
+ callback.call item
+ end
+
+ def &(filter)
+ Filter.new { |item| self.accepts?(item) and filter.accepts?(item) }
+ end
+
+ def |(filter)
+ Filter.new { |item| self.accepts?(item) or filter.accepts?(item) }
+ end
+
+ def !
+ Filter.new { |item| not accepts?(item) }
+ end
+end
+
+class Criteria
+ def self.status status
+ Filter.new {|item| item.status == status}
+ end
+
+ def self.priority priority
+ Filter.new {|item| item.priority == priority}
+ end
+
+ def self.tags tags
+ Filter.new {|item| (tags - item.tags).size == 0}
+ end
+end