Решение на Втора задача от Цветан Коев

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

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

Резултати

  • 3 точки от тестове
  • 0 бонус точки
  • 3 точки общо
  • 11 успешни тест(а)
  • 11 неуспешни тест(а)

Код

module TodoListStats
def tasks_todo
@list.count { |x| x.status == :todo }
end
def tasks_in_progress
@list.count { |x| x.status == :current }
end
def tasks_completed
@list.count { |x| x.status == :done }
end
def completed?
tasks_todo + tasks_in_progress == 0
end
end
class Task
attr_reader :status, :description, :priority , :tags
def self.parse (text)
status, description, priority, tags = text.split(/\s*\|\s*/)
if tags == nil then tags = [] else tags = tags.split(/,\s/) end
Task.new(status, description, priority, tags)
end
def initialize (status, description, priority, tags)
@status = status.downcase.to_sym
@description = description
@priority = priority.downcase.to_sym
@tags = tags
end
def eql? (right)
status == right.status and descrition == right.descrition and
priority == right.priority and tags == right.tags
end
end
class Criteria
def initialize(lambda_function)
@criteria_function = lambda_function
end
def check?(task)
@criteria_function.call task
end
def self.status(status)
Criteria.new ->(task){ task.status == status }
end
def self.priority(priority)
Criteria.new ->(task){ task.priority == priority }
end
def self.tags(tags)
Criteria.new ->(task){ task.tags == (task.tags + tags).uniq }
end
end
class TodoList
include Enumerable
include TodoListStats
attr_reader :list
def each (&block)
@list.each(&block)
end
def self.parse (text)
list = text.split(/\n/).map { |x| Task.parse(x.chomp) }
TodoList.new(list)
end
def initialize (list)
@list = list
end
def filter(criterias)
list = @list.select { |task| criterias.check? task }
TodoList.new(list)
end
def adjoin (sublist)
new_list = self.list + sublist.list
TodoList.new (new_list.uniq)
end
end

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

F....FFFFF.FFFFF......

Failures:

  1) TodoList filters tasks by status
     Failure/Error: ]
       expected collection contained:  ["Do the 5th Ruby challenge.", "Have some tea."]
       actual collection contained:    []
       the missing elements were:      ["Do the 5th Ruby challenge.", "Have some tea."]
     # /tmp/d20131107-4393-k90xjb/spec.rb:22: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)>'

  2) TodoList supports a conjuction of filters
     Failure/Error: filtered = todo_list.filter Criteria.status(:todo) & Criteria.priority(:high)
     NoMethodError:
       undefined method `&' for #<Criteria:0xba41ac0c>
     # /tmp/d20131107-4393-k90xjb/spec.rb:51: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)>'

  3) TodoList supports a disjunction of filters
     Failure/Error: filtered = todo_list.filter Criteria.status(:done) | Criteria.priority(:low)
     NoMethodError:
       undefined method `|' for #<Criteria:0xba418100>
     # /tmp/d20131107-4393-k90xjb/spec.rb:56: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)>'

  4) TodoList supports a negation of filters
     Failure/Error: filtered = todo_list.filter !Criteria.status(:todo)
     NoMethodError:
       undefined method `check?' for false:FalseClass
     # /tmp/d20131107-4393-k90xjb/solution.rb:82:in `block in filter'
     # /tmp/d20131107-4393-k90xjb/solution.rb:82:in `select'
     # /tmp/d20131107-4393-k90xjb/solution.rb:82:in `filter'
     # /tmp/d20131107-4393-k90xjb/spec.rb:66: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)>'

  5) TodoList supports simple filters combination
     Failure/Error: filtered = todo_list.filter Criteria.priority(:high) & !Criteria.tags(['development'])
     NoMethodError:
       undefined method `&' for #<Criteria:0xba40dd54>
     # /tmp/d20131107-4393-k90xjb/spec.rb:76: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)>'

  6) TodoList supports complex filters combination
     Failure/Error: Criteria.priority(:high) |
     NoMethodError:
       undefined method `&' for #<Criteria:0xba47df00>
     # /tmp/d20131107-4393-k90xjb/spec.rb:82: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)>'

  7) TodoList constructs an object for each task
     Failure/Error: task.status.should      eq :todo
       
       expected: :todo
            got: :"      todo"
       
       (compared using ==)
       
       Diff:
       @@ -1,2 +1,2 @@
       -:todo
       +:"      todo"
     # /tmp/d20131107-4393-k90xjb/spec.rb:112: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)>'

  8) TodoList returns the number of the tasks todo
     Failure/Error: todo_list.tasks_todo.should eq 5
       
       expected: 5
            got: 0
       
       (compared using ==)
     # /tmp/d20131107-4393-k90xjb/spec.rb:119: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)>'

  9) TodoList returns the number of the tasks in progress
     Failure/Error: todo_list.tasks_in_progress.should eq 2
       
       expected: 2
            got: 0
       
       (compared using ==)
     # /tmp/d20131107-4393-k90xjb/spec.rb:123: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)>'

  10) TodoList returns the number of the completed tasks
     Failure/Error: todo_list.tasks_completed.should eq 2
       
       expected: 2
            got: 0
       
       (compared using ==)
     # /tmp/d20131107-4393-k90xjb/spec.rb:127: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)>'

  11) TodoList checks if all tasks are completed
     Failure/Error: todo_list.completed?.should eq false
       
       expected: false
            got: true
       
       (compared using ==)
     # /tmp/d20131107-4393-k90xjb/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.08943 seconds
