Решение на Втора задача от Иван Проданов

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

Към профила на Иван Проданов

Резултати

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

Код

class TodoTask
attr_reader :status, :description, :priority, :tags
def initialize(status, description, priority, tags)
@status = status
@description = description
@priority = priority
@tags = tags
end
end
#Observer for anonymous methods
class Observer
attr_reader :observable
def initialize (observable)
@observable = observable
end
#I would like to use old_observable instead, but god hail the 80char rule...
def &(other)
set_observable { |tasks, old| old.call(tasks) & other.observable.(tasks) }
end
def |(other)
set_observable { |tasks, old| old.call(tasks) | other.observable.(tasks) }
end
def !
set_observable { |tasks, old| tasks - old.call(tasks) }
end
private
def set_observable
old_observable = observable
@observable = ->(tasks) { yield tasks, old_observable }
self
end
end
class Criteria
def Criteria.status (status)
build_observer { |task| task.status == status }
end
def Criteria.priority (priority)
build_observer { |task| task.priority == priority }
end
def Criteria.tags (tags)
build_observer { |task| (task.tags | tags) == task.tags }
end
private
def Criteria.build_observer (&comparer)
return Observer.new ->(tasks) { tasks.select &comparer }
end
end
class TaskParser
def TaskParser.parse (text)
strip_line = ->(line) { line.split('|').each(&:strip!) }
text.each_line.map{ |line| parse_line strip_line.call(line) }
end
private
def TaskParser.parse_line (txt) #genius 80char rule strikes again...
TodoTask.new(status(txt[0]), desc(txt[1]), priority(txt[2]), tags(txt[3]))
end
def TaskParser.status (text)
text.downcase.to_sym
end
def TaskParser.desc (text)
text
end
def TaskParser.priority (text)
text.downcase.to_sym
end
def TaskParser.tags (text)
text.split(',').each(&:strip!)
end
end
class TodoList
include Enumerable
attr_accessor :tasks
def initialize (tasks)
@tasks = tasks
end
def each
tasks.each{ |task| yield task }
end
def TodoList.parse (text)
TodoList.new TaskParser.parse text
end
def filter (observer)
TodoList.new observer.observable.call(tasks)
end
def adjoin (todo_list)
TodoList.new (tasks | todo_list.tasks)
end
def tasks_todo
tasks.select { |task| task.status == :todo }.count
end
def tasks_in_progress
tasks.select { |task| task.status == :current }.count
end
def tasks_completed
tasks.select { |task| task.status == :done }.count
end
def completed?
tasks.all? { |task| task.status == :done }
end
end

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

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

Finished in 0.09606 seconds
22 examples, 0 failures

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

Иван обнови решението на 04.11.2013 11:32 (преди около 11 години)

