Решение на Втора задача от Давид Петров

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

Към профила на Давид Петров

Резултати

  • 2 точки от тестове
  • 0 бонус точки
  • 2 точки общо
  • 7 успешни тест(а)
  • 15 неуспешни тест(а)

Код

class TodoList
include Enumerable
def self.parse(text)
array=[]
text.each_line {|l| array.push( Task.new l.split('|') )}
Helper.new (array)
end
end
class Helper
include Enumerable
attr_accessor :tasks_list
def initialize(tasks_list)
@tasks_list=tasks_list
end
def each
@tasks_list.each { |t| yield t }
end
def completed?
@tasks_list.all? {|x| x.status==:done}
end
def tasks_todo
@tasks_list.count {|x| x.status==:todo}
end
def tasks_in_progress
@tasks_list.count {|x| x.status==:current}
end
def tasks_completed
@tasks_list.count {|x| x.status==:done}
end
end
class Task
attr_accessor :status, :description, :priority, :tags
def initialize(line)
@status=line[0].strip.downcase.to_sym
@description=line[1].strip
@priority=line[2].strip.to_sym
@tags = line[3].nil? ? [] : line[3].strip.split(', ')
end
end

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

FFFFFFFFFFFF...FF....F

Failures:

  1) TodoList filters tasks by status
     Failure/Error: todo_list.filter(Criteria.status :done).map(&:description).should =~ [
     NameError:
       uninitialized constant Criteria
     # /tmp/d20131107-4393-1c29ick/spec.rb:19: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 filters tasks by priority
     Failure/Error: todo_list.filter(Criteria.priority :high).map(&:description).should =~ [
     NameError:
       uninitialized constant Criteria
     # /tmp/d20131107-4393-1c29ick/spec.rb:26: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 filters tasks by tag
     Failure/Error: todo_list.filter(Criteria.tags %w[food]).map(&:description).should =~ ['Eat spaghetti.']
     NameError:
       uninitialized constant Criteria
     # /tmp/d20131107-4393-1c29ick/spec.rb:36: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 filters tasks by multiple tags
     Failure/Error: todo_list.filter(Criteria.tags %w[development ruby]).map(&:description).should =~ [
     NameError:
       uninitialized constant Criteria
     # /tmp/d20131107-4393-1c29ick/spec.rb:40: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 filtering by multiple tags matches only tasks with all the tags
     Failure/Error: todo_list.filter(Criteria.tags %w[development FMI]).map(&:description).should =~ ['Do the 5th Ruby challenge.']
     NameError:
       uninitialized constant Criteria
     # /tmp/d20131107-4393-1c29ick/spec.rb:47: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 a conjuction of filters
     Failure/Error: filtered = todo_list.filter Criteria.status(:todo) & Criteria.priority(:high)
     NameError:
       uninitialized constant Criteria
     # /tmp/d20131107-4393-1c29ick/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)>'

  7) TodoList supports a disjunction of filters
     Failure/Error: filtered = todo_list.filter Criteria.status(:done) | Criteria.priority(:low)
     NameError:
       uninitialized constant Criteria
     # /tmp/d20131107-4393-1c29ick/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)>'

  8) TodoList supports a negation of filters
     Failure/Error: filtered = todo_list.filter !Criteria.status(:todo)
     NameError:
       uninitialized constant Criteria
     # /tmp/d20131107-4393-1c29ick/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)>'

  9) TodoList supports simple filters combination
     Failure/Error: filtered = todo_list.filter Criteria.priority(:high) & !Criteria.tags(['development'])
     NameError:
       uninitialized constant Criteria
     # /tmp/d20131107-4393-1c29ick/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)>'

  10) TodoList supports complex filters combination
     Failure/Error: filtered = todo_list.filter Criteria.status(:todo) &
     NameError:
       uninitialized constant Criteria
     # /tmp/d20131107-4393-1c29ick/spec.rb:81: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 can be adjoined with another to-do list
     Failure/Error: development = todo_list.filter Criteria.tags(['development'])
     NameError:
       uninitialized constant Criteria
     # /tmp/d20131107-4393-1c29ick/spec.rb:97: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)>'

  12) TodoList constructs an object for each task
     Failure/Error: task = todo_list.filter(Criteria.tags ['health']).first
     NameError:
       uninitialized constant Criteria
     # /tmp/d20131107-4393-1c29ick/spec.rb:110: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)>'

  13) TodoList checks if all tasks are completed
     Failure/Error: todo_list.filter(Criteria.status :done).completed?.should eq true
     NameError:
       uninitialized constant Criteria
     # /tmp/d20131107-4393-1c29ick/spec.rb:132: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)>'

  14) TodoList doesn't modify the to-do list when filtering
     Failure/Error: todo_list.filter(Criteria.status :todo)
     NameError:
       uninitialized constant Criteria
     # /tmp/d20131107-4393-1c29ick/spec.rb:136: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)>'

  15) TodoList creates a new object on filter
     Failure/Error: todo_list.filter(Criteria.tags %w[wtf]).should_not equal todo_list
     NameError:
       uninitialized constant Criteria
     # /tmp/d20131107-4393-1c29ick/spec.rb:173: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.05245 seconds
22 examples, 15 failures

Failed examples:

rspec /tmp/d20131107-4393-1c29ick/spec.rb:18 # TodoList filters tasks by status
rspec /tmp/d20131107-4393-1c29ick/spec.rb:25 # TodoList filters tasks by priority
rspec /tmp/d20131107-4393-1c29ick/spec.rb:35 # TodoList filters tasks by tag
rspec /tmp/d20131107-4393-1c29ick/spec.rb:39 # TodoList filters tasks by multiple tags
rspec /tmp/d20131107-4393-1c29ick/spec.rb:46 # TodoList filtering by multiple tags matches only tasks with all the tags
rspec /tmp/d20131107-4393-1c29ick/spec.rb:50 # TodoList supports a conjuction of filters
rspec /tmp/d20131107-4393-1c29ick/spec.rb:55 # TodoList supports a disjunction of filters
rspec /tmp/d20131107-4393-1c29ick/spec.rb:65 # TodoList supports a negation of filters
rspec /tmp/d20131107-4393-1c29ick/spec.rb:75 # TodoList supports simple filters combination
rspec /tmp/d20131107-4393-1c29ick/spec.rb:80 # TodoList supports complex filters combination
rspec /tmp/d20131107-4393-1c29ick/spec.rb:96 # TodoList can be adjoined with another to-do list
rspec /tmp/d20131107-4393-1c29ick/spec.rb:109 # TodoList constructs an object for each task
rspec /tmp/d20131107-4393-1c29ick/spec.rb:130 # TodoList checks if all tasks are completed
rspec /tmp/d20131107-4393-1c29ick/spec.rb:135 # TodoList doesn't modify the to-do list when filtering
rspec /tmp/d20131107-4393-1c29ick/spec.rb:172 # TodoList creates a new object on filter

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

Давид обнови решението на 06.11.2013 17:22 (преди около 11 години)

+class TodoList
+ include Enumerable
+ def self.parse(text)
+ array=[]
+ text.each_line {|l| array.push( Task.new l.split('|') )}
+ Helper.new (array)
+ end
+end
+
+class Helper
+ include Enumerable
+ attr_accessor :tasks_list
+ def initialize(tasks_list)
+ @tasks_list=tasks_list
+ end
+ def each
+ @tasks_list.each { |t| yield t }
+ end
+ def completed?
+ @tasks_list.all? {|x| x.status==:done}
+ end
+
+ def tasks_todo
+ @tasks_list.count {|x| x.status==:todo}
+ end
+
+ def tasks_in_progress
+ @tasks_list.count {|x| x.status==:current}
+ end
+
+ def tasks_completed
+ @tasks_list.count {|x| x.status==:done}
+ end
+
+end
+
+class Task
+ attr_accessor :status, :description, :priority, :tags
+ def initialize(line)
+ @status=line[0].strip.downcase.to_sym
+ @description=line[1].strip
+ @priority=line[2].strip.to_sym
+ @tags = line[3].nil? ? [] : line[3].strip.split(', ')
+ end
+end