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

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

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

Резултати

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

Код

class Task
attr_accessor :status, :description, :priority, :tags
def initialize(status, description, priority, tags)
@status = status.downcase.to_sym
@description = description
@priority = priority.downcase.to_sym
@tags = tags.split(',').map(&:strip)
end
end
class TodoList
include Enumerable
attr_accessor :tasks
def initialize(tasks)
@tasks = tasks
end
def each(&block)
@tasks.each(&block)
end
def self.parse(text)
tasks = text.lines.each.map do |line|
Task.new *line.split('|').map(&:strip)
end
new tasks
end
def filter(criteria)
TodoList.new @tasks.select { |task| criteria.applied_to? task }
end
def adjoin(other)
TodoList.new (@tasks + other.tasks).uniq
end
def tasks_todo
map(&:status).select { |status| status == :todo }.length
end
def tasks_in_progress
map(&:status).select { |status| status == :current }.length
end
def tasks_completed
map(&:status).select { |status| status == :done }.length
end
def completed?
map(&:status).select { |status| status != :todo }.length == 0
end
end
class Criteria
def initialize(&block)
@criteria = block
end
class << self
def status(status)
Criteria.new { |task| task.status == status }
end
def priority(priority)
Criteria.new { |task| task.priority == priority }
end
def tags(tags)
Criteria.new { |task| match?(tags, task.tags) }
end
end
def applied_to?(task)
@criteria.call task
end
def &(other)
Criteria.new { |task| applied_to?(task) and other.applied_to?(task) }
end
def |(other)
Criteria.new { |task| applied_to?(task) or other.applied_to?(task) }
end
def !
Criteria.new { |task| not applied_to?(task) }
end
end
def match?(tags, task_tags)
if tags == []
task_tags == tags
else
(task_tags & tags).length == tags.length
end
end

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

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

Failures:

  1) TodoList checks if all tasks are completed
     Failure/Error: todo_list.filter(Criteria.status :done).completed?.should eq true
       
       expected: true
            got: false
       
       (compared using ==)
     # /tmp/d20131107-4393-14w0t84/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)>'

Finished in 0.0397 seconds
22 examples, 1 failure

Failed examples:

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

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

Кристиян обнови решението на 04.11.2013 23:56 (преди около 11 години)

+class Task
+ attr_accessor :status, :description, :priority, :tags
+
+ def initialize(status, description, priority, tags)
+ @status = status.downcase.to_sym
+ @description = description
+ @priority = priority.downcase.to_sym
+ @tags = tags.split(',').map(&:strip)
+ end
+end
+
+class TodoList
+ include Enumerable
+
+ attr_accessor :tasks
+
+ def initialize(tasks)
+ @tasks = tasks
+ end
+
+ def each(&block)
+ @tasks.each(&block)
+ end
+
+ def self.parse(text)
+ tasks = text.lines.each.map do |line|
+ Task.new *line.split('|').map(&:strip)
+ end
+ new tasks
+ end
+
+ def filter(criteria)
+ TodoList.new @tasks.select { |task| criteria.applied_to? task }
+ end
+
+ def adjoin(other)
+ TodoList.new (@tasks + other.tasks).uniq
+ end
+
+ def tasks_todo
+ map(&:status).select { |status| status == :todo }.length
+ end
+
+ def tasks_in_progress
+ map(&:status).select { |status| status == :current }.length
+ end
+
+ def tasks_completed
+ map(&:status).select { |status| status == :done }.length
+ end
+
+ def completed?
+ map(&:status).select { |status| status != :todo }.length == 0
+ end
+end
+
+class Criteria
+ def initialize(&block)
+ @criteria = block
+ end
+
+ class << self
+ def status(status)
+ Criteria.new { |task| task.status == status }
+ end
+
+ def priority(priority)
+ Criteria.new { |task| task.priority == priority }
+ end
+
+ def tags(tags)
+ Criteria.new { |task| match?(tags, task.tags) }
+ end
+ end
+
+ def applied_to?(task)
+ @criteria.call task
+ end
+
+ def &(other)
+ Criteria.new { |task| applied_to?(task) and other.applied_to?(task) }
+ end
+
+ def |(other)
+ Criteria.new { |task| applied_to?(task) or other.applied_to?(task) }
+ end
+
+ def !
+ Criteria.new { |task| not applied_to?(task) }
+ end
+end
+
+def match?(tags, task_tags)
+ if tags == []
+ task_tags == tags
+ else
+ (task_tags & tags).length == tags.length
+ end
+end