Кристиян обнови решението на 04.11.2013 23:56 (преди около 11 години)
+class Task
+ 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)
+ end
+end
+
+class TodoList
+ include Enumerable
+
+ attr_accessor :tasks
+
+ def initialize(tasks)
+ @tasks = tasks
+ end
+
+ def each(&block)
+ @tasks.each(&block)
+ end
+
+ def self.parse(text)
+ tasks = text.lines.each.map do |line|
+ Task.new *line.split('|').map(&:strip)
+ end
+ new tasks
+ end
+
+ def filter(criteria)
+ TodoList.new @tasks.select { |task| criteria.applied_to? task }
+ end
+
+ def adjoin(other)
+ TodoList.new (@tasks + other.tasks).uniq
+ end
+
+ def tasks_todo
+ map(&:status).select { |status| status == :todo }.length
+ end
+
+ def tasks_in_progress
+ map(&:status).select { |status| status == :current }.length
+ end
+
+ def tasks_completed
+ map(&:status).select { |status| status == :done }.length
+ end
+
+ def completed?
+ map(&:status).select { |status| status != :todo }.length == 0
+ end
+end
+
+class Criteria
+ def initialize(&block)
+ @criteria = block
+ end
+
+ class << self
+ def status(status)
+ Criteria.new { |task| task.status == status }
+ end
+
+ def priority(priority)
+ Criteria.new { |task| task.priority == priority }
+ end
+
+ def tags(tags)
+ Criteria.new { |task| match?(tags, task.tags) }
+ end
+ end
+
+ def applied_to?(task)
+ @criteria.call task
+ end
+
+ def &(other)
+ Criteria.new { |task| applied_to?(task) and other.applied_to?(task) }
+ end
+
+ def |(other)
+ Criteria.new { |task| applied_to?(task) or other.applied_to?(task) }
+ end
+
+ def !
+ Criteria.new { |task| not applied_to?(task) }
+ end
+end
+
+def match?(tags, task_tags)
+ if tags == []
+ task_tags == tags
+ else
+ (task_tags & tags).length == tags.length
+ end
+end