+class TodoTask
+ attr_reader :status, :description, :priority, :tags
+
+ def initialize(status, description, priority, tags)
+ @status = status
+ @description = description
+ @priority = priority
+ @tags = tags
+ end
+end
+
+class TaskComparer
+ attr_reader :comparable
+
+ def initialize (comparable)
+ @comparable = comparable
+ end
+
+ def compare (tasks)
+ comparable.(tasks)
+ end
+
+ def &(other)
+ old_comparable = comparable
+ @comparable = ->(tasks) {old_comparable.(tasks) & other.compare(tasks)}
+ self
+ end
+
+ def |(other)
+ old_comparable = comparable
+ @comparable = ->(tasks) {old_comparable.(tasks) | other.compare(tasks)}
+ self
+ end
+end
+
+class Criteria
+ def Criteria.status (status)
+ compare = -> (task) { task.status == status}
+ return TaskComparer.new(-> (tasks) { tasks.select &compare })
+ end
+
+ def Criteria.priority (priority)
+ compare = -> (task) { task.priority == priority }
+ return TaskComparer.new(->(tasks) { tasks.select &compare })
+ end
+
+ def Criteria.tags (tags)
+ compare = -> (task) { (task.tags | tags) == task.tags }
+ return TaskComparer.new( ->(tasks) { tasks.select &compare })
+ end
+end
+
+class TaskParser
+ def TaskParser.parse_line (text)
+ TodoTask.new(status(text[0]), desc(text[1]), priority(text[2]), tags(text[3]))
+ end
+
+ def TaskParser.status (text)
+ case(text)
+ when "TODO" then :todo
+ when "CURRENT" then :current
+ when "DONE" then :done
+ end
+ end
+
+ def TaskParser.desc (text)
+ text
+ end
+
+ def TaskParser.priority (text)
+ case(text)
+ when "High" then :high
+ when "Normal" then :normal
+ when "Low" then :low
+ end
+ end
+
+ def TaskParser.tags (text)
+ text.split(',').each(&:strip!)
+ end
+end
+
+class TodoList
+ include Enumerable
+ attr_accessor :tasks
+
+ def initialize (tasks)
+ @tasks = tasks
+ end
+
+ def each
+ tasks.each{|task| yield task}
+ end
+
+ def TodoList.parse (text)
+ strip_line = ->(line) { line.split('|').each(&:strip!) }
+ tasks = text.each_line.map{|line| TaskParser.parse_line (strip_line.(line)) }
+ TodoList.new tasks
+ end
+
+ def filter (comparer)
+ TodoList.new comparer.compare(tasks)
+ end
+
+ def adjoin(todo_list)
+ TodoList.new (tasks | todo_list.tasks)
+ end
+
+ def tasks_todo
+ tasks.select { |task| task.status == :todo}.count
+ end
+
+ def tasks_in_progress
+ tasks.select { |task| task.status == :current}.count
+ end
+
+ def tasks_completed
+ tasks.select { |task| task.status == :done}.count
+ end
+
+ def completed?
+ tasks.all? { |task| task.status == :done}
+ end
+end

Иван обнови решението на 04.11.2013 11:41 (преди около 11 години)

class TodoTask
attr_reader :status, :description, :priority, :tags
def initialize(status, description, priority, tags)
@status = status
@description = description
@priority = priority
@tags = tags
end
end
class TaskComparer
attr_reader :comparable
def initialize (comparable)
@comparable = comparable
end
def compare (tasks)
comparable.(tasks)
end
def &(other)
old_comparable = comparable
- @comparable = ->(tasks) {old_comparable.(tasks) & other.compare(tasks)}
+ @comparable = ->(tasks) { old_comparable.(tasks) & other.compare(tasks) }
self
end
def |(other)
old_comparable = comparable
- @comparable = ->(tasks) {old_comparable.(tasks) | other.compare(tasks)}
+ @comparable = ->(tasks) { old_comparable.(tasks) | other.compare(tasks) }
+ self
+ end
+
+ def !
+ old_comparable = comparable
+ @comparable = ->(tasks) {tasks - old_comparable.(tasks)}
self
end
end
class Criteria
def Criteria.status (status)
compare = -> (task) { task.status == status}
return TaskComparer.new(-> (tasks) { tasks.select &compare })
end
def Criteria.priority (priority)
compare = -> (task) { task.priority == priority }
return TaskComparer.new(->(tasks) { tasks.select &compare })
end
def Criteria.tags (tags)
compare = -> (task) { (task.tags | tags) == task.tags }
return TaskComparer.new( ->(tasks) { tasks.select &compare })
end
end
class TaskParser
def TaskParser.parse_line (text)
TodoTask.new(status(text[0]), desc(text[1]), priority(text[2]), tags(text[3]))
end
def TaskParser.status (text)
case(text)
when "TODO" then :todo
when "CURRENT" then :current
when "DONE" then :done
end
end
def TaskParser.desc (text)
text
end
def TaskParser.priority (text)
case(text)
when "High" then :high
when "Normal" then :normal
when "Low" then :low
end
end
def TaskParser.tags (text)
text.split(',').each(&:strip!)
end
end
class TodoList
include Enumerable
attr_accessor :tasks
def initialize (tasks)
@tasks = tasks
end
def each
tasks.each{|task| yield task}
end
def TodoList.parse (text)
strip_line = ->(line) { line.split('|').each(&:strip!) }
tasks = text.each_line.map{|line| TaskParser.parse_line (strip_line.(line)) }
TodoList.new tasks
end
def filter (comparer)
TodoList.new comparer.compare(tasks)
end
def adjoin(todo_list)
TodoList.new (tasks | todo_list.tasks)
end
def tasks_todo
tasks.select { |task| task.status == :todo}.count
end
def tasks_in_progress
tasks.select { |task| task.status == :current}.count
end
def tasks_completed
tasks.select { |task| task.status == :done}.count
end
def completed?
tasks.all? { |task| task.status == :done}
end
end

Иван обнови решението на 04.11.2013 11:50 (преди около 11 години)

class TodoTask
- attr_reader :status, :description, :priority, :tags
+ attr_reader :status, :description, :priority, :tags
- def initialize(status, description, priority, tags)
- @status = status
- @description = description
- @priority = priority
- @tags = tags
- end
+ def initialize(status, description, priority, tags)
+ @status = status
+ @description = description
+ @priority = priority
+ @tags = tags
+ end
end
class TaskComparer
- attr_reader :comparable
+ attr_reader :comparable
- def initialize (comparable)
- @comparable = comparable
- end
+ def initialize (comparable)
+ @comparable = comparable
+ end
- def compare (tasks)
- comparable.(tasks)
- end
+ def compare (tasks)
+ comparable.(tasks)
+ end
- def &(other)
- old_comparable = comparable
- @comparable = ->(tasks) { old_comparable.(tasks) & other.compare(tasks) }
- self
- end
+ def &(other)
+ old_comparable = comparable
+ @comparable = ->(tasks) { old_comparable.(tasks) & other.compare(tasks) }
+ self
+ end
- def |(other)
- old_comparable = comparable
- @comparable = ->(tasks) { old_comparable.(tasks) | other.compare(tasks) }
- self
- end
+ def |(other)
+ old_comparable = comparable
+ @comparable = ->(tasks) { old_comparable.(tasks) | other.compare(tasks) }
+ self
+ end
- def !
- old_comparable = comparable
- @comparable = ->(tasks) {tasks - old_comparable.(tasks)}
- self
- end
+ def !
+ old_comparable = comparable
+ @comparable = ->(tasks) {tasks - old_comparable.(tasks)}
+ self
+ end
end
class Criteria
- def Criteria.status (status)
- compare = -> (task) { task.status == status}
- return TaskComparer.new(-> (tasks) { tasks.select &compare })
- end
+ def Criteria.status (status)
+ compare = -> (task) { task.status == status}
+ return TaskComparer.new(-> (tasks) { tasks.select &compare })
+ end
- def Criteria.priority (priority)
- compare = -> (task) { task.priority == priority }
- return TaskComparer.new(->(tasks) { tasks.select &compare })
- end
+ def Criteria.priority (priority)
+ compare = -> (task) { task.priority == priority }
+ return TaskComparer.new(->(tasks) { tasks.select &compare })
+ end
- def Criteria.tags (tags)
- compare = -> (task) { (task.tags | tags) == task.tags }
- return TaskComparer.new( ->(tasks) { tasks.select &compare })
- end
+ def Criteria.tags (tags)
+ compare = -> (task) { (task.tags | tags) == task.tags }
+ return TaskComparer.new( ->(tasks) { tasks.select &compare })
+ end
end
class TaskParser
- def TaskParser.parse_line (text)
- TodoTask.new(status(text[0]), desc(text[1]), priority(text[2]), tags(text[3]))
- end
+ def TaskParser.parse_line (txt) #genius 80char rule...
+ TodoTask.new(status(txt[0]), desc(txt[1]), priority(txt[2]), tags(txt[3]))
+ end
- def TaskParser.status (text)
- case(text)
- when "TODO" then :todo
- when "CURRENT" then :current
- when "DONE" then :done
- end
- end
+ def TaskParser.status (text)
+ case(text)
+ when "TODO" then :todo
+ when "CURRENT" then :current
+ when "DONE" then :done
+ end
+ end
- def TaskParser.desc (text)
- text
- end
+ def TaskParser.desc (text)
+ text
+ end
- def TaskParser.priority (text)
- case(text)
- when "High" then :high
- when "Normal" then :normal
- when "Low" then :low
- end
- end
+ def TaskParser.priority (text)
+ case(text)
+ when "High" then :high
+ when "Normal" then :normal
+ when "Low" then :low
+ end
+ end
- def TaskParser.tags (text)
- text.split(',').each(&:strip!)
- end
+ def TaskParser.tags (text)
+ text.split(',').each(&:strip!)
+ end
end
class TodoList
- include Enumerable
- attr_accessor :tasks
+ include Enumerable
+ attr_accessor :tasks
- def initialize (tasks)
- @tasks = tasks
- end
+ def initialize (tasks)
+ @tasks = tasks
+ end
- def each
- tasks.each{|task| yield task}
- end
+ def each
+ tasks.each{|task| yield task}
+ end
- def TodoList.parse (text)
- strip_line = ->(line) { line.split('|').each(&:strip!) }
- tasks = text.each_line.map{|line| TaskParser.parse_line (strip_line.(line)) }
- TodoList.new tasks
- end
+ def TodoList.parse (text)
+ strip_line = ->(line) { line.split('|').each(&:strip!) }
+ #style guide or 80char rule - pick one
+ tasks = text.each_line.map{|line| TaskParser.parse_line (strip_line.(line))}
+ TodoList.new tasks
+ end
- def filter (comparer)
- TodoList.new comparer.compare(tasks)
- end
+ def filter (comparer)
+ TodoList.new comparer.compare(tasks)
+ end
- def adjoin(todo_list)
- TodoList.new (tasks | todo_list.tasks)
- end
+ def adjoin(todo_list)
+ TodoList.new (tasks | todo_list.tasks)
+ end
- def tasks_todo
- tasks.select { |task| task.status == :todo}.count
- end
+ def tasks_todo
+ tasks.select { |task| task.status == :todo}.count
+ end
- def tasks_in_progress
- tasks.select { |task| task.status == :current}.count
- end
+ def tasks_in_progress
+ tasks.select { |task| task.status == :current}.count
+ end
- def tasks_completed
- tasks.select { |task| task.status == :done}.count
- end
+ def tasks_completed
+ tasks.select { |task| task.status == :done}.count
+ end
- def completed?
- tasks.all? { |task| task.status == :done}
- end
+ def completed?
+ tasks.all? { |task| task.status == :done}
+ end
end

Иван обнови решението на 04.11.2013 11:55 (преди около 11 години)

class TodoTask
attr_reader :status, :description, :priority, :tags
def initialize(status, description, priority, tags)
@status = status
@description = description
@priority = priority
@tags = tags
end
end
class TaskComparer
attr_reader :comparable
def initialize (comparable)
@comparable = comparable
end
def compare (tasks)
comparable.(tasks)
end
def &(other)
old_comparable = comparable
@comparable = ->(tasks) { old_comparable.(tasks) & other.compare(tasks) }
self
end
def |(other)
old_comparable = comparable
@comparable = ->(tasks) { old_comparable.(tasks) | other.compare(tasks) }
self
end
def !
old_comparable = comparable
- @comparable = ->(tasks) {tasks - old_comparable.(tasks)}
+ @comparable = ->(tasks) { tasks - old_comparable.(tasks) }
self
end
end
class Criteria
def Criteria.status (status)
- compare = -> (task) { task.status == status}
+ compare = -> (task) { task.status == status }
return TaskComparer.new(-> (tasks) { tasks.select &compare })
end
def Criteria.priority (priority)
compare = -> (task) { task.priority == priority }
return TaskComparer.new(->(tasks) { tasks.select &compare })
end
def Criteria.tags (tags)
compare = -> (task) { (task.tags | tags) == task.tags }
return TaskComparer.new( ->(tasks) { tasks.select &compare })
end
end
class TaskParser
def TaskParser.parse_line (txt) #genius 80char rule...
TodoTask.new(status(txt[0]), desc(txt[1]), priority(txt[2]), tags(txt[3]))
end
def TaskParser.status (text)
case(text)
when "TODO" then :todo
when "CURRENT" then :current
when "DONE" then :done
end
end
def TaskParser.desc (text)
text
end
def TaskParser.priority (text)
case(text)
when "High" then :high
when "Normal" then :normal
when "Low" then :low
end
end
def TaskParser.tags (text)
text.split(',').each(&:strip!)
end
end
class TodoList
include Enumerable
attr_accessor :tasks
def initialize (tasks)
@tasks = tasks
end
def each
- tasks.each{|task| yield task}
+ tasks.each{ |task| yield task }
end
def TodoList.parse (text)
strip_line = ->(line) { line.split('|').each(&:strip!) }
#style guide or 80char rule - pick one
tasks = text.each_line.map{|line| TaskParser.parse_line (strip_line.(line))}
TodoList.new tasks
end
def filter (comparer)
TodoList.new comparer.compare(tasks)
end
def adjoin(todo_list)
TodoList.new (tasks | todo_list.tasks)
end
def tasks_todo
- tasks.select { |task| task.status == :todo}.count
+ tasks.select { |task| task.status == :todo }.count
end
def tasks_in_progress
- tasks.select { |task| task.status == :current}.count
+ tasks.select { |task| task.status == :current }.count
end
def tasks_completed
- tasks.select { |task| task.status == :done}.count
+ tasks.select { |task| task.status == :done }.count
end
def completed?
- tasks.all? { |task| task.status == :done}
+ tasks.all? { |task| task.status == :done }
end
end

Иван обнови решението на 04.11.2013 17:29 (преди около 11 години)

class TodoTask
attr_reader :status, :description, :priority, :tags
def initialize(status, description, priority, tags)
@status = status
@description = description
@priority = priority
@tags = tags
end
end
-class TaskComparer
- attr_reader :comparable
+#Observer for anonymous methods
+class Observer
+ attr_reader :observable
- def initialize (comparable)
- @comparable = comparable
+ def initialize (observable)
+ @observable = observable
end
- def compare (tasks)
- comparable.(tasks)
- end
-
+ #I would like to use old_observable instead, but god hail the 80char rule...
def &(other)
- old_comparable = comparable
- @comparable = ->(tasks) { old_comparable.(tasks) & other.compare(tasks) }
- self
+ set_observable { |tasks, old| old.call(tasks) & other.observable.(tasks) }
end
def |(other)
- old_comparable = comparable
- @comparable = ->(tasks) { old_comparable.(tasks) | other.compare(tasks) }
- self
+ set_observable { |tasks, old| old.call(tasks) | other.observable.(tasks) }
end
def !
- old_comparable = comparable
- @comparable = ->(tasks) { tasks - old_comparable.(tasks) }
+ set_observable { |tasks, old| tasks - old.call(tasks) }
+ end
+
+ private
+ def set_observable
+ old_observable = observable
+ @observable = ->(tasks) { yield tasks, old_observable }
self
end
end
class Criteria
def Criteria.status (status)
- compare = -> (task) { task.status == status }
- return TaskComparer.new(-> (tasks) { tasks.select &compare })
+ build_observer { |task| task.status == status }
end
def Criteria.priority (priority)
- compare = -> (task) { task.priority == priority }
- return TaskComparer.new(->(tasks) { tasks.select &compare })
+ build_observer { |task| task.priority == priority }
end
def Criteria.tags (tags)
- compare = -> (task) { (task.tags | tags) == task.tags }
- return TaskComparer.new( ->(tasks) { tasks.select &compare })
+ build_observer { |task| (task.tags | tags) == task.tags }
end
+
+ private
+ def Criteria.build_observer (&comparer)
+ return Observer.new ->(tasks) { tasks.select &comparer }
+ end
end
class TaskParser
- def TaskParser.parse_line (txt) #genius 80char rule...
+ def TaskParser.parse (text)
+ strip_line = ->(line) { line.split('|').each(&:strip!) }
+ text.each_line.map{ |line| parse_line strip_line.call(line) }
+ end
+
+ def TaskParser.parse_line (txt) #genius 80char rule strikes again...
TodoTask.new(status(txt[0]), desc(txt[1]), priority(txt[2]), tags(txt[3]))
end
def TaskParser.status (text)
- case(text)
- when "TODO" then :todo
- when "CURRENT" then :current
- when "DONE" then :done
- end
+ text.downcase.to_sym
end
def TaskParser.desc (text)
text
end
def TaskParser.priority (text)
- case(text)
- when "High" then :high
- when "Normal" then :normal
- when "Low" then :low
- end
+ text.downcase.to_sym
end
def TaskParser.tags (text)
text.split(',').each(&:strip!)
end
end
class TodoList
include Enumerable
attr_accessor :tasks
def initialize (tasks)
@tasks = tasks
end
def each
tasks.each{ |task| yield task }
end
def TodoList.parse (text)
- strip_line = ->(line) { line.split('|').each(&:strip!) }
- #style guide or 80char rule - pick one
- tasks = text.each_line.map{|line| TaskParser.parse_line (strip_line.(line))}
- TodoList.new tasks
+ TodoList.new TaskParser.parse text
end
- def filter (comparer)
- TodoList.new comparer.compare(tasks)
+ def filter (observer)
+ TodoList.new observer.observable.call(tasks)
end
- def adjoin(todo_list)
+ def adjoin (todo_list)
TodoList.new (tasks | todo_list.tasks)
end
def tasks_todo
tasks.select { |task| task.status == :todo }.count
end
def tasks_in_progress
tasks.select { |task| task.status == :current }.count
end
def tasks_completed
tasks.select { |task| task.status == :done }.count
end
def completed?
tasks.all? { |task| task.status == :done }
end
end

Иван обнови решението на 04.11.2013 17:32 (преди около 11 години)

class TodoTask
attr_reader :status, :description, :priority, :tags
def initialize(status, description, priority, tags)
@status = status
@description = description
@priority = priority
@tags = tags
end
end
#Observer for anonymous methods
class Observer
attr_reader :observable
def initialize (observable)
@observable = observable
end
#I would like to use old_observable instead, but god hail the 80char rule...
def &(other)
set_observable { |tasks, old| old.call(tasks) & other.observable.(tasks) }
end
def |(other)
set_observable { |tasks, old| old.call(tasks) | other.observable.(tasks) }
end
def !
set_observable { |tasks, old| tasks - old.call(tasks) }
end
private
def set_observable
old_observable = observable
@observable = ->(tasks) { yield tasks, old_observable }
self
end
end
class Criteria
def Criteria.status (status)
build_observer { |task| task.status == status }
end
def Criteria.priority (priority)
build_observer { |task| task.priority == priority }
end
def Criteria.tags (tags)
build_observer { |task| (task.tags | tags) == task.tags }
end
private
def Criteria.build_observer (&comparer)
return Observer.new ->(tasks) { tasks.select &comparer }
end
end
class TaskParser
def TaskParser.parse (text)
strip_line = ->(line) { line.split('|').each(&:strip!) }
text.each_line.map{ |line| parse_line strip_line.call(line) }
end
+ private
def TaskParser.parse_line (txt) #genius 80char rule strikes again...
TodoTask.new(status(txt[0]), desc(txt[1]), priority(txt[2]), tags(txt[3]))
end
def TaskParser.status (text)
text.downcase.to_sym
end
def TaskParser.desc (text)
text
end
def TaskParser.priority (text)
text.downcase.to_sym
end
def TaskParser.tags (text)
text.split(',').each(&:strip!)
end
end
class TodoList
include Enumerable
attr_accessor :tasks
def initialize (tasks)
@tasks = tasks
end
def each
tasks.each{ |task| yield task }
end
def TodoList.parse (text)
TodoList.new TaskParser.parse text
end
def filter (observer)
TodoList.new observer.observable.call(tasks)
end
def adjoin (todo_list)
TodoList.new (tasks | todo_list.tasks)
end
def tasks_todo
tasks.select { |task| task.status == :todo }.count
end
def tasks_in_progress
tasks.select { |task| task.status == :current }.count
end
def tasks_completed
tasks.select { |task| task.status == :done }.count
end
def completed?
tasks.all? { |task| task.status == :done }
end
end