22 examples, 11 failures

Failed examples:

rspec /tmp/d20131107-4393-k90xjb/spec.rb:18 # TodoList filters tasks by status
rspec /tmp/d20131107-4393-k90xjb/spec.rb:50 # TodoList supports a conjuction of filters
rspec /tmp/d20131107-4393-k90xjb/spec.rb:55 # TodoList supports a disjunction of filters
rspec /tmp/d20131107-4393-k90xjb/spec.rb:65 # TodoList supports a negation of filters
rspec /tmp/d20131107-4393-k90xjb/spec.rb:75 # TodoList supports simple filters combination
rspec /tmp/d20131107-4393-k90xjb/spec.rb:80 # TodoList supports complex filters combination
rspec /tmp/d20131107-4393-k90xjb/spec.rb:109 # TodoList constructs an object for each task
rspec /tmp/d20131107-4393-k90xjb/spec.rb:118 # TodoList returns the number of the tasks todo
rspec /tmp/d20131107-4393-k90xjb/spec.rb:122 # TodoList returns the number of the tasks in progress
rspec /tmp/d20131107-4393-k90xjb/spec.rb:126 # TodoList returns the number of the completed tasks
rspec /tmp/d20131107-4393-k90xjb/spec.rb:130 # TodoList checks if all tasks are completed

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

Цветан обнови решението на 06.11.2013 05:41 (преди около 11 години)

+module TodoListStats
+ def tasks_todo
+ @list.count { |x| x.status == :todo }
+ end
+
+ def tasks_in_progress
+ @list.count { |x| x.status == :current }
+ end
+
+ def tasks_completed
+ @list.count { |x| x.status == :done }
+ end
+
+ def completed?
+ tasks_todo + tasks_in_progress == 0
+ end
+end
+
+class Task
+ attr_reader :status, :description, :priority , :tags
+ def self.parse (text)
+ status, description, priority, tags = text.split(/\s*\|\s*/)
+ if tags == nil then tags = [] else tags = tags.split(/,\s/) end
+ Task.new(status, description, priority, tags)
+ end
+
+ def initialize (status, description, priority, tags)
+ @status = status.downcase.to_sym
+ @description = description
+ @priority = priority.downcase.to_sym
+ @tags = tags
+ end
+
+ def eql? (right)
+ status == right.status and descrition == right.descrition and
+ priority == right.priority and tags == right.tags
+ end
+end
+
+class Criteria
+ def initialize(lambda_function)
+ @criteria_function = lambda_function
+ end
+
+ def check?(task)
+ @criteria_function.call task
+ end
+
+ def self.status(status)
+ Criteria.new ->(task){ task.status == status }
+ end
+
+ def self.priority(priority)
+ Criteria.new ->(task){ task.priority == priority }
+ end
+
+ def self.tags(tags)
+ Criteria.new ->(task){ task.tags == (task.tags + tags).uniq }
+ end
+end
+
+class TodoList
+ include Enumerable
+ include TodoListStats
+
+ attr_reader :list
+
+ def each (&block)
+ @list.each(&block)
+ end
+
+ def self.parse (text)
+ list = text.split(/\n/).map { |x| Task.parse(x.chomp) }
+ TodoList.new(list)
+ end
+
+ def initialize (list)
+ @list = list
+ end
+
+ def filter(criterias)
+ list = @list.select { |task| criterias.check? task }
+ TodoList.new(list)
+ end
+
+ def adjoin (sublist)
+ new_list = self.list + sublist.list
+ TodoList.new (new_list.uniq)
+ end
+end