Решение на Втора задача от Илиян Танев

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

Към профила на Илиян Танев

Резултати

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

Код

class Criteria
attr_accessor :proc
def initialize(proc)
@proc = proc
end
def self.status(status)
Criteria.new(Proc.new { |x| x.send(:status) == status })
end
def self.priority(priority)
Criteria.new(Proc.new { |x| x.send(:priority) == priority })
end
def self.tags(tags)
Criteria.new(Proc.new { |x| contains? x.send(:tags), tags })
end
def self.contains?(list, sublist)
(list - sublist).size == list.size - sublist.size
end
def &(other)
Criteria.new(Proc.new { |x| proc.call(x) && other.proc.call(x) })
end
def |(other)
Criteria.new(Proc.new { |x| proc.call(x) || other.proc.call(x) })
end
def !
Criteria.new(Proc.new { |x| !proc.call(x) })
end
end
class Task
attr_accessor :status
attr_accessor :description
attr_accessor :priority
attr_accessor :tags
def initialize(task_props)
@status = task_props[0].downcase.strip.to_sym
@description = task_props[1].strip
@priority = task_props[2].downcase.strip.to_sym
@tags = task_props[3].strip.split(",").map(&:strip)
end
end
class TodoList
include Enumerable
attr_accessor :tasks
def initialize(tasks)
@tasks = tasks
end
def self.parse(text)
@tasks = []
text.each_line { |line| @tasks << Task.new(line.split("|")) }
TodoList.new(@tasks)
end
def filter(criteria)
TodoList.new(@tasks.find_all { |task| criteria.proc.call(task) })
end
def adjoin(other)
TodoList.new(@tasks.concat other.tasks)
end
def tasks_todo
@tasks.find_all { |task| task.status == :todo }.size
end
def tasks_in_progress
@tasks.find_all { |task| task.status == :current }.size
end
def tasks_completed
@tasks.find_all { |task| task.status == :done }.size
end
def completed?
@taks.all? { |task| task.status == :done }
end
def each(&block)
@tasks.each(&block)
end
end

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

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

Failures:

  1) TodoList checks if all tasks are completed
     Failure/Error: todo_list.completed?.should eq false
     NoMethodError:
       undefined method `all?' for nil:NilClass
     # /tmp/d20131107-4393-1r7b5l9/solution.rb:88:in `completed?'
     # /tmp/d20131107-4393-1r7b5l9/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.22061 seconds
22 examples, 1 failure

Failed examples:

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

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

Илиян обнови решението на 03.11.2013 18:31 (преди над 10 години)

+class Criteria
+ attr_accessor :proc
+
+ def initialize(proc)
+ @proc = proc
+ end
+
+ def self.status(status)
+ Criteria.new(Proc.new { |x| x.send(:status) == status })
+ end
+
+ def self.priority(priority)
+ Criteria.new(Proc.new { |x| x.send(:priority) == priority })
+ end
+
+ def self.tags(tags)
+ Criteria.new(Proc.new { |x| contains? x.send(:tags), tags })
+ end
+
+ def self.contains?(list, sublist)
+ (list - sublist).size == list.size - sublist.size
+ end
+
+ def &(other)
+ Criteria.new(Proc.new { |x| proc.call(x) && other.proc.call(x) })
+ end
+
+ def |(other)
+ Criteria.new(Proc.new { |x| proc.call(x) || other.proc.call(x) })
+ end
+
+ def !
+ Criteria.new(Proc.new { |x| !proc.call(x) })
+ end
+end
+
+class Task
+ attr_accessor :status
+ attr_accessor :description
+ attr_accessor :priority
+ attr_accessor :tags
+
+ def initialize(task_props)
+ @status = task_props[0].downcase.strip.to_sym
+ @description = task_props[1].strip
+ @priority = task_props[2].downcase.strip.to_sym
+ @tags = task_props[3].strip.split(",").map(&:strip)
+ end
+end
+
+class TodoList
+ include Enumerable
+
+ attr_accessor :tasks
+
+ def initialize(tasks)
+ @tasks = tasks
+ end
+
+ def self.parse(text)
+ @tasks = []
+ text.each_line { |line| @tasks << Task.new(line.split("|")) }
+
+ TodoList.new(@tasks)
+ end
+
+ def filter(criteria)
+ TodoList.new(@tasks.find_all { |task| criteria.proc.call(task) })
+ end
+
+ def adjoin(other)
+ TodoList.new(@tasks.concat other.tasks)
+ end
+
+ def tasks_todo
+ @tasks.find_all { |task| task.status == :todo }.size
+ end
+
+ def tasks_in_progress
+ @tasks.find_all { |task| task.status == :current }.size
+ end
+
+ def tasks_completed
+ @tasks.find_all { |task| task.status == :done }.size
+ end
+
+ def completed?
+ @taks.all? { |task| task.status == :done }
+ end
+
+ def each(&block)
+ @tasks.each(&block)
+ end
+end