Решение на Втора задача от Христо Аврамов

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

Към профила на Христо Аврамов

Резултати

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

Код

class Task
attr_accessor :status
attr_accessor :description
attr_accessor :priority
attr_accessor :tags
def initialize(text)
array_with_all_date = text.split('|').map(&:strip)
status , description, priority, tags = array_with_all_date
self.status, self.description = status.downcase.to_sym, description
self.priority, self.tags = priority.downcase.to_sym, tags.to_s.split(',')
end
end
class Criteria
attr_accessor :status_field
attr_accessor :priority_field
attr_accessor :tags_field
def self.status(status)
newCriteria = Criteria.new
newCriteria.status_field = status
newCriteria
end
def self.priority(priority)
newCriteria = Criteria.new
newCriteria.priority_field = priority
newCriteria
end
def self.tags(tags)
newCriteria = Criteria.new
newCriteria.tags_field = tags
newCriteria
end
end
class TodoList
include Enumerable
attr_accessor :tasks
def initialize
@tasks = Array.new
end
def self.parse(text)
@tasks = Array.new
array_with_all_tasks = text.split("\n")
array_with_all_tasks.each {|task| @tasks.push(Task.new(task))}
self
end
def self.tasks_todo
@tasks.select{|task| task.status==:todo}.size
end
def self.tasks_in_progress
@tasks.select{|task| task.status==:current}.size
end
def self.tasks_completed
@tasks.select{|task| task.status==:done}.size
end
def self.completed?
self.tasks_completed == @tasks.size
end
def self.filter(criterias)
tags,newList = criterias.tags_field,TodoList.new
newList.tasks = @tasks.select do |task|
task.status == criterias.status_field or
task.priority == criterias.priority_field
end
end
def self.each
@tasks.each{|task| yield task}
end
end

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

..FFFFFFFFFF...FFFFFF.

Failures:

  1) TodoList filters tasks by tag
     Failure/Error: todo_list.filter(Criteria.tags %w[food]).map(&:description).should =~ ['Eat spaghetti.']
       expected collection contained:  ["Eat spaghetti."]
       actual collection contained:    []
       the missing elements were:      ["Eat spaghetti."]
     # /tmp/d20131107-4393-qd1vby/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)>'

  2) TodoList filters tasks by multiple tags
     Failure/Error: ]
       expected collection contained:  ["Do the 5th Ruby challenge.", "Grok Ruby."]
       actual collection contained:    []
       the missing elements were:      ["Do the 5th Ruby challenge.", "Grok Ruby."]
     # /tmp/d20131107-4393-qd1vby/spec.rb:43: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 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.']
       expected collection contained:  ["Do the 5th Ruby challenge."]
       actual collection contained:    []
       the missing elements were:      ["Do the 5th Ruby challenge."]
     # /tmp/d20131107-4393-qd1vby/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)>'

  4) TodoList supports a conjuction of filters
     Failure/Error: filtered = todo_list.filter Criteria.status(:todo) & Criteria.priority(:high)
     NoMethodError:
       undefined method `&' for #<Criteria:0xb9c0a724 @status_field=:todo>
     # /tmp/d20131107-4393-qd1vby/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)>'

  5) TodoList supports a disjunction of filters
     Failure/Error: filtered = todo_list.filter Criteria.status(:done) | Criteria.priority(:low)
     NoMethodError:
       undefined method `|' for #<Criteria:0xb9bff784 @status_field=:done>
     # /tmp/d20131107-4393-qd1vby/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)>'

  6) TodoList supports a negation of filters
     Failure/Error: filtered = todo_list.filter !Criteria.status(:todo)
     NoMethodError:
       undefined method `tags_field' for false:FalseClass
     # /tmp/d20131107-4393-qd1vby/solution.rb:71:in `filter'
     # /tmp/d20131107-4393-qd1vby/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)>'

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

  8) TodoList supports complex filters combination
     Failure/Error: Criteria.priority(:high) |
     NoMethodError:
       undefined method `&' for #<Criteria:0xb9aff884 @status_field=:todo>
     # /tmp/d20131107-4393-qd1vby/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)>'

  9) TodoList can be adjoined with another to-do list
     Failure/Error: adjoined    = development.adjoin food
     NoMethodError:
       undefined method `adjoin' for []:Array
     # /tmp/d20131107-4393-qd1vby/spec.rb:99: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 constructs an object for each task
     Failure/Error: task.status.should      eq :todo
     NoMethodError:
       undefined method `status' for nil:NilClass
     # /tmp/d20131107-4393-qd1vby/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)>'

  11) TodoList checks if all tasks are completed
     Failure/Error: todo_list.filter(Criteria.status :done).completed?.should eq true
     NoMethodError:
       undefined method `completed?' for #<Array:0xb9ae7fb8>
     # /tmp/d20131107-4393-qd1vby/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)>'

  12) TodoList doesn't modify the to-do list when filtering
     Failure/Error: todo_list.should have(text_input.lines.count).items
     NoMethodError:
       undefined method `items' for TodoList:Class
     # /tmp/d20131107-4393-qd1vby/spec.rb:137: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 implements Enumerable
     Failure/Error: todo_list.should be_an Enumerable
       expected TodoList to be a kind of Enumerable
     # /tmp/d20131107-4393-qd1vby/spec.rb:142: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 contains tasks with the neccessary interface
     Failure/Error: task = todo_list.first
     NoMethodError:
       undefined method `first' for TodoList:Class
     # /tmp/d20131107-4393-qd1vby/spec.rb:146: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 tasks have an array of tags
     Failure/Error: todo_list.first.tags.should be_an Array
     NoMethodError:
       undefined method `first' for TodoList:Class
     # /tmp/d20131107-4393-qd1vby/spec.rb:155: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)>'

  16) TodoList preserves the order of tasks
     Failure/Error: todo_list.map(&:description).should eq [
     NoMethodError:
       undefined method `map' for TodoList:Class
     # /tmp/d20131107-4393-qd1vby/spec.rb:159: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.03869 seconds
22 examples, 16 failures

Failed examples:

rspec /tmp/d20131107-4393-qd1vby/spec.rb:35 # TodoList filters tasks by tag
rspec /tmp/d20131107-4393-qd1vby/spec.rb:39 # TodoList filters tasks by multiple tags
rspec /tmp/d20131107-4393-qd1vby/spec.rb:46 # TodoList filtering by multiple tags matches only tasks with all the tags
rspec /tmp/d20131107-4393-qd1vby/spec.rb:50 # TodoList supports a conjuction of filters
rspec /tmp/d20131107-4393-qd1vby/spec.rb:55 # TodoList supports a disjunction of filters
rspec /tmp/d20131107-4393-qd1vby/spec.rb:65 # TodoList supports a negation of filters
rspec /tmp/d20131107-4393-qd1vby/spec.rb:75 # TodoList supports simple filters combination
rspec /tmp/d20131107-4393-qd1vby/spec.rb:80 # TodoList supports complex filters combination
rspec /tmp/d20131107-4393-qd1vby/spec.rb:96 # TodoList can be adjoined with another to-do list
rspec /tmp/d20131107-4393-qd1vby/spec.rb:109 # TodoList constructs an object for each task
rspec /tmp/d20131107-4393-qd1vby/spec.rb:130 # TodoList checks if all tasks are completed
rspec /tmp/d20131107-4393-qd1vby/spec.rb:135 # TodoList doesn't modify the to-do list when filtering
rspec /tmp/d20131107-4393-qd1vby/spec.rb:140 # TodoList implements Enumerable
rspec /tmp/d20131107-4393-qd1vby/spec.rb:145 # TodoList contains tasks with the neccessary interface
rspec /tmp/d20131107-4393-qd1vby/spec.rb:154 # TodoList tasks have an array of tags
rspec /tmp/d20131107-4393-qd1vby/spec.rb:158 # TodoList preserves the order of tasks

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

Христо обнови решението на 06.11.2013 01:29 (преди около 11 години)

+class Task
+ attr_accessor :status
+ attr_accessor :description
+ attr_accessor :priority
+ attr_accessor :tags
+
+ def initialize(text)
+ array_with_all_date = text.split('|').map(&:strip)
+ status , description, priority, tags = array_with_all_date
+
+ self.status, self.description = status.downcase.to_sym, description
+ self.priority, self.tags = priority.downcase.to_sym, tags.to_s.split(',')
+
+ end
+end
+
+class Criteria
+ attr_accessor :status_field
+ attr_accessor :priority_field
+ attr_accessor :tags_field
+ def self.status(status)
+ newCriteria = Criteria.new
+ newCriteria.status_field = status
+ newCriteria
+ end
+
+ def self.priority(priority)
+ newCriteria = Criteria.new
+ newCriteria.priority_field = priority
+ newCriteria
+ end
+
+ def self.tags(tags)
+ newCriteria = Criteria.new
+ newCriteria.tags_field = tags
+ newCriteria
+ end
+end
+
+class TodoList
+ include Enumerable
+ attr_accessor :tasks
+ def initialize
+ @tasks = Array.new
+ end
+
+ def self.parse(text)
+ @tasks = Array.new
+ array_with_all_tasks = text.split("\n")
+ array_with_all_tasks.each {|task| @tasks.push(Task.new(task))}
+ self
+ end
+
+ def self.tasks_todo
+ @tasks.select{|task| task.status==:todo}.size
+ end
+
+ def self.tasks_in_progress
+ @tasks.select{|task| task.status==:current}.size
+ end
+
+ def self.tasks_completed
+ @tasks.select{|task| task.status==:done}.size
+ end
+
+ def self.completed?
+ self.tasks_completed == @tasks.size
+ end
+
+ def self.filter(criterias)
+ tags,newList = criterias.tags_field,TodoList.new
+ newList.tasks = @tasks.select do |task|
+ task.status == criterias.status_field or
+ task.priority == criterias.priority_field
+ end
+ end
+
+ def self.each
+ @tasks.each{|task| yield task}
+ end
+
+end