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

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

Към профила на Владимир Конушлиев

Резултати

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

Код

require 'forwardable'
class TodoList
include Enumerable
extend Forwardable
attr_accessor :tasks
def_delegators :@tasks, :[], :size, :each
def self.parse(input_text)
TodoList.new(input_text.split(/\n/).map do |line|
status, description, priority, tags = line.split('|').map(&:strip)
Task.new(status, description, priority, tags)
end)
end
def initialize(tasks = [])
self.tasks = tasks
end
def filter(criteria)
TodoList.new tasks.select{ |task| criteria.call task }
end
def adjoin(other)
TodoList.new(self.tasks | other.tasks)
end
def tasks_todo
filter(Criteria.status(:todo)).size
end
def tasks_in_progress
filter(Criteria.status(:current)).size
end
def tasks_completed
filter(Criteria.status(:done)).size
end
end
class Criteria
extend Forwardable
attr_accessor :c_lambda
def_delegator :@c_lambda, :call
class << self
def status(status_value)
Criteria.new -> (task) { task.status == status_value }
end
def priority(priority_value)
Criteria.new -> (task) { task.priority == priority_value }
end
def tags(tag_values)
Criteria.new -> (task) { (tag_values.map(&:to_sym) - task.tags).empty? }
end
end
def initialize(c_lambda)
self.c_lambda = c_lambda
end
def &(other)
Criteria.new -> (task) { call(task) && other.call(task) }
end
def |(other)
Criteria.new -> (task) { call(task) | other.call(task) }
end
def !
Criteria.new -> (task) { !call(task) }
end
end
class Task
attr_accessor :status, :description, :priority, :tags
def initialize(status, description, priority, tags)
self.status = status.downcase.to_sym
self.description = description
self.priority = priority.downcase.to_sym
self.tags = tags ? tags.split(',').map(&:strip).map(&:to_sym) : []
end
end

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

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

Failures:

  1) TodoList constructs an object for each task
     Failure/Error: task.tags.should        include('health')
       expected [:health] to include "health"
       Diff:
       @@ -1,2 +1,2 @@
       -["health"]
       +[:health]
     # /tmp/d20131107-4393-1wayb0m/spec.rb:115: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:0xb892f6a4>
     # /tmp/d20131107-4393-1wayb0m/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.05532 seconds
22 examples, 2 failures

Failed examples:

rspec /tmp/d20131107-4393-1wayb0m/spec.rb:109 # TodoList constructs an object for each task
rspec /tmp/d20131107-4393-1wayb0m/spec.rb:130 # TodoList checks if all tasks are completed

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

Владимир обнови решението на 06.11.2013 13:20 (преди над 10 години)

+require 'forwardable'
+
+class TodoList
+ include Enumerable
+ extend Forwardable
+
+ attr_accessor :tasks
+
+ def_delegators :@tasks, :[], :size, :each
+
+ def self.parse(input_text)
+ TodoList.new(input_text.split(/\n/).map do |line|
+ status, description, priority, tags = line.split('|').map(&:strip)
+
+ Task.new(status, description, priority, tags)
+ end)
+ end
+
+ def initialize(tasks = [])
+ self.tasks = tasks
+ end
+
+ def filter(criteria)
+ TodoList.new tasks.select{ |task| criteria.call task }
+ end
+
+ def adjoin(other)
+ TodoList.new(self.tasks | other.tasks)
+ end
+
+ def tasks_todo
+ filter(Criteria.status(:todo)).size
+ end
+
+ def tasks_in_progress
+ filter(Criteria.status(:current)).size
+ end
+
+ def tasks_completed
+ filter(Criteria.status(:done)).size
+ end
+
+end
+
+class Criteria
+ attr_accessor :c_lambda
+
+ class << self
+
+ def status(status_value)
+ Criteria.new -> (task) { task.status == status_value }
+ end
+
+ def priority(priority_value)
+ Criteria.new -> (task) { task.priority == priority_value }
+ end
+
+ def tags(tag_values)
+ Criteria.new -> (task) { (tag_values.map(&:to_sym) - task.tags).empty? }
+ end
+
+ end
+
+ def initialize(c_lambda)
+ self.c_lambda = c_lambda
+ end
+
+ def call(task)
+ c_lambda.call(task)
+ end
+
+ def &(other)
+ Criteria.new -> (task) { self.call(task) && other.call(task) }
+ end
+
+ def |(other)
+ Criteria.new -> (task) { self.call(task) | other.call(task) }
+ end
+
+ def !
+ Criteria.new -> (task) { !self.call(task) }
+ end
+
+end
+
+class Task
+ attr_accessor :status, :description, :priority, :tags
+
+ def initialize(status, description, priority, tags)
+ self.status = status.downcase.to_sym
+ self.description = description
+ self.priority = priority.downcase.to_sym
+ self.tags = tags ? tags.split(',').map(&:strip).map(&:to_sym) : []
+ end
+
+end

Владимир обнови решението на 06.11.2013 13:25 (преди над 10 години)

require 'forwardable'
class TodoList
include Enumerable
extend Forwardable
attr_accessor :tasks
def_delegators :@tasks, :[], :size, :each
def self.parse(input_text)
TodoList.new(input_text.split(/\n/).map do |line|
status, description, priority, tags = line.split('|').map(&:strip)
Task.new(status, description, priority, tags)
end)
end
def initialize(tasks = [])
self.tasks = tasks
end
def filter(criteria)
TodoList.new tasks.select{ |task| criteria.call task }
end
def adjoin(other)
TodoList.new(self.tasks | other.tasks)
end
def tasks_todo
filter(Criteria.status(:todo)).size
end
def tasks_in_progress
filter(Criteria.status(:current)).size
end
def tasks_completed
filter(Criteria.status(:done)).size
end
end
class Criteria
+ extend Forwardable
+
attr_accessor :c_lambda
+ def_delegator :@c_lambda, :call
+
class << self
def status(status_value)
Criteria.new -> (task) { task.status == status_value }
end
def priority(priority_value)
Criteria.new -> (task) { task.priority == priority_value }
end
def tags(tag_values)
Criteria.new -> (task) { (tag_values.map(&:to_sym) - task.tags).empty? }
end
end
def initialize(c_lambda)
self.c_lambda = c_lambda
end
- def call(task)
- c_lambda.call(task)
- end
-
def &(other)
- Criteria.new -> (task) { self.call(task) && other.call(task) }
+ Criteria.new -> (task) { call(task) && other.call(task) }
end
def |(other)
- Criteria.new -> (task) { self.call(task) | other.call(task) }
+ Criteria.new -> (task) { call(task) | other.call(task) }
end
def !
- Criteria.new -> (task) { !self.call(task) }
+ Criteria.new -> (task) { !call(task) }
end
end
class Task
attr_accessor :status, :description, :priority, :tags
def initialize(status, description, priority, tags)
self.status = status.downcase.to_sym
self.description = description
self.priority = priority.downcase.to_sym
self.tags = tags ? tags.split(',').map(&:strip).map(&:to_sym) : []
end
end