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

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

Към профила на Георги Кръстев

Резултати

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

Код

class Task < Struct.new :status, :description, :priority, :tags
def self.parse(entry)
status, description, priority, tags = entry.split('|').map(&:strip)
new status.downcase.to_sym, description, priority.downcase.to_sym,
tags ? tags.split(',').map(&:strip) : []
end
end
class Criteria
class << self
def status(acceptable)
new { |task| task.status == acceptable }
end
def priority(acceptable)
new { |task| task.priority == acceptable }
end
def tags(required)
new { |task| (required - task.tags).empty? }
end
end
def initialize(&predicate)
@predicate = predicate
end
def to_proc
@predicate
end
def !
Criteria.new { |task| not to_proc.call task }
end
def &(other)
Criteria.new { |task| to_proc.call task and other.to_proc.call task }
end
def |(other)
Criteria.new { |task| to_proc.call task or other.to_proc.call task }
end
end
class TodoList
include Enumerable
def self.parse(backlog)
new backlog.lines.map { |entry| Task.parse entry }
end
def initialize(tasks)
@tasks = tasks
end
def each(&block)
@tasks.each(&block)
end
def filter(criteria)
TodoList.new select(&criteria)
end
def adjoin(other)
TodoList.new to_a | other.to_a
end
def tasks_todo
count(&Criteria.status(:todo))
end
def tasks_in_progress
count(&Criteria.status(:current))
end
def tasks_completed
count(&Criteria.status(:done))
end
def completed?
all?(&Criteria.status(:done))
end
end

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

......................

Finished in 0.41815 seconds
22 examples, 0 failures

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

Георги обнови решението на 02.11.2013 01:09 (преди над 10 години)

+class Task < Struct.new :status, :description, :priority, :tags
+ def self.parse(string)
+ status, description, priority, tags = string.split('|').map(&:strip)
+ new status.downcase.to_sym, description, priority.downcase.to_sym,
+ tags ? tags.split(',').map(&:strip) : []
+ end
+end
+
+class Criteria
+ def initialize(&predicate)
+ @predicate = predicate
+ end
+
+ def self.status(acceptable)
+ new { |task| task.status == acceptable }
+ end
+
+ def self.priority(acceptable)
+ new { |task| task.priority == acceptable }
+ end
+
+ def self.tags(required)
+ new { |task| (required - task.tags).empty? }
+ end
+
+ def to_proc
+ @predicate
+ end
+
+ def !
+ Criteria.new { |task| not to_proc.call task }
+ end
+
+ def &(other)
+ Criteria.new { |task| to_proc.call task and other.to_proc.call task }
+ end
+
+ def |(other)
+ Criteria.new { |task| to_proc.call task or other.to_proc.call task }
+ end
+end
+
+class TodoList
+ include Enumerable
+
+ def initialize(tasks)
+ @tasks = tasks
+ end
+
+ def self.parse(string)
+ new string.split("\n").map { |line| Task.parse line }
+ end
+
+ def each(&block)
+ @tasks.each(&block)
+ end
+
+ def filter(criteria)
+ TodoList.new select(&criteria)
+ end
+
+ def adjoin(other)
+ TodoList.new to_a | other.to_a
+ end
+
+ def tasks_todo
+ count(&Criteria.status(:todo))
+ end
+
+ def tasks_in_progress
+ count(&Criteria.status(:current))
+ end
+
+ def tasks_completed
+ count(&Criteria.status(:done))
+ end
+end

Георги обнови решението на 03.11.2013 08:57 (преди над 10 години)

class Task < Struct.new :status, :description, :priority, :tags
def self.parse(string)
status, description, priority, tags = string.split('|').map(&:strip)
new status.downcase.to_sym, description, priority.downcase.to_sym,
tags ? tags.split(',').map(&:strip) : []
end
end
class Criteria
def initialize(&predicate)
@predicate = predicate
end
def self.status(acceptable)
new { |task| task.status == acceptable }
end
def self.priority(acceptable)
new { |task| task.priority == acceptable }
end
def self.tags(required)
new { |task| (required - task.tags).empty? }
end
def to_proc
@predicate
end
def !
Criteria.new { |task| not to_proc.call task }
end
def &(other)
Criteria.new { |task| to_proc.call task and other.to_proc.call task }
end
def |(other)
Criteria.new { |task| to_proc.call task or other.to_proc.call task }
end
end
class TodoList
include Enumerable
def initialize(tasks)
@tasks = tasks
end
def self.parse(string)
- new string.split("\n").map { |line| Task.parse line }
+ new string.lines.map { |line| Task.parse line }
end
def each(&block)
@tasks.each(&block)
end
def filter(criteria)
TodoList.new select(&criteria)
end
def adjoin(other)
TodoList.new to_a | other.to_a
end
def tasks_todo
count(&Criteria.status(:todo))
end
def tasks_in_progress
count(&Criteria.status(:current))
end
def tasks_completed
count(&Criteria.status(:done))
end
end

Георги обнови решението на 05.11.2013 19:55 (преди над 10 години)

