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

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

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

Резултати

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

Код

class Task
attr_accessor :status, :description, :priority, :tags
def initialize status, description, priority, tags
@status = status.downcase.to_sym
@description = description
@priority = priority.downcase.to_sym
@tags = tags
end
end
class TodoList
include Enumerable
attr_accessor :tasks
def initialize tasks
@tasks = tasks
end
def self.parse(text)
tasks = text.lines.map { |task| task.split('|').map &:strip }
tasks = tasks.map do |status, description, priority, tags|
Task.new status, description, priority, tags.split(/,\s*/)
end
new tasks
end
def each
@tasks.each { |task| yield task }
end
def filter(criteria)
self.class.new @tasks.select(&criteria.condition)
end
def tasks_todo
@tasks.map { |task| task.status }.count(:todo)
end
def tasks_in_progress
@tasks.map { |task| task.status }.count(:current)
end
def tasks_completed
@tasks.map { |task| task.status }.count(:done)
end
def completed?
@tasks.map { |task| task.status == :done }.count(false).zero?
end
def adjoin(other)
self.class.new @tasks | other.tasks
end
end
class Criteria
attr_accessor :condition
def initialize(condition)
@condition = condition
end
def |(other)
self.class.new(->(task) { condition.(task) or other.condition.(task) })
end
def &(other)
self.class.new(->(task) { condition.(task) and other.condition.(task) })
end
def !
self.class.new(->(task) { !condition.(task) })
end
class << self
def status(task_status)
new(->(task) { task.status == task_status })
end
def priority(task_priority)
new(->(task) { task.priority == task_priority })
end
def tags(task_tags)
new(->(task) { (task_tags - task.tags).size.zero? })
end
end
end

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

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

Finished in 0.03985 seconds
22 examples, 0 failures

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

Христо обнови решението на 05.11.2013 02:14 (преди над 10 години)

+class Task
+ attr_accessor :status, :description, :priority, :tags
+
+ def initialize task
+ @status, @description, @priority, @tags = task
+ @status, @priority = [@status, @priority].map { |i| i.downcase.to_sym }
+ @tags = @tags.split(/,\s*/)
+ end
+end
+
+
+class TodoList
+ include Enumerable
+
+ attr_accessor :tasks
+
+ def initialize tasks
+ @tasks = tasks
+ end
+
+ def self.parse(text)
+ tasks = text.lines.map { |task| task.split('|') }
+ tasks.map { |task_parts| task_parts.map! &:strip }
+ new(tasks.map { |task| Task.new(task) })
+ end
+
+ def each
+ @tasks.each { |task| yield task }
+ end
+
+ def filter(criteria)
+ self.class.new @tasks.select(&criteria.condition)
+ end
+
+ def tasks_todo
+ @tasks.map { |task| task.status == :todo }.count(true)
+ end
+
+ def tasks_in_progress
+ @tasks.map { |task| task.status == :current }.count(true)
+ end
+
+ def tasks_completed
+ @tasks.map { |task| task.status == :done }.count(true)
+ end
+
+ def completed?
+ @tasks.map { |task| task.status == :done }.count(false).zero?
+ end
+
+ def adjoin(other)
+ self.class.new @tasks | other.tasks
+ end
+end
+
+
+class Criteria
+ attr_accessor :condition
+
+ def initialize(condition)
+ @condition = condition
+ end
+
+ def |(other)
+ Criteria.new(->(task) { condition.(task) or other.condition.(task) })
+ end
+
+ def &(other)
+ Criteria.new(->(task) { condition.(task) and other.condition.(task) })
+ end
+
+ def !
+ Criteria.new(->(task) { !condition.(task) })
+ end
+
+ class << self
+ def status(task_status)
+ Criteria.new(->(task) { task.status == task_status })
+ end
+
+ def priority(task_priority)
+ Criteria.new(->(task) { task.priority == task_priority })
+ end
+
+ def tags(task_tags)
+ Criteria.new(->(task) { (task_tags - task.tags).size.zero? })
+ end
+ end
+end

Христо обнови решението на 06.11.2013 03:50 (преди над 10 години)

