-
-
Save joegaudet/dc1ad7ca8c5985206989d87919a6b479 to your computer and use it in GitHub Desktop.
Enema of the state
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
# frozen_string_literal: true | |
module Hodor | |
class MainService | |
def initialize( | |
foo_klass: Foo | |
bar_service: Hodor::BarService.new, | |
baz_service: Hodor::BazService.new, | |
qux_service: Hodor::QuxService.new | |
) | |
@foo_klass = foo_klass | |
#initialize dependencies | |
@bar_service = bar_service | |
@baz_service = baz_service | |
@qux_service = qux_service | |
end | |
def call(foo_id, status_name) | |
@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 |
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
module Hodor | |
class MainServiceTest | |
test 'it should call' do | |
sut = MainService.new(foo_klass: fake, bar_service: FakeService) | |
result = sut.call(1) | |
assert_result | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment