Венцислав обнови решението на 06.11.2013 16:34 (преди около 11 години)
+class TodoList
+ include Enumerable
+ attr_accessor :tasks
+
+ def initialize(tasks)
+ @tasks = tasks
+ end
+
+ def self.parse(text)
+ TodoList.new(text.split("\n").map { |line| Task.new(line) })
+ end
+
+ def each(&block)
+ @tasks.each(&block)
+ end
+
+ def filter(criteria)
+ TodoList.new(self.tasks.select { |task| criteria.ifTask(task) })
+ end
+
+ def adjoin(other)
+ TodoList.new(self.tasks + other.tasks)
+ end
+
+ def tasks_todo
+ tasks.select { |task| task.status == :todo}.size
+ end
+
+ def tasks_in_progress
+ tasks.select { |task| task.status == :current}.size
+ end
+
+ def tasks_completed
+ tasks.select { |task| task.status == :done}.size
+ end
+end
+
+class Task
+ attr_accessor :status, :description, :priority, :tags, :info
+
+ def initialize(text)
+ @info = text.split('|').map(&:strip)
+ @status, @description = info[0].downcase.to_sym, @info[1]
+ @priority = @info[2].downcase.to_sym
+ @tags = @info[3] == nil ? [] : @info[3].split(',').map(&:strip)
+ end
+end
+
+class Criteria
+ attr_accessor :status, :priority, :tags
+
+ def ifTask(task)
+ status == task.status or
+ priority == task.priority or
+ tags & task.tags == tags
+ end
+
+ def initialize(st, pr, tag)
+ @status = st
+ @priority = pr
+ @tags = tag
+ end
+
+ class << self
+ def status(stat)
+ Criteria.new(stat, nil, nil)
+ end
+
+ def priority(prio)
+ Criteria.new(nil, prio, nil)
+ end
+
+ def tags(tag)
+ Criteria.new(nil, nil, tag)
+ end
+ end
+end