class Task
attr_accessor :status, :description, :priority, :tags
- def initialize task
- @status, @description, @priority, @tags = task
- @status, @priority = [@status, @priority].map { |i| i.downcase.to_sym }
- @tags = @tags.split(/,\s*/)
+ def initialize status, description, priority, tags
+ @status = status.downcase.to_sym
+ @description = description
+ @priority = priority.downcase.to_sym
+ @tags = tags
end
end
class TodoList
include Enumerable
-
attr_accessor :tasks
def initialize tasks
@tasks = tasks
end
def self.parse(text)
- tasks = text.lines.map { |task| task.split('|') }
- tasks.map { |task_parts| task_parts.map! &:strip }
- new(tasks.map { |task| Task.new(task) })
+ tasks = text.lines.map { |task| task.split('|').map &:strip }
+ tasks = tasks.map do |status, description, priority, tags|
+ Task.new status, description, priority, tags.split(/,\s*/)
+ end
+
+ new tasks
end
def each
@tasks.each { |task| yield task }
end
def filter(criteria)
self.class.new @tasks.select(&criteria.condition)
end
- def tasks_todo
- @tasks.map { |task| task.status == :todo }.count(true)
- end
-
- def tasks_in_progress
- @tasks.map { |task| task.status == :current }.count(true)
- end
-
- def tasks_completed
- @tasks.map { |task| task.status == :done }.count(true)
- end
-
def completed?
@tasks.map { |task| task.status == :done }.count(false).zero?
end
def adjoin(other)
self.class.new @tasks | other.tasks
end
+
+ def method_missing(name)
+ tasks_statuses = @tasks.map { |task| task.status }
+ return tasks_statuses.count(:todo) if name == :tasks_todo
+ return tasks_statuses.count(:current) if name == :tasks_in_progress
+ return tasks_statuses.count(:done) if name == :tasks_completed
+ super
+ end
+
+ def respond_to_missing?(method, *)
+ method =~ /tasks_((todo)|(in_progress)|(completed))/ || super
+ end
end
class Criteria
attr_accessor :condition
def initialize(condition)
@condition = condition
end
def |(other)
- Criteria.new(->(task) { condition.(task) or other.condition.(task) })
+ self.class.new(->(task) { condition.(task) or other.condition.(task) })
end
def &(other)
- Criteria.new(->(task) { condition.(task) and other.condition.(task) })
+ self.class.new(->(task) { condition.(task) and other.condition.(task) })
end
def !
- Criteria.new(->(task) { !condition.(task) })
+ self.class.new(->(task) { !condition.(task) })
end
class << self
def status(task_status)
- Criteria.new(->(task) { task.status == task_status })
+ new(->(task) { task.status == task_status })
end
def priority(task_priority)
- Criteria.new(->(task) { task.priority == task_priority })
+ new(->(task) { task.priority == task_priority })
end
def tags(task_tags)
- Criteria.new(->(task) { (task_tags - task.tags).size.zero? })
+ new(->(task) { (task_tags - task.tags).size.zero? })
end
end
-end
+end

Христо обнови решението на 06.11.2013 12:54 (преди над 10 години)

class Task
attr_accessor :status, :description, :priority, :tags
def initialize status, description, priority, tags
@status = status.downcase.to_sym
@description = description
@priority = priority.downcase.to_sym
@tags = tags
end
end
class TodoList
include Enumerable
attr_accessor :tasks
def initialize tasks
@tasks = tasks
end
def self.parse(text)
tasks = text.lines.map { |task| task.split('|').map &:strip }
tasks = tasks.map do |status, description, priority, tags|
Task.new status, description, priority, tags.split(/,\s*/)
end
new tasks
end
def each
@tasks.each { |task| yield task }
end
def filter(criteria)
self.class.new @tasks.select(&criteria.condition)
end
- def completed?
- @tasks.map { |task| task.status == :done }.count(false).zero?
+ def tasks_todo
+ @tasks.map { |task| task.status }.count(:todo)
end
- def adjoin(other)
- self.class.new @tasks | other.tasks
+ def tasks_in_progress
+ @tasks.map { |task| task.status }.count(:current)
end
- def method_missing(name)
- tasks_statuses = @tasks.map { |task| task.status }
- return tasks_statuses.count(:todo) if name == :tasks_todo
- return tasks_statuses.count(:current) if name == :tasks_in_progress
- return tasks_statuses.count(:done) if name == :tasks_completed
- super
+ def tasks_completed
+ @tasks.map { |task| task.status }.count(:done)
end
- def respond_to_missing?(method, *)
- method =~ /tasks_((todo)|(in_progress)|(completed))/ || super
+ def completed?
+ @tasks.map { |task| task.status == :done }.count(false).zero?
+ end
+
+ def adjoin(other)
+ self.class.new @tasks | other.tasks
end
end
class Criteria
attr_accessor :condition
def initialize(condition)
@condition = condition
end
def |(other)
self.class.new(->(task) { condition.(task) or other.condition.(task) })
end
def &(other)
self.class.new(->(task) { condition.(task) and other.condition.(task) })
end
def !
self.class.new(->(task) { !condition.(task) })
end
class << self
def status(task_status)
new(->(task) { task.status == task_status })
end
def priority(task_priority)
new(->(task) { task.priority == task_priority })
end
def tags(task_tags)
new(->(task) { (task_tags - task.tags).size.zero? })
end
end
end