Решение на Втора задача от Венцислав Велков

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

Към профила на Венцислав Велков

Резултати

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

Код

class TodoList
include Enumerable
attr_accessor :tasks
def initialize(tasks)
@tasks = tasks
end
def self.parse(text)
TodoList.new(text.split("\n").map { |line| Task.new(line) })
end
def each(&block)
@tasks.each(&block)
end
def filter(criteria)
TodoList.new(self.tasks.select { |task| criteria.fulfill(task) })
end
def adjoin(other)
TodoList.new(self.tasks + other.tasks)
end
def tasks_todo
tasks.select { |task| task.status == :todo}.size
end
def tasks_in_progress
tasks.select { |task| task.status == :current}.size
end
def tasks_completed
tasks.select { |task| task.status == :done}.size
end
end
class Task
attr_accessor :status, :description, :prior, :tags
def initialize(text)
info = text.split('|').map(&:strip)
@status, @description = info[0].downcase.to_sym, info[1]
@prior = info[2].downcase.to_sym
@tags = info[3] == nil ? [] : info[3].split(',').map(&:strip)
end
end
class Criteria
attr_accessor :status, :prior, :tags
def fulfill(task)
status==task.status or prior==task.prior or tags&task.tags==tags
end
def !
CompositionCriteria.new([self], :'!')
end
def |(other)
CompositionCriteria.new([self, other], :|)
end
def &(other)
CompositionCriteria.new([self, other], :&)
end
def initialize(*args)
@status, @prior, @tags = args[0], args[1], args[2]
end
class << self
def status(status)
Criteria.new(status, nil, nil)
end
def priority(prior)
Criteria.new(nil, prior, nil)
end
def tags(tags)
Criteria.new(nil, nil, tags)
end
end
end
class CompositionCriteria < Criteria
attr_accessor :criterias, :operator
def fulfill(task)
case @operator
when :'!' then not @criterias[0].fulfill(task)
when :& then @criterias[0].fulfill(task) and @criterias[1].fulfill(task)
when :| then @criterias[0].fulfill(task) or @criterias[1].fulfill(task)
end
end
def initialize(*args)
@criterias, @operator = args[0], args[1]
end
end

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

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

Failures:

  1) TodoList constructs an object for each task
     Failure/Error: task.priority.should    eq :low
     NoMethodError:
       undefined method `priority' for #<Task:0xb99dec34>
     # /tmp/d20131107-4393-t654tb/spec.rb:114: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 checks if all tasks are completed
     Failure/Error: todo_list.completed?.should eq false
     NoMethodError:
       undefined method `completed?' for #<TodoList:0xb99cff04>
     # /tmp/d20131107-4393-t654tb/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)>'

  3) TodoList contains tasks with the neccessary interface
     Failure/Error: task.should respond_to :priority
       expected #<Task:0xb99b3fac @description="Eat spaghetti.", @status=:todo, @prior=:high, @tags=["food", "happiness"]> to respond to :priority
     # /tmp/d20131107-4393-t654tb/spec.rb:150: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.03237 seconds
22 examples, 3 failures

Failed examples:

rspec /tmp/d20131107-4393-t654tb/spec.rb:109 # TodoList constructs an object for each task
rspec /tmp/d20131107-4393-t654tb/spec.rb:130 # TodoList checks if all tasks are completed
rspec /tmp/d20131107-4393-t654tb/spec.rb:145 # TodoList contains tasks with the neccessary interface

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

Венцислав обнови решението на 06.11.2013 16:34 (преди около 11 години)

+class TodoList
+ include Enumerable
+ attr_accessor :tasks
+
+ def initialize(tasks)
+ @tasks = tasks
+ end
+
+ def self.parse(text)
+ TodoList.new(text.split("\n").map { |line| Task.new(line) })
+ end
+
+ def each(&block)
+ @tasks.each(&block)
+ end
+
+ def filter(criteria)
+ TodoList.new(self.tasks.select { |task| criteria.ifTask(task) })
+ end
+
+ def adjoin(other)
+ TodoList.new(self.tasks + other.tasks)
+ end
+
+ def tasks_todo
+ tasks.select { |task| task.status == :todo}.size
+ end
+
+ def tasks_in_progress
+ tasks.select { |task| task.status == :current}.size
+ end
+
+ def tasks_completed
+ tasks.select { |task| task.status == :done}.size
+ end
+end
+
+class Task
+ attr_accessor :status, :description, :priority, :tags, :info
+
+ def initialize(text)
+ @info = text.split('|').map(&:strip)
+ @status, @description = info[0].downcase.to_sym, @info[1]
+ @priority = @info[2].downcase.to_sym
+ @tags = @info[3] == nil ? [] : @info[3].split(',').map(&:strip)
+ end
+end
+
+class Criteria
+ attr_accessor :status, :priority, :tags
+
+ def ifTask(task)
+ status == task.status or
+ priority == task.priority or
+ tags & task.tags == tags
+ end
+
+ def initialize(st, pr, tag)
+ @status = st
+ @priority = pr
+ @tags = tag
+ end
+
+ class << self
+ def status(stat)
+ Criteria.new(stat, nil, nil)
+ end
+
+ def priority(prio)
+ Criteria.new(nil, prio, nil)
+ end
+
+ def tags(tag)
+ Criteria.new(nil, nil, tag)
+ end
+ end
+end

Венцислав обнови решението на 06.11.2013 17:28 (преди около 11 години)

class TodoList
include Enumerable
attr_accessor :tasks
def initialize(tasks)
@tasks = tasks
end
def self.parse(text)
TodoList.new(text.split("\n").map { |line| Task.new(line) })
end
def each(&block)
@tasks.each(&block)
end
def filter(criteria)
- TodoList.new(self.tasks.select { |task| criteria.ifTask(task) })
+ TodoList.new(self.tasks.select { |task| criteria.fulfill(task) })
end
def adjoin(other)
TodoList.new(self.tasks + other.tasks)
end
def tasks_todo
tasks.select { |task| task.status == :todo}.size
end
def tasks_in_progress
tasks.select { |task| task.status == :current}.size
end
def tasks_completed
tasks.select { |task| task.status == :done}.size
end
end
class Task
- attr_accessor :status, :description, :priority, :tags, :info
+ attr_accessor :status, :description, :prior, :tags
def initialize(text)
- @info = text.split('|').map(&:strip)
- @status, @description = info[0].downcase.to_sym, @info[1]
- @priority = @info[2].downcase.to_sym
- @tags = @info[3] == nil ? [] : @info[3].split(',').map(&:strip)
+ info = text.split('|').map(&:strip)
+ @status, @description = info[0].downcase.to_sym, info[1]
+ @prior = info[2].downcase.to_sym
+ @tags = info[3] == nil ? [] : info[3].split(',').map(&:strip)
end
end
class Criteria
- attr_accessor :status, :priority, :tags
+ attr_accessor :status, :prior, :tags
- def ifTask(task)
- status == task.status or
- priority == task.priority or
- tags & task.tags == tags
+ def fulfill(task)
+ status==task.status or prior==task.prior or tags&task.tags==tags
end
- def initialize(st, pr, tag)
- @status = st
- @priority = pr
- @tags = tag
+ def !
+ CompositionCriteria.new([self], :'!')
end
+ def |(other)
+ CompositionCriteria.new([self, other], :|)
+ end
+
+ def &(other)
+ CompositionCriteria.new([self, other], :&)
+ end
+
+ def initialize(*args)
+ @status, @prior, @tags = args[0], args[1], args[2]
+ end
+
class << self
- def status(stat)
- Criteria.new(stat, nil, nil)
+ def status(status)
+ Criteria.new(status, nil, nil)
end
- def priority(prio)
- Criteria.new(nil, prio, nil)
+ def priority(prior)
+ Criteria.new(nil, prior, nil)
end
- def tags(tag)
- Criteria.new(nil, nil, tag)
+ def tags(tags)
+ Criteria.new(nil, nil, tags)
end
+ end
+end
+
+class CompositionCriteria < Criteria
+ attr_accessor :criterias, :operator
+
+ def fulfill(task)
+ case @operator
+ when :'!' then not @criterias[0].fulfill(task)
+ when :& then @criterias[0].fulfill(task) and @criterias[1].fulfill(task)
+ when :| then @criterias[0].fulfill(task) or @criterias[1].fulfill(task)
+ end
+ end
+
+ def initialize(*args)
+ @criterias, @operator = args[0], args[1]
end
end