Created
August 24, 2018 21:35
-
-
Save jilucev/4d340d277e22ad514d080e6cacd93298 to your computer and use it in GitHub Desktop.
Enema of the state
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
# frozen_string_literal: true | |
module Hodor | |
class MainService | |
attr_accessor :foo_klass, | |
:foo, | |
:foo_id | |
:foo_status, | |
:bar_service, | |
:baz_service, | |
:qux_service | |
def initialize(foo_klass = Foo) | |
@foo_klass = foo_klass | |
#initialize dependencies | |
@bar_service = Hodor::BarService.new | |
@baz_service = Hodor::BazService.new | |
@qux_service = Hodor::QuxService.new | |
end | |
def call(foo_id, status_name) | |
@foo_id = foo_id | |
@foo = foo_klass.find(foo_id) | |
@foo_status = foo.statuses.where(name: status_name) | |
#call each dependent service | |
# This service updates all bar records with foo_status to have a different status | |
bar_service.(foo_id, foo_status) | |
# This service erases foo_status from bar's history (join records) | |
baz_service.(foo_id, foo_status) | |
# This service erases foo_status from foo (destroys statuses that were associated with foo) | |
qux_service.(foo_id, foo_status) | |
# publish changes to foo | |
foo | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment