Skip to content

Instantly share code, notes, and snippets.

@joegaudet
Created August 24, 2018 22:34
Show Gist options
  • Select an option

  • Save joegaudet/a3254afad7237291beb7039d34fb19cf to your computer and use it in GitHub Desktop.

Select an option

Save joegaudet/a3254afad7237291beb7039d34fb19cf to your computer and use it in GitHub Desktop.
module Fooo
class FindById
def call(id)
Foo.find(id)
end
end
class FindStatus
def call(id, name)
Foo.find(id).where(status: name)
end
end
end
# frozen_string_literal: true
module Hodor
class MainService
def initialize(
find_foo: Foo::FindById.new,
find_status: Foo::FindStatus.new,
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 = @find_foo.(foo_id)
foo_status = @find_status.(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
class FakeQuery
def initialize(result)
@result = result
end
def call
@result
end
end
class FakeService
def initialize(args, result, exception = nil)
@args = args
@result = result
end
def call(args*)
raise exception if exception != nil
result
end
end
module Hodor
class MainServiceTest
test 'it do thing' do
id = 1
status_name = 'asdf'
foo = Foo.new(id: 1)
fake_find = FakeQuery.new(foo)
fake_find_status = FakeQuery.new([Status.new(name: status_name)])
sut = Hodor.new(
find_foo: fake_find
find_status: fake_find_status,
bar_service: FakeService.new([id, status_name], foo)
baz_service: FakeService.new([id, status_name], foo),
qux_service: FakeService.new([id, status_name], foo)
)
actual = sut.(id, status_name)
assert_something
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment