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

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

Към профила на Наталия Пацовска

Резултати

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

Код

class TodoList
include Enumerable
require './criteria.rb'
attr_accessor :tasks
def initialize(tasks)
@tasks = tasks
end
def each(&block)
@tasks.each(&block)
end
def self.parse(text)
tasks = text.each_line.map do |line|
@status, @descr, @priority, *@tags = line.split(/['|'',']/).map(&:strip)
Task.new @status, @descr, @priority, @tags
end
new tasks
end
def filter(criteria)
TodoList.new @tasks.select { |task| criteria.evaluate_for task }
end
def adjoin(other)
TodoList.new (tasks + other.tasks).uniq
end
def tasks_todo
@tasks.count { |task| task.status == :todo }
end
def tasks_in_progress
@tasks.count { |task| task.status == :current }
end
def tasks_completed
@tasks.count { |task| task.status == :done }
end
def completed?
@tasks.all? { |task| task.status == :done }
end
end
class Task
attr_reader :status, :description, :priority, :tags
def status
@status.downcase.to_sym
end
def priority
@priority.downcase.to_sym
end
def initialize(status, description, priority, tags)
@status = status
@description = description
@priority = priority
@tags = tags
end
end
class Criteria
require "Set"
def initialize(filter)
@filter = filter
end
def evaluate_for(object)
@filter.call(object)
end
def self.status(value)
Criteria.new ->(x) { x.send(:status) == value }
end
def self.priority(value)
Criteria.new ->(x) { x.send(:priority) == value }
end
def self.tags(value)
Criteria.new ->(x) { Set.new(value).subset? Set.new x.send(:tags) }
end
def &(other)
Criteria.new ->(x){ self.evaluate_for(x) and other.evaluate_for(x) }
end
def |(other)
Criteria.new ->(x){ self.evaluate_for(x) or other.evaluate_for(x) }
end
def !
Criteria.new ->(x){ not self.evaluate_for(x) }
end
end

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

/tmp/d20131107-4393-1p193j9/solution.rb:3:in `require': cannot load such file -- ./criteria.rb (LoadError)
	from /tmp/d20131107-4393-1p193j9/solution.rb:3:in `<class:TodoList>'
	from /tmp/d20131107-4393-1p193j9/solution.rb:1:in `<top (required)>'
	from /data/rails/evans-2013/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.5/lib/rspec/core/configuration.rb:867:in `require'
	from /data/rails/evans-2013/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.5/lib/rspec/core/configuration.rb:867:in `block in setup_load_path_and_require'
	from /data/rails/evans-2013/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.5/lib/rspec/core/configuration.rb:867:in `each'
	from /data/rails/evans-2013/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.5/lib/rspec/core/configuration.rb:867:in `setup_load_path_and_require'
	from /data/rails/evans-2013/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.5/lib/rspec/core/configuration_options.rb:25:in `configure'
	from /data/rails/evans-2013/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.5/lib/rspec/core/command_line.rb:21:in `run'
	from /data/rails/evans-2013/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.5/lib/rspec/core/runner.rb:80:in `run'
	from /data/rails/evans-2013/shared/bundle/ruby/2.1.0/gems/rspec-core-2.14.5/lib/rspec/core/runner.rb:17:in `block in autorun'

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

Наталия обнови решението на 06.11.2013 16:14 (преди над 10 години)

+class TodoList
+ include Enumerable
+ require './criteria.rb'
+
+ attr_accessor :tasks
+
+ def initialize(tasks)
+ @tasks = tasks
+ end
+
+ def each(&block)
+ @tasks.each(&block)
+ end
+
+ def self.parse(text)
+ tasks = text.each_line.map do |line|
+ @status, @descr, @priority, *@tags = line.split(/['|'',']/).map(&:strip)
+ Task.new @status, @descr, @priority, @tags
+ end
+
+ new tasks
+ end
+
+ def filter(criteria)
+ TodoList.new @tasks.select { |task| criteria.evaluate_for task }
+ end
+
+ def adjoin(other)
+ TodoList.new (tasks + other.tasks).uniq
+ end
+
+ def tasks_todo
+ @tasks.count { |task| task.status == :todo }
+ end
+
+ def tasks_in_progress
+ @tasks.count { |task| task.status == :current }
+ end
+
+ def tasks_completed
+ @tasks.count { |task| task.status == :done }
+ end
+
+ def completed?
+ @tasks.all? { |task| task.status == :done }
+ end
+
+end
+
+class Task
+
+ attr_reader :status, :description, :priority, :tags
+
+ def status
+ @status.downcase.to_sym
+ end
+
+ def priority
+ @priority.downcase.to_sym
+ end
+
+ def initialize(status, description, priority, tags)
+ @status = status
+ @description = description
+ @priority = priority
+ @tags = tags
+ end
+
+end
+
+class Criteria
+
+ require "Set"
+
+ def initialize(filter)
+ @filter = filter
+ end
+
+ def evaluate_for(object)
+ @filter.call(object)
+ end
+
+ def self.status(value)
+ Criteria.new ->(x) { x.send(:status) == value }
+ end
+
+ def self.priority(value)
+ Criteria.new ->(x) { x.send(:priority) == value }
+ end
+
+ def self.tags(value)
+ Criteria.new ->(x) { Set.new(value).subset? Set.new x.send(:tags) }
+ end
+
+ def &(other)
+ Criteria.new ->(x){ self.evaluate_for(x) and other.evaluate_for(x) }
+ end
+
+ def |(other)
+ Criteria.new ->(x){ self.evaluate_for(x) or other.evaluate_for(x) }
+ end
+
+ def !
+ Criteria.new ->(x){ not self.evaluate_for(x) }
+ end
+
+end