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

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

Към профила на Никола Ненков

Резултати

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

Код

class TodoList
include Enumerable
attr_reader :todos
def initialize(todos)
@todos = todos
end
def each
return @todos unless block_given?
@todos.each { |todo| yield todo }
end
def filter(criteria)
filtered_todos = @todos.select { |todo| criteria.core.call(todo) }
TodoList.new(filtered_todos)
end
def adjoin(other)
TodoList.new(@todos | other.todos)
end
def tasks_todo
@todos.select { |todo| todo.status == :todo }.count
end
def tasks_in_progress
@todos.select { |todo| todo.status == :current }.count
end
def tasks_completed
@todos.select { |todo| todo.status == :done }.count
end
def completed?
@todos.all? { |todo| todo.status == :done }
end
def self.parse(text)
todos = text.lines.map { |line| parse_line(line) }
TodoList.new(todos)
end
def self.parse_line(line)
todo_raw = line.chomp.split('|').map(&:strip)
[0, 2].each { |index| todo_raw[index] = todo_raw[index].downcase.to_sym }
todo_raw[3] = todo_raw[3].nil? ? [] : todo_raw[3].split(',').map(&:strip)
Todo.new(*todo_raw)
end
end
class Todo
attr_reader :status
attr_reader :description
attr_reader :priority
attr_reader :tags
def initialize(status, description, priority, tags)
@status = status
@description = description
@priority = priority
@tags = tags
end
def eql?(other)
return false unless @status == other.status and @priority == other.priority
return false unless @description == other.description
(@tags - other.tags).empty? and (other.tags - @tags).empty?
end
def hash
status.hash + description.hash + priority.hash + tags.sort.hash
end
end
class Criteria
attr_reader :core
def initialize(core)
@core = core
end
def self.status(todo_status)
status_filter = proc { |todo| todo.status == todo_status }
Criteria.new(status_filter)
end
def self.priority(todo_priority)
priority_filter = proc { |todo| todo.priority == todo_priority }
Criteria.new(priority_filter)
end
def self.tags(todo_tags)
tags_filter = proc { |todo| (todo_tags - todo.tags).empty? }
Criteria.new(tags_filter)
end
def !
negative = proc { |todo| not core.call(todo) }
Criteria.new(negative)
end
def |(other)
union = proc { |todo| core.call(todo) or other.core.call(todo) }
Criteria.new(union)
end
def &(other)
intersection = proc { |todo| core.call(todo) and other.core.call(todo) }
Criteria.new(intersection)
end
end

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

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

Finished in 0.04044 seconds
22 examples, 0 failures

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

Никола обнови решението на 04.11.2013 15:31 (преди над 10 години)

+class TodoList
+
+ include Enumerable
+
+ attr_reader :todos
+
+ def initialize(todos)
+ @todos = todos
+ end
+
+ def each
+ return @todos unless block_given?
+ @todos.each { |todo| yield todo }
+ end
+
+ def filter(criteria)
+ @todos.select { |todo| criteria.core.call(todo) }
+ end
+
+ def adjoin(other)
+ TodoList.new(@todos | other.todos)
+ end
+
+ def tasks_todo
+ @todos.select { |todo| todo.status == :todo }.count
+ end
+
+ def tasks_in_progress
+ @todos.select { |todo| todo.status == :current }.count
+ end
+
+ def tasks_completed
+ @todos.select { |todo| todo.status == :done }.count
+ end
+
+ def completed?
+ @todos.all? { |todo| todo.status == :done }
+ end
+
+ def self.parse(text)
+ todos = text.lines.map { |line| TodoParser.parse(line.chomp) }
+ TodoList.new(todos)
+ end
+end
+
+class Todo
+
+ attr_reader :status
+ attr_reader :description
+ attr_reader :priority
+ attr_reader :tags
+
+ def initialize(status, description, priority, tags)
+ @status = status
+ @description = description
+ @priority = priority
+ @tags = tags
+ end
+
+ def eql?(other)
+ return false unless @status == other.status and @priority == other.priority
+ return false unless @description == other.description
+ (@tags - other.tags).empty? and (other.tags - @tags).empty?
+ end
+
+ def hash
+ status.hash + description.hash + priority.hash + tags.sort.hash
+ end
+end
+
+class Criteria
+
+ attr_reader :core
+
+ def initialize(core)
+ @core = core
+ end
+
+ def self.status(todo_status)
+ core = proc { |todo| todo.status == todo_status }
+ Criteria.new(core)
+ end
+
+ def self.priority(todo_priority)
+ core = proc { |todo| todo.priority == todo_priority}
+ Criteria.new(core)
+ end
+ #TODO tags and !
+ def |(other)
+ union_core = proc { |todo| core.call(todos) or other.core.call(todos) }
+ Criteria.new(union_core)
+ end
+
+ def &(other)
+ section_core = proc { |todo| core.call(todo) and other.core.call(todo) }
+ Criteria.new(section_core)
+ end
+end
+
+class TodoParser
+ def self.parse(text)
+ todo_raw = text.split('|').map(&:strip)
+ [0, 2].each { |index| todo_raw[index] = todo_raw[index].downcase.to_sym }
+ todo_raw[3] = todo_raw[3].split(',').map(&:strip)
+ Todo.new(*todo_raw)
+ end
+end

Никола обнови решението на 04.11.2013 18:04 (преди над 10 години)

class TodoList
include Enumerable
attr_reader :todos
def initialize(todos)
@todos = todos
end
def each
return @todos unless block_given?
@todos.each { |todo| yield todo }
end
def filter(criteria)
- @todos.select { |todo| criteria.core.call(todo) }
+ filtered_todos = @todos.select { |todo| criteria.core.call(todo) }
+ TodoList.new(filtered_todos)
end
def adjoin(other)
TodoList.new(@todos | other.todos)
end
def tasks_todo
@todos.select { |todo| todo.status == :todo }.count
end
def tasks_in_progress
@todos.select { |todo| todo.status == :current }.count
end
def tasks_completed
@todos.select { |todo| todo.status == :done }.count
end
def completed?
@todos.all? { |todo| todo.status == :done }
end
- def self.parse(text)
- todos = text.lines.map { |line| TodoParser.parse(line.chomp) }
+ def TodoList.parse(text)
+ todos = text.lines.map { |line| TodoList.parse_line(line) }
TodoList.new(todos)
end
+
+ def TodoList.parse_line(line)
+ todo_raw = line.chomp.split('|').map(&:strip)
+ [0, 2].each { |index| todo_raw[index] = todo_raw[index].downcase.to_sym }
+ todo_raw[3] = todo_raw[3].split(',').map(&:strip)
+ Todo.new(*todo_raw)
+ end
end
class Todo
attr_reader :status
attr_reader :description
attr_reader :priority
attr_reader :tags
def initialize(status, description, priority, tags)
@status = status
@description = description
@priority = priority
@tags = tags
end
def eql?(other)
return false unless @status == other.status and @priority == other.priority
return false unless @description == other.description
(@tags - other.tags).empty? and (other.tags - @tags).empty?
end
def hash
status.hash + description.hash + priority.hash + tags.sort.hash
end
end
class Criteria
attr_reader :core
def initialize(core)
@core = core
end
- def self.status(todo_status)
+ def Criteria.status(todo_status)
core = proc { |todo| todo.status == todo_status }
Criteria.new(core)
end
- def self.priority(todo_priority)
- core = proc { |todo| todo.priority == todo_priority}
+ def Criteria.priority(todo_priority)
+ core = proc { |todo| todo.priority == todo_priority }
Criteria.new(core)
end
#TODO tags and !
- def |(other)
- union_core = proc { |todo| core.call(todos) or other.core.call(todos) }
- Criteria.new(union_core)
+ def !
+ negative = proc { |todo| not core.call(todo) }
+ Criteria.new(negative)
end
- def &(other)
- section_core = proc { |todo| core.call(todo) and other.core.call(todo) }
- Criteria.new(section_core)
+ def |(other)
+ union = proc { |todo| core.call(todo) or other.core.call(todo) }
+ Criteria.new(union)
end
-end
-class TodoParser
- def self.parse(text)
- todo_raw = text.split('|').map(&:strip)
- [0, 2].each { |index| todo_raw[index] = todo_raw[index].downcase.to_sym }
- todo_raw[3] = todo_raw[3].split(',').map(&:strip)
- Todo.new(*todo_raw)
+ def &(other)
+ intersection = proc { |todo| core.call(todo) and other.core.call(todo) }
+ Criteria.new(intersection)
end
end

