Last active
November 1, 2016 12:22
-
-
Save yesnik/10dbeb6f4939fe38595802239866e205 to your computer and use it in GitHub Desktop.
Базовый класс сервиса
This file contains hidden or 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 ServiceBase | |
attr_reader :data | |
class << self | |
def call(data) | |
instance = new(data) | |
instance.call | |
instance | |
end | |
end | |
# Сервис может принимать на вход данные | |
# Их можно получить, вызвав метод .data | |
def initialize(data) | |
@data = data | |
end | |
# В методе содержатся действия, которые сервис выполняет. | |
# Его нужно определить в дочернем классе | |
def call | |
raise NotImplementedError | |
end | |
end | |
# Пример сервиса | |
class StatusService < ServiceBase | |
attr_reader :status | |
def call | |
set_status | |
end | |
private | |
def set_status | |
@status = true | |
end | |
end | |
# Пример использования сервиса | |
data = {a: 1, b: 2} | |
service = StatusService.call(data) | |
service.data | |
# => {:a=>1, :b=>2} | |
service.status | |
# => true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment