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

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

Към профила на Веселин Генадиев

Резултати

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

Код

class Todo
attr_reader :status
attr_reader :description
attr_reader :priority
attr_reader :tags
def initialize arguments
@status = arguments[0].strip.downcase.to_sym
@description = arguments[1].strip
@priority = arguments[2].strip.downcase.to_sym
@tags = arguments[3].nil? ? %w[] : arguments[3].split(%r{\s*\,\s*})
end
def contains_all_tags? tag_list
tag_list.all?{|tag| @tags.include?(tag)}
end
def equals_status? status
@status == status
end
def equals_priority? priority
@priority == priority
end
end
class Criteria
attr_reader :criterias
def initialize criterias
@criterias = criterias
end
def self.status status
Criteria.new lambda{ |todo| todo.equals_status?(status) }
end
def self.priority priority
Criteria.new lambda { |todo| todo.equals_priority?(priority) }
end
def self.tags tag_list
Criteria.new lambda { |todo| todo.contains_all_tags?(tag_list) }
end
def &(other)
intnersect_criterias = lambda do |todo|
criterias.call(todo) && other.criterias.call(todo)
end
Criteria.new intnersect_criterias
end
def |(other)
union_criterias = lambda do |todo|
criterias.call(todo) || other.criterias.call(todo)
end
Criteria.new union_criterias
end
def !
Criteria.new lambda { |todo| !criterias.call(todo) }
end
end
class TodoList
include Enumerable
attr_reader :todo_list
def initialize(list)
@todo_list = list
end
def self.parse text
todo = TodoList.new []
text.each_line(separator=$/) do |line|
todo.todo_list << Todo.new(line.split(%r{\s*\|\s*}))
end
todo
end
def each(&block)
@todo_list.each(&block)
end
def filter(criteria)
TodoList.new select(&criteria.criterias)
end
def adjoin(other)
TodoList.new(@todo_list | other.todo_list)
end
def tasks_todo
@todo_list.count{|task| task.status == :todo}
end
def tasks_in_progress
@todo_list.count{|task| task.status == :current}
end
def tasks_completed
@todo_list.count{|task| task.status == :done}
end
def completed?
tasks_completed == self.length
end
end

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

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

Failures:

  1) 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-cnlzrp/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)>'

  2) TodoList constructs an object for each task
     Failure/Error: task.status.should      eq :todo
     NoMethodError:
       undefined method `status' for nil:NilClass
     # /tmp/d20131107-4393-cnlzrp/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)>'

  3) TodoList checks if all tasks are completed
     Failure/Error: todo_list.completed?.should eq false
     NoMethodError:
       undefined method `length' for #<TodoList:0xb9674c60>
     # /tmp/d20131107-4393-cnlzrp/solution.rb:106:in `completed?'
     # /tmp/d20131107-4393-cnlzrp/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)>'

Finished in 0.05324 seconds
22 examples, 3 failures

Failed examples:

rspec /tmp/d20131107-4393-cnlzrp/spec.rb:39 # TodoList filters tasks by multiple tags
rspec /tmp/d20131107-4393-cnlzrp/spec.rb:109 # TodoList constructs an object for each task
rspec /tmp/d20131107-4393-cnlzrp/spec.rb:130 # TodoList checks if all tasks are completed

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

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

+class Todo
+ attr_accessor :status
+ attr_accessor :description
+ attr_accessor :priority
+ attr_accessor :tags
+
+ def initialize arguments
+ @status = arguments[0].strip.downcase.to_sym
+ @description = arguments[1].strip
+ @priority = arguments[2].strip.downcase.to_sym
+ @tags = arguments[3].nil? ? %w[] : arguments[3].split(%r{\s*\,\s*})
+ end
+
+ def contains_all? tag_list
+ tag_list.all?{|tag| @tags.include?(tag)}
+ end
+
+ def equals_status? status
+ @status == status
+ end
+
+ def equals_priority? priority
+ @priority == priority
+ end
+end
+
+class Criteria
+ def self.status symbol
+ [:equals_status?, symbol]
+ end
+
+ def self.priority symbol
+ [:equals_priority?, symbol]
+ end
+
+ def self.tags tag_list
+ [:contains_all?, tag_list]
+ end
+end
+
+class TodoList < Array
+ def initialize(list)
+ super(list)
+ end
+
+ def self.parse text
+ list = TodoList.new []
+ text.each_line(separator=$/)do |line|
+ list << Todo.new(line.split(%r{\s*\|\s*}))
+ end
+ list
+ end
+
+ def filter(criteria=[])
+ hash = Hash[*criteria]
+ list = self.select{ |m| criteria_covered(m, hash) }
+ end
+
+ def criteria_covered(task, hash)
+ hash.all?{ |c, v| task.send(c, v)}
+ end
+
+ def adjoin(other)
+ self | other
+ end
+
+ def tasks_todo
+ self.count{|task| (task.status == :todo)}
+ end
+
+ def tasks_in_progress
+ self.count{|task| task.status == :current}
+ end
+
+ def tasks_completed
+ self.count{|task| task.status == :done}
+ end
+
+ def completed?
+ tasks_completed == self.length
+ end
+end

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

class Todo
- attr_accessor :status
- attr_accessor :description
- attr_accessor :priority
- attr_accessor :tags
+ attr_reader :status
+ attr_reader :description
+ attr_reader :priority
+ attr_reader :tags
def initialize arguments
@status = arguments[0].strip.downcase.to_sym
@description = arguments[1].strip
@priority = arguments[2].strip.downcase.to_sym
@tags = arguments[3].nil? ? %w[] : arguments[3].split(%r{\s*\,\s*})
end
- def contains_all? tag_list
+ def contains_all_tags? tag_list
tag_list.all?{|tag| @tags.include?(tag)}
end
def equals_status? status
@status == status
end
def equals_priority? priority
@priority == priority
end
end
class Criteria
- def self.status symbol
- [:equals_status?, symbol]
+ attr_reader :criterias
+
+ def initialize criterias
+ @criterias = criterias
end
- def self.priority symbol
- [:equals_priority?, symbol]
+ def self.status status
+ Criteria.new lambda{ |todo| todo.equals_status?(status) }
end
+ def self.priority priority
+ Criteria.new lambda { |todo| todo.equals_priority?(priority) }
+ end
+
def self.tags tag_list
- [:contains_all?, tag_list]
+ Criteria.new lambda { |todo| todo.contains_all_tags?(tag_list) }
end
+
+ def &(other)
+ intnersect_criterias = lambda do |todo|
+ criterias.call(todo) && other.criterias.call(todo)
+ end
+ Criteria.new intnersect_criterias
+ end
+
+ def |(other)
+ union_criterias = lambda do |todo|
+ criterias.call(todo) || other.criterias.call(todo)
+ end
+ Criteria.new union_criterias
+ end
+
+ def !
+ Criteria.new lambda { |todo| !criterias.call(todo) }
+ end
end
-class TodoList < Array
+class TodoList
+ include Enumerable
+ attr_reader :todo_list
+
def initialize(list)
- super(list)
+ @todo_list = list
end
def self.parse text
- list = TodoList.new []
- text.each_line(separator=$/)do |line|
- list << Todo.new(line.split(%r{\s*\|\s*}))
+ todo = TodoList.new []
+ text.each_line(separator=$/) do |line|
+ todo.todo_list << Todo.new(line.split(%r{\s*\|\s*}))
end
- list
+ todo
end
- def filter(criteria=[])
- hash = Hash[*criteria]
- list = self.select{ |m| criteria_covered(m, hash) }
+ def each(&block)
+ @todo_list.each(&block)
end
- def criteria_covered(task, hash)
- hash.all?{ |c, v| task.send(c, v)}
+ def filter(criteria)
+ TodoList.new select(&criteria.criterias)
end
def adjoin(other)
- self | other
+ TodoList.new(@todo_list | other.todo_list)
end
def tasks_todo
- self.count{|task| (task.status == :todo)}
+ @todo_list.count{|task| task.status == :todo}
end
def tasks_in_progress
- self.count{|task| task.status == :current}
+ @todo_list.count{|task| task.status == :current}
end
def tasks_completed
- self.count{|task| task.status == :done}
+ @todo_list.count{|task| task.status == :done}
end
def completed?
tasks_completed == self.length
end
end