Никола обнови решението на 05.11.2013 21:37 (преди над 10 години)

class TodoList
include Enumerable
attr_reader :todos
def initialize(todos)
@todos = todos
end
def each
return @todos unless block_given?
@todos.each { |todo| yield todo }
end
def filter(criteria)
filtered_todos = @todos.select { |todo| criteria.core.call(todo) }
TodoList.new(filtered_todos)
end
def adjoin(other)
TodoList.new(@todos | other.todos)
end
def tasks_todo
@todos.select { |todo| todo.status == :todo }.count
end
def tasks_in_progress
@todos.select { |todo| todo.status == :current }.count
end
def tasks_completed
@todos.select { |todo| todo.status == :done }.count
end
def completed?
@todos.all? { |todo| todo.status == :done }
end
- def TodoList.parse(text)
- todos = text.lines.map { |line| TodoList.parse_line(line) }
+ def self.parse(text)
+ todos = text.lines.map { |line| TodoList.parse_line(line) }
TodoList.new(todos)
end
- def TodoList.parse_line(line)
+ def self.parse_line(line)
todo_raw = line.chomp.split('|').map(&:strip)
[0, 2].each { |index| todo_raw[index] = todo_raw[index].downcase.to_sym }
- todo_raw[3] = todo_raw[3].split(',').map(&:strip)
+ todo_raw[3] = todo_raw[3].nil? ? [] : todo_raw[3].split(',').map(&:strip)
Todo.new(*todo_raw)
end
end
class Todo
attr_reader :status
attr_reader :description
attr_reader :priority
attr_reader :tags
def initialize(status, description, priority, tags)
@status = status
@description = description
@priority = priority
@tags = tags
end
def eql?(other)
return false unless @status == other.status and @priority == other.priority
return false unless @description == other.description
(@tags - other.tags).empty? and (other.tags - @tags).empty?
end
def hash
status.hash + description.hash + priority.hash + tags.sort.hash
end
end
class Criteria
attr_reader :core
def initialize(core)
@core = core
end
- def Criteria.status(todo_status)
- core = proc { |todo| todo.status == todo_status }
- Criteria.new(core)
+ def self.status(todo_status)
+ status_filter = proc { |todo| todo.status == todo_status }
+ Criteria.new(status_filter)
end
- def Criteria.priority(todo_priority)
- core = proc { |todo| todo.priority == todo_priority }
- Criteria.new(core)
+ def self.priority(todo_priority)
+ priority_filter = proc { |todo| todo.priority == todo_priority }
+ Criteria.new(priority_filter)
end
- #TODO tags and !
+
+ def self.tags(todo_tags)
+ tags_filter = proc { |todo| (todo_tags - todo.tags).empty? }
+ Criteria.new(tags_filter)
+ end
+
def !
negative = proc { |todo| not core.call(todo) }
Criteria.new(negative)
end
def |(other)
union = proc { |todo| core.call(todo) or other.core.call(todo) }
Criteria.new(union)
end
def &(other)
intersection = proc { |todo| core.call(todo) and other.core.call(todo) }
Criteria.new(intersection)
end
end

Никола обнови решението на 05.11.2013 21:39 (преди над 10 години)

class TodoList
include Enumerable
attr_reader :todos
def initialize(todos)
@todos = todos
end
def each
return @todos unless block_given?
@todos.each { |todo| yield todo }
end
def filter(criteria)
filtered_todos = @todos.select { |todo| criteria.core.call(todo) }
TodoList.new(filtered_todos)
end
def adjoin(other)
TodoList.new(@todos | other.todos)
end
def tasks_todo
@todos.select { |todo| todo.status == :todo }.count
end
def tasks_in_progress
@todos.select { |todo| todo.status == :current }.count
end
def tasks_completed
@todos.select { |todo| todo.status == :done }.count
end
def completed?
@todos.all? { |todo| todo.status == :done }
end
def self.parse(text)
- todos = text.lines.map { |line| TodoList.parse_line(line) }
+ todos = text.lines.map { |line| TodoList.parse_line(line) }
TodoList.new(todos)
end
def self.parse_line(line)
todo_raw = line.chomp.split('|').map(&:strip)
[0, 2].each { |index| todo_raw[index] = todo_raw[index].downcase.to_sym }
todo_raw[3] = todo_raw[3].nil? ? [] : todo_raw[3].split(',').map(&:strip)
Todo.new(*todo_raw)
end
end
class Todo
attr_reader :status
attr_reader :description
attr_reader :priority
attr_reader :tags
def initialize(status, description, priority, tags)
@status = status
@description = description
@priority = priority
@tags = tags
end
def eql?(other)
return false unless @status == other.status and @priority == other.priority
return false unless @description == other.description
(@tags - other.tags).empty? and (other.tags - @tags).empty?
end
def hash
status.hash + description.hash + priority.hash + tags.sort.hash
end
end
class Criteria
attr_reader :core
def initialize(core)
@core = core
end
def self.status(todo_status)
status_filter = proc { |todo| todo.status == todo_status }
Criteria.new(status_filter)
end
def self.priority(todo_priority)
priority_filter = proc { |todo| todo.priority == todo_priority }
Criteria.new(priority_filter)
end
def self.tags(todo_tags)
tags_filter = proc { |todo| (todo_tags - todo.tags).empty? }
Criteria.new(tags_filter)
end
def !
negative = proc { |todo| not core.call(todo) }
Criteria.new(negative)
end
def |(other)
union = proc { |todo| core.call(todo) or other.core.call(todo) }
Criteria.new(union)
end
def &(other)
intersection = proc { |todo| core.call(todo) and other.core.call(todo) }
Criteria.new(intersection)
end
end

Никола обнови решението на 05.11.2013 21:47 (преди над 10 години)

class TodoList
include Enumerable
attr_reader :todos
def initialize(todos)
@todos = todos
end
def each
return @todos unless block_given?
@todos.each { |todo| yield todo }
end
def filter(criteria)
filtered_todos = @todos.select { |todo| criteria.core.call(todo) }
TodoList.new(filtered_todos)
end
def adjoin(other)
TodoList.new(@todos | other.todos)
end
def tasks_todo
@todos.select { |todo| todo.status == :todo }.count
end
def tasks_in_progress
@todos.select { |todo| todo.status == :current }.count
end
def tasks_completed
@todos.select { |todo| todo.status == :done }.count
end
def completed?
@todos.all? { |todo| todo.status == :done }
end
def self.parse(text)
todos = text.lines.map { |line| TodoList.parse_line(line) }
TodoList.new(todos)
end
def self.parse_line(line)
todo_raw = line.chomp.split('|').map(&:strip)
[0, 2].each { |index| todo_raw[index] = todo_raw[index].downcase.to_sym }
todo_raw[3] = todo_raw[3].nil? ? [] : todo_raw[3].split(',').map(&:strip)
Todo.new(*todo_raw)
end
end
class Todo
attr_reader :status
attr_reader :description
attr_reader :priority
attr_reader :tags
def initialize(status, description, priority, tags)
@status = status
@description = description
@priority = priority
@tags = tags
end
def eql?(other)
return false unless @status == other.status and @priority == other.priority
return false unless @description == other.description
(@tags - other.tags).empty? and (other.tags - @tags).empty?
end
def hash
status.hash + description.hash + priority.hash + tags.sort.hash
end
end
class Criteria
attr_reader :core
def initialize(core)
@core = core
end
def self.status(todo_status)
status_filter = proc { |todo| todo.status == todo_status }
Criteria.new(status_filter)
end
def self.priority(todo_priority)
priority_filter = proc { |todo| todo.priority == todo_priority }
Criteria.new(priority_filter)
end
def self.tags(todo_tags)
tags_filter = proc { |todo| (todo_tags - todo.tags).empty? }
- Criteria.new(tags_filter)
+ Criteria.new(tags_filter)
end
def !
negative = proc { |todo| not core.call(todo) }
Criteria.new(negative)
end
def |(other)
union = proc { |todo| core.call(todo) or other.core.call(todo) }
Criteria.new(union)
end
def &(other)
intersection = proc { |todo| core.call(todo) and other.core.call(todo) }
Criteria.new(intersection)
end
end

Никола обнови решението на 06.11.2013 15:25 (преди над 10 години)

+module TodoParser
+
+ def self.included(base)
+ base.extend(ClassMethods)
+ end
+
+ module ClassMethods
+
+ def parse(text)
+ todos = text.lines.map { |line| parse_line(line) }
+ TodoList.new(todos)
+ end
+
+ def parse_line(line)
+ todo_raw = line.chomp.split('|').map(&:strip)
+ [0, 2].each { |index| todo_raw[index] = todo_raw[index].downcase.to_sym }
+ todo_raw[3] = todo_raw[3].nil? ? [] : todo_raw[3].split(',').map(&:strip)
+ Todo.new(*todo_raw)
+ end
+ end
+end
+
class TodoList
include Enumerable
+ include TodoParser
attr_reader :todos
def initialize(todos)
@todos = todos
end
def each
return @todos unless block_given?
@todos.each { |todo| yield todo }
end
def filter(criteria)
filtered_todos = @todos.select { |todo| criteria.core.call(todo) }
TodoList.new(filtered_todos)
end
def adjoin(other)
TodoList.new(@todos | other.todos)
end
def tasks_todo
@todos.select { |todo| todo.status == :todo }.count
end
def tasks_in_progress
@todos.select { |todo| todo.status == :current }.count
end
def tasks_completed
@todos.select { |todo| todo.status == :done }.count
end
def completed?
@todos.all? { |todo| todo.status == :done }
- end
-
- def self.parse(text)
- todos = text.lines.map { |line| TodoList.parse_line(line) }
- TodoList.new(todos)
- end
-
- def self.parse_line(line)
- todo_raw = line.chomp.split('|').map(&:strip)
- [0, 2].each { |index| todo_raw[index] = todo_raw[index].downcase.to_sym }
- todo_raw[3] = todo_raw[3].nil? ? [] : todo_raw[3].split(',').map(&:strip)
- Todo.new(*todo_raw)
end
end
class Todo
attr_reader :status
attr_reader :description
attr_reader :priority
attr_reader :tags
def initialize(status, description, priority, tags)
@status = status
@description = description
@priority = priority
@tags = tags
end
def eql?(other)
return false unless @status == other.status and @priority == other.priority
return false unless @description == other.description
(@tags - other.tags).empty? and (other.tags - @tags).empty?
end
def hash
status.hash + description.hash + priority.hash + tags.sort.hash
end
end
class Criteria
attr_reader :core
def initialize(core)
@core = core
end
def self.status(todo_status)
status_filter = proc { |todo| todo.status == todo_status }
Criteria.new(status_filter)
end
def self.priority(todo_priority)
priority_filter = proc { |todo| todo.priority == todo_priority }
Criteria.new(priority_filter)
end
def self.tags(todo_tags)
tags_filter = proc { |todo| (todo_tags - todo.tags).empty? }
Criteria.new(tags_filter)
end
def !
negative = proc { |todo| not core.call(todo) }
Criteria.new(negative)
end
def |(other)
union = proc { |todo| core.call(todo) or other.core.call(todo) }
Criteria.new(union)
end
def &(other)
intersection = proc { |todo| core.call(todo) and other.core.call(todo) }
Criteria.new(intersection)
end
end

Никола обнови решението на 06.11.2013 15:27 (преди над 10 години)

module TodoParser
- def self.included(base)
- base.extend(ClassMethods)
- end
+ def self.included(base)
+ base.extend(ClassMethods)
+ end
module ClassMethods
def parse(text)
todos = text.lines.map { |line| parse_line(line) }
TodoList.new(todos)
end
def parse_line(line)
todo_raw = line.chomp.split('|').map(&:strip)
[0, 2].each { |index| todo_raw[index] = todo_raw[index].downcase.to_sym }
todo_raw[3] = todo_raw[3].nil? ? [] : todo_raw[3].split(',').map(&:strip)
Todo.new(*todo_raw)
end
end
end
class TodoList
include Enumerable
include TodoParser
attr_reader :todos
def initialize(todos)
@todos = todos
end
def each
return @todos unless block_given?
@todos.each { |todo| yield todo }
end
def filter(criteria)
filtered_todos = @todos.select { |todo| criteria.core.call(todo) }
TodoList.new(filtered_todos)
end
def adjoin(other)
TodoList.new(@todos | other.todos)
end
def tasks_todo
@todos.select { |todo| todo.status == :todo }.count
end
def tasks_in_progress
@todos.select { |todo| todo.status == :current }.count
end
def tasks_completed
@todos.select { |todo| todo.status == :done }.count
end
def completed?
@todos.all? { |todo| todo.status == :done }
end
end
class Todo
attr_reader :status
attr_reader :description
attr_reader :priority
attr_reader :tags
def initialize(status, description, priority, tags)
@status = status
@description = description
@priority = priority
@tags = tags
end
def eql?(other)
return false unless @status == other.status and @priority == other.priority
return false unless @description == other.description
(@tags - other.tags).empty? and (other.tags - @tags).empty?
end
def hash
status.hash + description.hash + priority.hash + tags.sort.hash
end
end
class Criteria
attr_reader :core
def initialize(core)
@core = core
end
def self.status(todo_status)
status_filter = proc { |todo| todo.status == todo_status }
Criteria.new(status_filter)
end
def self.priority(todo_priority)
priority_filter = proc { |todo| todo.priority == todo_priority }
Criteria.new(priority_filter)
end
def self.tags(todo_tags)
tags_filter = proc { |todo| (todo_tags - todo.tags).empty? }
Criteria.new(tags_filter)
end
def !
negative = proc { |todo| not core.call(todo) }
Criteria.new(negative)
end
def |(other)
union = proc { |todo| core.call(todo) or other.core.call(todo) }
Criteria.new(union)
end
def &(other)
intersection = proc { |todo| core.call(todo) and other.core.call(todo) }
Criteria.new(intersection)
end
end

Никола обнови решението на 06.11.2013 15:36 (преди над 10 години)

-module TodoParser
-
- def self.included(base)
- base.extend(ClassMethods)
- end
-
- module ClassMethods
-
- def parse(text)
- todos = text.lines.map { |line| parse_line(line) }
- TodoList.new(todos)
- end
-
- def parse_line(line)
- todo_raw = line.chomp.split('|').map(&:strip)
- [0, 2].each { |index| todo_raw[index] = todo_raw[index].downcase.to_sym }
- todo_raw[3] = todo_raw[3].nil? ? [] : todo_raw[3].split(',').map(&:strip)
- Todo.new(*todo_raw)
- end
- end
-end
-
class TodoList
include Enumerable
- include TodoParser
attr_reader :todos
def initialize(todos)
@todos = todos
+ end
+
+ def self.parse(text)
+ todos = text.lines.map { |line| TodoList.parse_line(line) }
+ TodoList.new(todos)
+ end
+
+ def self.parse_line(line)
+ todo_raw = line.chomp.split('|').map(&:strip)
+ [0, 2].each { |index| todo_raw[index] = todo_raw[index].downcase.to_sym }
+ todo_raw[3] = todo_raw[3].nil? ? [] : todo_raw[3].split(',').map(&:strip)
+ Todo.new(*todo_raw)
end
def each
return @todos unless block_given?
@todos.each { |todo| yield todo }
end
def filter(criteria)
filtered_todos = @todos.select { |todo| criteria.core.call(todo) }
TodoList.new(filtered_todos)
end
def adjoin(other)
TodoList.new(@todos | other.todos)
end
def tasks_todo
@todos.select { |todo| todo.status == :todo }.count
end
def tasks_in_progress
@todos.select { |todo| todo.status == :current }.count
end
def tasks_completed
@todos.select { |todo| todo.status == :done }.count
end
def completed?
@todos.all? { |todo| todo.status == :done }
end
end
class Todo
attr_reader :status
attr_reader :description
attr_reader :priority
attr_reader :tags
def initialize(status, description, priority, tags)
@status = status
@description = description
@priority = priority
@tags = tags
end
def eql?(other)
return false unless @status == other.status and @priority == other.priority
return false unless @description == other.description
(@tags - other.tags).empty? and (other.tags - @tags).empty?
end
def hash
status.hash + description.hash + priority.hash + tags.sort.hash
end
end
class Criteria
attr_reader :core
def initialize(core)
@core = core
end
def self.status(todo_status)
status_filter = proc { |todo| todo.status == todo_status }
Criteria.new(status_filter)
end
def self.priority(todo_priority)
priority_filter = proc { |todo| todo.priority == todo_priority }
Criteria.new(priority_filter)
end
def self.tags(todo_tags)
tags_filter = proc { |todo| (todo_tags - todo.tags).empty? }
Criteria.new(tags_filter)
end
def !
negative = proc { |todo| not core.call(todo) }
Criteria.new(negative)
end
def |(other)
union = proc { |todo| core.call(todo) or other.core.call(todo) }
Criteria.new(union)
end
def &(other)
intersection = proc { |todo| core.call(todo) and other.core.call(todo) }
Criteria.new(intersection)
end
end

Никола обнови решението на 06.11.2013 15:38 (преди над 10 години)

class TodoList
include Enumerable
attr_reader :todos
def initialize(todos)
@todos = todos
end
- def self.parse(text)
- todos = text.lines.map { |line| TodoList.parse_line(line) }
- TodoList.new(todos)
- end
-
- def self.parse_line(line)
- todo_raw = line.chomp.split('|').map(&:strip)
- [0, 2].each { |index| todo_raw[index] = todo_raw[index].downcase.to_sym }
- todo_raw[3] = todo_raw[3].nil? ? [] : todo_raw[3].split(',').map(&:strip)
- Todo.new(*todo_raw)
- end
-
def each
return @todos unless block_given?
@todos.each { |todo| yield todo }
end
def filter(criteria)
filtered_todos = @todos.select { |todo| criteria.core.call(todo) }
TodoList.new(filtered_todos)
end
def adjoin(other)
TodoList.new(@todos | other.todos)
end
def tasks_todo
@todos.select { |todo| todo.status == :todo }.count
end
def tasks_in_progress
@todos.select { |todo| todo.status == :current }.count
end
def tasks_completed
@todos.select { |todo| todo.status == :done }.count
end
def completed?
@todos.all? { |todo| todo.status == :done }
+ end
+
+ def self.parse(text)
+ todos = text.lines.map { |line| TodoList.parse_line(line) }
+ TodoList.new(todos)
+ end
+
+ def self.parse_line(line)
+ todo_raw = line.chomp.split('|').map(&:strip)
+ [0, 2].each { |index| todo_raw[index] = todo_raw[index].downcase.to_sym }
+ todo_raw[3] = todo_raw[3].nil? ? [] : todo_raw[3].split(',').map(&:strip)
+ Todo.new(*todo_raw)
end
end
class Todo
attr_reader :status
attr_reader :description
attr_reader :priority
attr_reader :tags
def initialize(status, description, priority, tags)
@status = status
@description = description
@priority = priority
@tags = tags
end
def eql?(other)
return false unless @status == other.status and @priority == other.priority
return false unless @description == other.description
(@tags - other.tags).empty? and (other.tags - @tags).empty?
end
def hash
status.hash + description.hash + priority.hash + tags.sort.hash
end
end
class Criteria
attr_reader :core
def initialize(core)
@core = core
end
def self.status(todo_status)
status_filter = proc { |todo| todo.status == todo_status }
Criteria.new(status_filter)
end
def self.priority(todo_priority)
priority_filter = proc { |todo| todo.priority == todo_priority }
Criteria.new(priority_filter)
end
def self.tags(todo_tags)
tags_filter = proc { |todo| (todo_tags - todo.tags).empty? }
Criteria.new(tags_filter)
end
def !
negative = proc { |todo| not core.call(todo) }
Criteria.new(negative)
end
def |(other)
union = proc { |todo| core.call(todo) or other.core.call(todo) }
Criteria.new(union)
end
def &(other)
intersection = proc { |todo| core.call(todo) and other.core.call(todo) }
Criteria.new(intersection)
end
end

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

class TodoList
include Enumerable
attr_reader :todos
def initialize(todos)
@todos = todos
end
def each
return @todos unless block_given?
@todos.each { |todo| yield todo }
end
def filter(criteria)
filtered_todos = @todos.select { |todo| criteria.core.call(todo) }
TodoList.new(filtered_todos)
end
def adjoin(other)
TodoList.new(@todos | other.todos)
end
def tasks_todo
@todos.select { |todo| todo.status == :todo }.count
end
def tasks_in_progress
@todos.select { |todo| todo.status == :current }.count
end
def tasks_completed
@todos.select { |todo| todo.status == :done }.count
end
def completed?
@todos.all? { |todo| todo.status == :done }
end
def self.parse(text)
- todos = text.lines.map { |line| TodoList.parse_line(line) }
+ todos = text.lines.map { |line| parse_line(line) }
TodoList.new(todos)
end
def self.parse_line(line)
todo_raw = line.chomp.split('|').map(&:strip)
[0, 2].each { |index| todo_raw[index] = todo_raw[index].downcase.to_sym }
todo_raw[3] = todo_raw[3].nil? ? [] : todo_raw[3].split(',').map(&:strip)
Todo.new(*todo_raw)
end
end
class Todo
attr_reader :status
attr_reader :description
attr_reader :priority
attr_reader :tags
def initialize(status, description, priority, tags)
@status = status
@description = description
@priority = priority
@tags = tags
end
def eql?(other)
return false unless @status == other.status and @priority == other.priority
return false unless @description == other.description
(@tags - other.tags).empty? and (other.tags - @tags).empty?
end
def hash
status.hash + description.hash + priority.hash + tags.sort.hash
end
end
class Criteria
attr_reader :core
def initialize(core)
@core = core
end
def self.status(todo_status)
status_filter = proc { |todo| todo.status == todo_status }
Criteria.new(status_filter)
end
def self.priority(todo_priority)
priority_filter = proc { |todo| todo.priority == todo_priority }
Criteria.new(priority_filter)
end
def self.tags(todo_tags)
tags_filter = proc { |todo| (todo_tags - todo.tags).empty? }
Criteria.new(tags_filter)
end
def !
negative = proc { |todo| not core.call(todo) }
Criteria.new(negative)
end
def |(other)
union = proc { |todo| core.call(todo) or other.core.call(todo) }
Criteria.new(union)
end
def &(other)
intersection = proc { |todo| core.call(todo) and other.core.call(todo) }
Criteria.new(intersection)
end
end