Решение на Втора задача от Лилия Любенова

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

Към профила на Лилия Любенова

Резултати

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

Код

class Task
def initialize(task_as_array = [])
@status, @description, @priority, @tags = task_as_array
end
def status
@status.downcase.intern
end
def description
@description
end
def priority
@priority.downcase.intern
end
def tags
@tags.split(", ")
end
end
class TodoList
include Enumerable
attr_reader :tasks
def initialize(tasks_array)
@tasks = tasks_array
end
def self.parse(text)
TodoList.new text.lines.map {|line| Task.new(line.split("|").map(&:strip))}
end
def each
@tasks.each {|task| yield task}
end
def filter(criteria)
TodoList.new tasks.select {|task| criteria.call task}
end
def adjoin(other)
TodoList.new self.tasks + (other.tasks - self.tasks)
end
def tasks_todo
self.select {|task| task.status == :todo}.size
end
def tasks_in_progress
self.select {|task| task.status == :current}.size
end
def tasks_completed
self.select {|task| task.status == :done}.size
end
def completed?
self.all? {|task| task.status == :done}
end
end
class Criteria
class << self
def status(criterion)
-> (task) {task.status == criterion}
end
def priority(criterion)
-> (task) {task.priority == criterion}
end
def tags(criterion)
-> (task) {(criterion - task.tags).size == 0}
end
end
end

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

.....FFFFF............

Failures:

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

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

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

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

  5) TodoList supports complex filters combination
     Failure/Error: Criteria.priority(:high) |
     NoMethodError:
       undefined method `&' for #<Proc:0xb8ed0388>
     # /tmp/d20131107-4393-12qjnr5/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)>'

Finished in 0.03761 seconds
22 examples, 5 failures

Failed examples:

rspec /tmp/d20131107-4393-12qjnr5/spec.rb:50 # TodoList supports a conjuction of filters
rspec /tmp/d20131107-4393-12qjnr5/spec.rb:55 # TodoList supports a disjunction of filters
rspec /tmp/d20131107-4393-12qjnr5/spec.rb:65 # TodoList supports a negation of filters
rspec /tmp/d20131107-4393-12qjnr5/spec.rb:75 # TodoList supports simple filters combination
rspec /tmp/d20131107-4393-12qjnr5/spec.rb:80 # TodoList supports complex filters combination

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

Лилия обнови решението на 06.11.2013 14:59 (преди около 11 години)

+class Task
+
+ def initialize(task_as_array = [])
+ @status, @description, @priority, @tags = task_as_array
+ end
+
+ def status
+ @status.downcase.intern
+ end
+
+ def description
+ @description
+ end
+
+ def priority
+ @priority.downcase.intern
+ end
+
+ def tags
+ @tags.split(", ")
+ end
+
+end
+
+
+class TodoList
+ include Enumerable
+
+ attr_reader :tasks
+
+ def initialize(tasks_array)
+ @tasks = tasks_array
+ end
+
+ def self.parse(text)
+ TodoList.new text.lines.map {|line| Task.new(line.split("|").map(&:strip))}
+ end
+
+ def each
+ @tasks.each {|task| yield task}
+ end
+
+ def filter(criteria)
+ TodoList.new tasks.select {|task| criteria.call task}
+ end
+
+ def adjoin(other)
+ TodoList.new self.tasks + (other.tasks - self.tasks)
+ end
+
+ def tasks_todo
+ self.select {|task| task.status == :todo}.size
+ end
+
+ def tasks_in_progress
+ self.select {|task| task.status == :current}.size
+ end
+
+ def tasks_completed
+ self.select {|task| task.status == :done}.size
+ end
+
+ def completed?
+ self.all? {|task| task.status == :done}
+ end
+
+end
+
+
+class Criteria
+
+ class << self
+ def status(criterion)
+ -> (task) {task.status == criterion}
+ end
+
+ def priority(criterion)
+ -> (task) {task.priority == criterion}
+ end
+
+ def tags(criterion)
+ -> (task) {(criterion - task.tags).size == 0}
+ end
+ end
+
+end