Решение на Втора задача от Мария Митева

Обратно към всички решения

Към профила на Мария Митева

Резултати

  • 6 точки от тестове
  • 0 бонус точки
  • 6 точки общо
  • 21 успешни тест(а)
  • 1 неуспешни тест(а)

Код

class TodoList
include Enumerable
attr_accessor :tasks
def each(&block)
@tasks.each(&block)
end
def self.parse(text)
lines = text.lines.map { |line| line.split('|') }
tasks = lines.map do |status, description, priority, tags|
Task.new status, description, priority, tags
end
new tasks
end
def initialize(tasks)
@tasks = tasks
end
def filter(criteria)
TodoList.new @tasks.select { |task| criteria.match? task }
end
def adjoin(other_todo_list)
TodoList.new (@tasks + other_todo_list.tasks).uniq
end
def tasks_todo
filter(Criteria.status :todo).tasks.count
end
def tasks_completed
filter(Criteria.status :done).tasks.count
end
def tasks_in_progress
filter(Criteria.status :current).tasks.count
end
def completed?
count_by_status(:done) == tasks.count
end
end
class Task
attr_reader :status, :description, :priority, :tags
def initialize(status, description, priority, tags)
@status = status.strip.downcase.to_sym
@description = description.strip
@priority = priority.strip.downcase.to_sym
@tags = tags.split(',').map(&:strip)
end
end
module Criteria
def self.status(status)
Criterion.new { |task| task.status == status }
end
def self.priority(priority)
Criterion.new { |task| task.priority == priority }
end
def self.tags(tags_list)
Criterion.new { |task| task.tags | tags_list == task.tags }
end
end
class Criterion
def initialize(&block)
@matcher = block
end
def match?(task)
@matcher.call task
end
def &(other)
Criterion.new { |task| self.match?(task) and other.match?(task) }
end
def |(other)
Criterion.new { |task| self.match?(task) or other.match?(task) }
end
def !
Criterion.new { |task| not match?(task) }
end
end

Лог от изпълнението

...............F......

Failures:

  1) TodoList checks if all tasks are completed
     Failure/Error: todo_list.completed?.should eq false
     NoMethodError:
       undefined method `count_by_status' for #<TodoList:0xb8689a6c>
     # /tmp/d20131107-4393-1u0lz8s/solution.rb:44:in `completed?'
     # /tmp/d20131107-4393-1u0lz8s/spec.rb:131:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.03972 seconds
22 examples, 1 failure

Failed examples:

rspec /tmp/d20131107-4393-1u0lz8s/spec.rb:130 # TodoList checks if all tasks are completed

История (1 версия и 0 коментара)

Мария обнови решението на 05.11.2013 23:33 (преди над 10 години)

+class TodoList
+ include Enumerable
+
+ attr_accessor :tasks
+
+ def each(&block)
+ @tasks.each(&block)
+ end
+
+ def self.parse(text)
+ lines = text.lines.map { |line| line.split('|') }
+ tasks = lines.map do |status, description, priority, tags|
+ Task.new status, description, priority, tags
+ end
+
+ new tasks
+ end
+
+ def initialize(tasks)
+ @tasks = tasks
+ end
+
+ def filter(criteria)
+ TodoList.new @tasks.select { |task| criteria.match? task }
+ end
+
+ def adjoin(other_todo_list)
+ TodoList.new (@tasks + other_todo_list.tasks).uniq
+ end
+
+ def tasks_todo
+ filter(Criteria.status :todo).tasks.count
+ end
+
+ def tasks_completed
+ filter(Criteria.status :done).tasks.count
+ end
+
+ def tasks_in_progress
+ filter(Criteria.status :current).tasks.count
+ end
+
+ def completed?
+ count_by_status(:done) == tasks.count
+ end
+end
+
+class Task
+ attr_reader :status, :description, :priority, :tags
+
+ def initialize(status, description, priority, tags)
+ @status = status.strip.downcase.to_sym
+ @description = description.strip
+ @priority = priority.strip.downcase.to_sym
+ @tags = tags.split(',').map(&:strip)
+ end
+
+end
+
+module Criteria
+ def self.status(status)
+ Criterion.new { |task| task.status == status }
+ end
+
+ def self.priority(priority)
+ Criterion.new { |task| task.priority == priority }
+ end
+
+ def self.tags(tags_list)
+ Criterion.new { |task| task.tags | tags_list == task.tags }
+ end
+end
+
+class Criterion
+ def initialize(&block)
+ @matcher = block
+ end
+
+ def match?(task)
+ @matcher.call task
+ end
+
+ def &(other)
+ Criterion.new { |task| self.match?(task) and other.match?(task) }
+ end
+
+ def |(other)
+ Criterion.new { |task| self.match?(task) or other.match?(task) }
+ end
+
+ def !
+ Criterion.new { |task| not match?(task) }
+ end
+end