class Task < Struct.new :status, :description, :priority, :tags
- def self.parse(string)
- status, description, priority, tags = string.split('|').map(&:strip)
+ def self.parse(entry)
+ status, description, priority, tags = entry.split('|').map(&:strip)
new status.downcase.to_sym, description, priority.downcase.to_sym,
tags ? tags.split(',').map(&:strip) : []
end
end
class Criteria
- def initialize(&predicate)
- @predicate = predicate
- end
+ class << self
+ def status(acceptable)
+ new { |task| task.status == acceptable }
+ end
- def self.status(acceptable)
- new { |task| task.status == acceptable }
- end
+ def priority(acceptable)
+ new { |task| task.priority == acceptable }
+ end
- def self.priority(acceptable)
- new { |task| task.priority == acceptable }
+ def tags(required)
+ new { |task| (required - task.tags).empty? }
+ end
end
- def self.tags(required)
- new { |task| (required - task.tags).empty? }
+ def initialize(&predicate)
+ @predicate = predicate
end
def to_proc
@predicate
end
def !
Criteria.new { |task| not to_proc.call task }
end
def &(other)
Criteria.new { |task| to_proc.call task and other.to_proc.call task }
end
def |(other)
Criteria.new { |task| to_proc.call task or other.to_proc.call task }
end
end
class TodoList
include Enumerable
+ def self.parse(backlog)
+ new backlog.lines.map { |entry| Task.parse entry }
+ end
+
def initialize(tasks)
@tasks = tasks
end
- def self.parse(string)
- new string.lines.map { |line| Task.parse line }
- end
-
def each(&block)
@tasks.each(&block)
end
def filter(criteria)
- TodoList.new select(&criteria)
+ TodoSubList.new select(&criteria)
end
- def adjoin(other)
- TodoList.new to_a | other.to_a
- end
-
def tasks_todo
count(&Criteria.status(:todo))
end
def tasks_in_progress
count(&Criteria.status(:current))
end
def tasks_completed
count(&Criteria.status(:done))
+ end
+
+ def completed?
+ all?(&Criteria.status(:done))
+ end
+end
+
+class TodoSubList < TodoList
+ def initialize(tasks)
+ super
+ end
+
+ def adjoin(other)
+ TodoSubList.new to_a | other.to_a
end
end

Георги обнови решението на 06.11.2013 00:46 (преди над 10 години)

class Task < Struct.new :status, :description, :priority, :tags
def self.parse(entry)
status, description, priority, tags = entry.split('|').map(&:strip)
new status.downcase.to_sym, description, priority.downcase.to_sym,
tags ? tags.split(',').map(&:strip) : []
end
end
class Criteria
class << self
def status(acceptable)
new { |task| task.status == acceptable }
end
def priority(acceptable)
new { |task| task.priority == acceptable }
end
def tags(required)
new { |task| (required - task.tags).empty? }
end
end
def initialize(&predicate)
@predicate = predicate
end
def to_proc
@predicate
end
def !
Criteria.new { |task| not to_proc.call task }
end
def &(other)
Criteria.new { |task| to_proc.call task and other.to_proc.call task }
end
def |(other)
Criteria.new { |task| to_proc.call task or other.to_proc.call task }
end
end
-class TodoList
+module Schedule
include Enumerable
- def self.parse(backlog)
- new backlog.lines.map { |entry| Task.parse entry }
- end
-
- def initialize(tasks)
- @tasks = tasks
- end
-
- def each(&block)
- @tasks.each(&block)
- end
-
- def filter(criteria)
- TodoSubList.new select(&criteria)
- end
-
def tasks_todo
count(&Criteria.status(:todo))
end
def tasks_in_progress
count(&Criteria.status(:current))
end
def tasks_completed
count(&Criteria.status(:done))
end
def completed?
all?(&Criteria.status(:done))
end
end
-class TodoSubList < TodoList
+class TodoList
+ include Schedule
+
+ def self.parse(backlog)
+ new backlog.lines.map { |entry| Task.parse entry }
+ end
+
def initialize(tasks)
- super
+ @tasks = tasks
end
+ def each(&block)
+ @tasks.each(&block)
+ end
+
+ def filter(criteria)
+ TodoList.new select(&criteria)
+ end
+
def adjoin(other)
- TodoSubList.new to_a | other.to_a
+ TodoList.new to_a | other.to_a
end
end

Георги обнови решението на 06.11.2013 15:02 (преди над 10 години)

class Task < Struct.new :status, :description, :priority, :tags
def self.parse(entry)
status, description, priority, tags = entry.split('|').map(&:strip)
new status.downcase.to_sym, description, priority.downcase.to_sym,
tags ? tags.split(',').map(&:strip) : []
end
end
class Criteria
class << self
def status(acceptable)
new { |task| task.status == acceptable }
end
def priority(acceptable)
new { |task| task.priority == acceptable }
end
def tags(required)
new { |task| (required - task.tags).empty? }
end
end
def initialize(&predicate)
@predicate = predicate
end
def to_proc
@predicate
end
def !
Criteria.new { |task| not to_proc.call task }
end
def &(other)
Criteria.new { |task| to_proc.call task and other.to_proc.call task }
end
def |(other)
Criteria.new { |task| to_proc.call task or other.to_proc.call task }
end
end
-module Schedule
+class TodoList
include Enumerable
- def tasks_todo
- count(&Criteria.status(:todo))
- end
-
- def tasks_in_progress
- count(&Criteria.status(:current))
- end
-
- def tasks_completed
- count(&Criteria.status(:done))
- end
-
- def completed?
- all?(&Criteria.status(:done))
- end
-end
-
-class TodoList
- include Schedule
-
def self.parse(backlog)
new backlog.lines.map { |entry| Task.parse entry }
end
def initialize(tasks)
@tasks = tasks
end
def each(&block)
@tasks.each(&block)
end
def filter(criteria)
TodoList.new select(&criteria)
end
def adjoin(other)
TodoList.new to_a | other.to_a
end
-end
+
+ def tasks_todo
+ count(&Criteria.status(:todo))
+ end
+
+ def tasks_in_progress
+ count(&Criteria.status(:current))
+ end
+
+ def tasks_completed
+ count(&Criteria.status(:done))
+ end
+
+ def completed?
+ all?(&Criteria.status(:done))
+ end
+end