Skip to content

Instantly share code, notes, and snippets.

@jilucev
Created August 24, 2018 21:35
Show Gist options
  • Save jilucev/4d340d277e22ad514d080e6cacd93298 to your computer and use it in GitHub Desktop.
Save jilucev/4d340d277e22ad514d080e6cacd93298 to your computer and use it in GitHub Desktop.
Enema of the state
# 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