-
-
Save anoam/7e7b255e793ab00f2f1d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Handler | |
DEFAULT_NAME = 'empty name' | |
attr_reader :name | |
def initialize(name = DEFAULT_NAME) | |
@name = name | |
end | |
def calculate(array); end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HandlerController | |
# Это Java-Style таким образом передавать используемые классы | |
# (В ruby часто билдят прямо внутри объекта) | |
# Но это согласуется с приципами SOLID. | |
# Можно гибко подолжить другой итератор | |
def initialize(iterator, repository, printer) | |
@iterator = iterator | |
@repository = repository | |
@printer = printer | |
end | |
# Метод меняет инстанс, по-этому он с ! | |
def calculate!(array, names = nil) | |
@repository.clear! | |
@iterator.filter(names).map do |handler| | |
@repository.add(handler.name, handler.calculate(array)) | |
end | |
end | |
def print_out | |
@printer.print(@repository) | |
end | |
def add_handler(handler) | |
@iterator.add_handler(handler) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Iterator | |
def initialize | |
@collection = [] | |
end | |
def add_handler(item) | |
@collection.push(item) | |
end | |
def filter(names) | |
if names | |
@collection.find_all { |item| names.include?(item.name) } | |
else | |
@collection | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MinHandler < Handler | |
def calculate(array) | |
array.min | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Нам не нужно создавать инстанс. Значит достаточно модуля. | |
module Printer | |
def print(repository) | |
repository.each do |result| | |
p "Handler #{result[:name]} return #{result[:result]}" | |
end | |
end | |
module_function :print | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ResultReposytory | |
def initialize | |
@repository = [] | |
end | |
def clear! | |
@repository.clear | |
end | |
def add(name, result) | |
@repository.push(name: name, result: result) | |
end | |
def each(&block) | |
@repository.each(&block) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment